C#:无法在我的Microsoft Access数据库中插入数据

时间:2016-10-01 04:20:05

标签: c# .net ms-access

我无法将数据插入Microsoft Access数据库。以下是我的代码:

            roomTableAdapter.Connection.Open();
            string s = "INSERT INTO `room` ([number], [priceroom]) VALUES (@param1,@param2)";
            System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(s, roomTableAdapter.Connection);
            cmd.Parameters.AddWithValue("@param1", textBoxX4.Text);
            cmd.Parameters.AddWithValue("@param2", "4000");
            roomTableAdapter.Adapter.InsertCommand = cmd;
            roomTableAdapter.Adapter.InsertCommand.ExecuteNonQuery();

            roomTableAdapter.Adapter.Fill(this.database3DataSet5);
            roomTableAdapter.Update(database3DataSet5.room);



            dataGridViewX1.Update();
            dataGridViewX1.Refresh();
            roomTableAdapter.Connection.Close();

2 个答案:

答案 0 :(得分:1)

 roomTableAdapter.Connection.Open();
string s = string.Format("INSERT INTO `room` ([number], [priceroom]) VALUES  (@param1,@param2);");
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(s, roomTableAdapter.Connection);
cmd.Parameters.AddWithValue("@param1", textBoxX4.Text);
cmd.Parameters.AddWithValue("@param2", "4000");

roomTableAdapter.Adapter.InsertCommand = cmd;
roomTableAdapter.Adapter.InsertCommand.ExecuteNonQuery();
 roomTableAdapter.Adapter.Fill(this.database3DataSet5);
roomTableAdapter.Update(database3DataSet5.room);

 dataGridViewX1.Update();
dataGridViewX1.Refresh();
roomTableAdapter.Connection.Close();

答案 1 :(得分:1)

应该没有"滴答"在Access SQL中。因此:

string s = string.Format("INSERT INTO room ([number], [priceroom]) VALUES (?,?);");