插入访问数据库

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

您缺少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); 

有关更多信息,请参阅此问题 。

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

单词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中,因此您必须使用? 在命令文本中代替params。

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

Feilds =“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();