我正在尝试使用以下功能将记录添加到数据库中
错误发生在
adapter.InsertCommand.ExecuteNonQuery();
完整代码:
public void Insert()
{
string query;
myDB.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT * From Eiendom", myDB);
query = @"INSERT INTO Eiendom (AreaID, AgentID, KlientID, AdresID, EiedomAantalBadkamers, EiedomAantalSlaapkamers, EiendomPrys, EiedomSwembad, EiedomGarages, EiedomAantalVloere, EiedomOppervlakte, EiedomTipePlan, EiedomAdisioneleInligting)
VALUES ('" + areaID + " , " + agentID + " , " + klientID + " , "
+ adressID + " , " + badkamers + " , " + slaapkamers + " , " + prys + " . "
+ " , " + swembad + " , " + garages + " , " + vloere + " , " + oppervlakte
+ " , " + plan + " , " + inligting + "')" ;
OleDbCommand insert = new OleDbCommand(query, myDB);
adapter.InsertCommand = insert;
adapter.InsertCommand.ExecuteNonQuery();
DataSet ds = new DataSet();
adapter.Fill(ds, "Eiendom");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Eiendom";
myDB.Close();
}
我看到一条错误消息;
System.Data.OleDb.OleDbException:'查询值和目标字段的数量不同。'
我实际上确实理解了为什么这种情况会自我呈现,因为我没有添加数据库中的自动编号列
所以我要问的是,作为添加记录的一种解决方法,我可以做些什么,但是添加记录时自动编号列会自动填充?
答案 0 :(得分:0)
您不需要手工制作InsertCommamd。 OleDbCommandBuilder可以为您完成所有操作。我认为它应该处理自动编号。
var cbr = new OleDbCommandBuilder(adapter);
cbr.QuotePrefix = "[";
cbr.QuoteSuffix = "]";
adapter.InsertCommand = cbr.GetInsertCommand(true);