我在C#中创建一个简单的程序,您可以在其中添加和操作MS Access数据库中的数据。在我的代码中,我添加了一个按钮来更新现有数据,或者创建一个新字段,但我一直收到错误,说我的insert语句有问题。
这是我的代码:
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
String Code = textBox1.Text;
String Name = textBox2.Text;
String Address1 = textBox3.Text;
String Address2 = textBox4.Text;
String Address3 = textBox5.Text;
String balance = textBox6.Text;
String Sales = textBox7.Text;
String Cost = textBox8.Text;
if (Choice == 1)
{
sql = String.Format("UPDATE Debtors " + "SET names = '{0}'," + "address1 = {1}," + "Address2 = {2}," + "Address3 = {3}," + "Balance = '{4}' " + "Sales = '{5}' " + "Cost = '{6}' " + "WHERE accountCode = {7};", Name, Address1, Address2, Address3, balance,Sales,Cost, Code);
}
else
{
sql = String.Format("INSERT INTO Debtors(names, address1, address2, address3, Balance, salesYearToDate, costYearToDate) " + "VALUES " + "('{0}'," + "'{1}'," + "'{2}'," + "'{3}'," + "{4}," + "{5}," + "{6});", Name, Address1, Address2, Address3, balance, Sales, Cost);
}
Ey(sql);
DataLoad();
BackTrack();
}
catch (System.Exception exc)
{
MessageBox.Show(exc.Message);
}
}
答案 0 :(得分:0)
更新声明中的余额和销售后缺少一些逗号。
也不需要分号。
您还需要提交您的陈述。
此致