我收到错误:
标准表达式中的数据类型不匹配
使用此代码时。并使用Access数据库。
OleDbConnection bab = new OleDbConnection();
bab.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sdega\OneDrive\school\Werknemersdata.accdb;Persist Security Info=False;";
bab.Open();
try
{
OleDbCommand kaas = new OleDbCommand();
kaas.Connection = bab;
kaas.CommandText = "insert into Werknemersdata (Naam, Adres, Postcode, Woonplaats, Salaris) values ('" + txtNaam.Text + "', '" + txtAdress.Text + "', '" + txtpostcode1.Text + " " +txtpostcode2.Text + "', '" + txtwoonplaats.Text + "', '" + txtsalaris.Text + "') ";
kaas.ExecuteNonQuery(); // this is where it goes wrong
txtStatus.BackColor = Color.Green;
MessageBox.Show("data saved");
bab.Close();
}
catch (Exception ghakbal)
{
MessageBox.Show("Error" + ghakbal);
}
答案 0 :(得分:2)
您在name
之后错过了一个'
,在'" + txtpostcode1.Text + "
之前错过了一个" +txtpostcode2.Text + "'
,在它们之间错过了一个,
。它应该是这样的:
'" + txtpostcode1.Text + "' , '" +txtpostcode2.Text + "',
另外,我强烈建议您始终使用parameterized queries来避免SQL Injection。像这样:
kaas.CommandText = "insert into Werknemersdata (Naam, Adres, Postcode, Woonplaats, Salaris) values (?, ? ,.....");
kaas.Parameters.AddWithValue("Naam", txtNaam.Text);
kaas.Parameters.AddWithValue("Adres", txtAdress.Text);
//And other parameters...
最好直接指定类型并使用Value
属性。阅读更多here。