插入访问数据库

时间:2013-04-09 19:37:45

标签: c# sql oledbcommand

我无法将文本框中的数据插入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();
}
}

8 个答案:

答案 0 :(得分:7)

您缺少insert语句的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

附注:确保将任何保留的关键字包装在方括号中。

答案 1 :(得分: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));

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

OleDbCommand不支持命名参数,因此您的SQL语句应为:

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

答案 4 :(得分:0)

始终以这种方式插入最简单,快速和令人难忘的方式。

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

答案 5 :(得分:0)

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 :(得分:0)

string Query = "insert into tablename values ('" + txtstring.text + "', " + txtDouble.text + ")";
Cmd = new OleDbCommand();
Cmd.Connection = Con;
Cmd.CommandText = Query;
Cmd.ExecuteNonQuery();

答案 7 :(得分:-2)

  

Feilds =“T1,T2,T3,T4,T5,T6,T7,T8”; value =
  “'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();