插入访问数据库

本文关键字:数据库 访问 插入 | 更新日期: 2023-09-27 18:08:13

我有麻烦插入数据从文本框到ms访问数据库,我得到一个错误" Syntax error in INSERT INTO. "

有人能帮我一下吗?下面是代码:
public void button1_Click(object sender, EventArgs e)//save
{ 
using (OleDbConnection conn = new   
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
Source=|DataDirectory|'productdb.mdb"))
{
OleDbCommand CmdSql = new OleDbCommand("Insert into [product](Kod, names, 
price,type,volume,manufacturer,importer)
enter code here
{
conn.Open();
CmdSql.Parameters.AddWithValue("@Kod", textBox1.Text);
CmdSql.Parameters.AddWithValue("@names", textBox2.Text);
CmdSql.Parameters.AddWithValue("@price", textBox3.Text);
CmdSql.Parameters.AddWithValue("@type", textBox4.Text);
CmdSql.Parameters.AddWithValue("@volume", textBox5.Text);
CmdSql.Parameters.AddWithValue("@manufacturer", textBox6.Text);
CmdSql.Parameters.AddWithValue("@importer", textBox7.Text);
CmdSql.ExecuteNonQuery();// i get the error here<<<
conn.Close();
}
}

插入访问数据库

您缺少插入语句的VALUES部分:

OleDbCommand CmdSql = new OleDbCommand("Insert into [product] (Kod, [names], price, type, volume, manufacturer, importer) VALUES (@Kod, @names, @price, @type, @volume, @manufacturer, @importer)", conn);

你正在使用Access和OldeDbCommand…所以你实际上需要使用?而不是一个命名参数:

OleDbCommand CmdSql = new OleDbCommand("Insert into [product] (Kod, [names], price, type, volume, manufacturer, importer) VALUES (?, ?, ?, ?, ?, ?, ?)", conn);

更多信息请参见此问题。

旁注:确保所有保留的关键字都用方括号括起来。

NAMES是MS-Access Jet SQL的保留关键字,您需要将其括在方括号中。这是收到语法错误的原因。(当然,假设缺失的VALUES部分只是一个错别字)。所以正确的语法是:

OleDbCommand CmdSql = new OleDbCommand("Insert into [product] (Kod, [names],price,type," + 
                                       "volume,manufacturer,importer) " +
                                       "VALUES (?, ?, ?, ?, ?, ?, ?)";

我用一个问号改变了参数的占位符。OleDb不支持命名参数,只是一个问号就可以了,但是,最重要的是按照命令所期望的确切顺序将参数添加到OleDbCommand中。

你的代码还有另一个方面需要解决。您可以使用AddWithValue方法来构建参数列表。这意味着参数的数据类型是由值的数据类型隐式派生的。到处都使用TextBox.Text,这是一个字符串。因此,当接收字段是不同类型时(例如Price可能是数字类型),这可能会导致更新出现问题。如果数据库字段不是文本类型,则向传入参数值添加适当的转换。

例如:

// Supposing you have an in place validator for the text to be converted......
CmdSql.Parameters.AddWithValue("@price", Convert.ToDecimal(textBox3.Text));

OleDbCommand不支持命名参数,所以你的SQL语句应该是:

OleDbCommand CmdSql = new OleDbCommand(
    "INSERT INTO [product] " +
    "(Kod, names, price, type, volume, manufacturer ,importer) " +
    "VALUES (?, ?, ?, ?, ?, ?, ?)"
    , conn);

您编写的命令不完整。它应该是这样的:

OleDbCommand CmdSql = new OleDbCommand("Insert into [product](Kod, names, 
price,type,volume,manufacturer,importer) values(@Kod,@names,@price,@type,
@volume,@manufacturer,@importer)");

命名参数只在SqlCommand中支持,在oledbcommand中不支持,所以你必须使用?在命令文本中代替参数

OleDbConnection con = new   
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
Source=|DataDirectory|'productdb.mdb"
String strSQL="Insert into [product](Kod, names, 
price,type,volume,manufacturer,importer) values(@Kod,@names,@price,@type,
@volume,@manufacturer,@importer)"
        OleDBCommand CmdSql= new OleDBCommand(strSQL, con);
        CmdSql.CommandType = CommandType.Text;
        CmdSql.Parameters.AddWithValue("@Kod", textBox1.Text);
        CmdSql.Parameters.AddWithValue("@names", textBox2.Text);
        CmdSql.Parameters.AddWithValue("@price", textBox3.Text);
        CmdSql.Parameters.AddWithValue("@type", textBox4.Text);
        CmdSql.Parameters.AddWithValue("@volume", textBox5.Text);
        CmdSql.Parameters.AddWithValue("@manufacturer", textBox6.Text);
        CmdSql.Parameters.AddWithValue("@importer", textBox7.Text);
        con.Open();
        try
        {
             CmdSql.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }
        finally
        {
            con.Close();
            CmdSql.Dispose();
        }
string Query = "insert into tablename values ('" + txtstring.text + "', " + txtDouble.text + ")";
Cmd = new OleDbCommand();
Cmd.Connection = Con;
Cmd.CommandText = Query;
Cmd.ExecuteNonQuery();

插入总是这样最简单,快速和难忘的方式。

String query = "Insert into Supplier(Kod, names,price,type,volume,manufacturer,importer) values('" + textBox1.text + "','" +textBox2.text + "','" + textBox3.text + "','" + textBox4.text + "','" + textBox5.text + "','" + textBox6.text + "','" + textBox7.text + "') ";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

fields = "T1,T2,T3,T4,T5,T6,T7,T8";值=
"纳杰菲"、"DONYA"、"3/26/2014 12:00:00点","کد:1نامونامخانوادگی:افشین نجفی','کد:df نام نامخانوادگی:fsdfsdf ','*','-',' 3/4/2014 7:13:29点"表= "表",

 OleDbConnection sc = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;DataSource="+@"G:'sazenama'SazeNama'Sazeama'DBSazeNama.accdb");
                sc.Open();
                OleDbCommand sm;
                if (edit == false)
                    sm = new OleDbCommand("insert into " + Table + "(" + Feilds + ")     values(" + value + "')", sc);
                else
                    sm = new OleDbCommand("update  " + Table + " set " + Feilds + "'", sc);
                sm.ExecuteNonQuery();