我还是c#编程的新手,当我尝试在数据库中添加数据时,任何人都可以帮我解决这个错误,但是这行命令后出现错误.ExecuteNonQuery();
private void button1_Click(object sender, EventArgs e)
{
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\aberto\Documents\esimar_db.accdb;Persist Security Info=False;";
string cmdstr = "insert into employee(employee_name, location,connection,depanse,caisse)values(@employee_name,@location,@connection,@depanse,@caisse)";
OleDbConnection connect = new OleDbConnection(constr);
OleDbCommand command = new OleDbCommand(cmdstr, connect);
connect.Open();
command.Parameters.AddWithValue("@employee_name", textBox1.Text);
command.Parameters.AddWithValue("@location", comboBox1.Text);
command.Parameters.AddWithValue("@connection", textBox2.Text);
command.Parameters.AddWithValue("@depanse", textBox3.Text);
command.Parameters.AddWithValue("@printing", textBox4.Text);
command.Parameters.AddWithValue("@caisse", textBox5.Text);
command.ExecuteNonQuery();
MessageBox.Show( "Record saved successfully");
connect.Close();
}
答案 0 :(得分:1)
CONNECTION
在Access SQL中为reserved word,因此如果要将其用作列(或表)名称,则必须将其括在方括号中:
string cmdstr =
"insert into employee(employee_name, location, [connection], depanse, caisse) values (@employee_name, @location, @connection, @depanse, @caisse)";
另外,请注意,CommandText中只包含五(5)个参数,但您有六(6)个Parameters.AddWithValue
语句。 OleDb 忽略参数名称,只关注参数的声明顺序,因此只有这些语句的前五(5)个会有效。