【问题标题】:insert into access database插入access数据库
【发布时间】:2013-04-09 19:37:44
【问题描述】:

我在将文本框中的数据插入 ms access 数据库时遇到问题,出现错误“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();
}
}

【问题讨论】:

  • “在此处输入代码”您好像缺少某些代码?

标签: c# sql oledbcommand


【解决方案1】:

您缺少插入语句的 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);

更多信息请参见this question。

附注:确保将所有保留关键字括在方括号中。

【讨论】:

    【解决方案2】:

    NAMES 这个词是reserved keyword for 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,这是一个字符串。因此,当接收字段为不同类型(例如价格可能是数字)时,这可能会导致更新出现问题。如果数据库字段不是文本类型,则向传入参数值添加适当的转换。

    例如:

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

    【讨论】:

    • 对AddWithValue隐式转换的良好调用。
    【解决方案3】:

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

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

    【讨论】:

    • 试过这样 OleDbCommand CmdSql = new OleDbCommand("插入 [产品](Kod, [names], price,type,volume,manufacturer,importer) values(@Kod,@names,@价格,@type,@volume,@manufacturer,@importer)");还是不行
    • 再次阅读答案 - OleDbCommand 不支持命名参数。请改用问号。
    • 根据史蒂夫的回答,尝试将列名放入方括号[] 以避免使用保留关键字。
    【解决方案4】:

    你写的命令不完整。应该是这样的:

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

    命名参数仅在 SqlCommand 中不受 oledbcommand 支持,因此您必须使用 ?代替命令文本中的参数。

    【讨论】:

    • 代替@Kod 和命令文本中的其余部分尝试使用?。否则,只需在命令文本中构建实际查询,例如:“插入产品(kod,名称,价格,类型,体积,制造商,进口商)值(22,'我的名字',200,1,1000,'DELL',' APPLE')" 将字符串括在单引号中。
    • 试过这样:OleDbCommand CmdSql = new OleDbCommand("INSERT INTO [product] " +"([Kod], [names], [price], [type], [volume], [制造商] ,[进口商]) " +"VALUES (?Kod,?names,?price,?type,?volume,?manufacturer,?importer)", conn);得到同样的错误
    • 不要使用带有 ? 的名称mark."VALUES (?,?,?,?,?,?,?)", conn);
    • 我已更改:OleDbCommand CmdSql = new OleDbCommand("INSERT INTO [product]" + "(Kod,[names],price,type,volume,manufacturer,importer) " + "VALUES ( ?,?,?,?,?,?,?)", conn);现在我得到一个错误:由于索引、主键或关系中的重复值,没有进行更改。
    • 检查数据库中的键列。是 KDO 吗?那么您的命令可能包含该表中已经存在的 KDO 值。
    【解决方案5】:
    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();
            }
    

    【讨论】:

      【解决方案6】:
      string Query = "insert into tablename values ('" + txtstring.text + "', " + txtDouble.text + ")";
      Cmd = new OleDbCommand();
      Cmd.Connection = Con;
      Cmd.CommandText = Query;
      Cmd.ExecuteNonQuery();
      

      【讨论】:

        【解决方案7】:

        始终以最简单、最快速和最难忘的方式插入。

        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();
        

        【讨论】:

        • 谢谢,这很有帮助。
        • 这为 SQL 注入打开了大门。这可能与您正在做的事情无关,但如果您不使用参数并从用户那里获取文本,那么您会将数据库暴露给 sql 注入。 en.wikipedia.org/wiki/SQL_injection
        【解决方案8】:

        菲尔兹 = "T1,T2,T3,T4,T5,T6,T7,T8" ;价值 =
        “'NAJAFI','DONYA','3/26/2014 12:00:00 AM','کد :1 نامونام خانوادگی:افشین نجفی','کد :df نام و نام خانوادگی:fsdfsdf','*','-','3/4/2014 7:13:29 PM" Table ="Table" ;

         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();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-15
          • 2017-03-05
          • 1970-01-01
          相关资源
          最近更新 更多