[英]INSERT Statement Error, Not executing
试图从用户从文本框输入的数据中执行SQL语句,一旦他们单击按钮,它就应该插入表中,但是说我的INSERT语句有错误
单击时,这是按钮代码:
private void SaveBtn_Click(object sender, EventArgs e)
{
string Invoice = InvoiceNoTxt.Text;
string Account = AccountTxt.Text;
string dates = textBox1.Text;
string TotalSells = TotalSellTxt.Text;
string Vats = VatTxt.Text;
string TotalCosts = TotalCostTxt.Text;
if (EditChoice == 1)
{
//this is the SQL statement that updates the table
Sql = String.Format("UPDATE InvoiceHeader " + "SET AccountCode = {0}," + "Day = '{1}'," + "TotalSell = {2}, " + "Vat = {3}, " + "TotalCost = {4}, " + "WHERE InvoiceNo = {5};", Account, dates, TotalSells, Vats, TotalCosts, Invoice);
}
else
{
//this is the SQL statement that adds to the table in the database
Sql = String.Format("INSERT INTO InvoiceHeader(AccountCode,Day,TotalSell,Vat,TotalCost) " + "VALUES " + "({0}," + "'{1}'," + "{2}," + "{3}," + "{4});", Account, dates, TotalSells, Vats, TotalCosts);
}
//this is calling the method that executes the SQL code
La(Sql);
//this reloads the data from the database with the new data in it
LoadData();
//this clears the data in the textfields and refreshs the panels to use again
Back();
}
这是执行sql的execute方法:
private void La(String Sql)
{
//this code in the method allows for the sql to execute from all the statements
DbConn = new OleDbConnection(ConString);
DbCmd = new OleDbCommand(Sql, DbConn);
DbConn.Open();
DbCmd.ExecuteNonQuery();
DbConn.Close();
}
这可能是查询文本中的多余逗号。
"UPDATE InvoiceHeader " + "SET AccountCode = {0}," + "Day = '{1}'," + "TotalSell = {2}, " + "Vat = {3}, " + "TotalCost = {4}, " + "WHERE InvoiceNo = {5};"
如果您的查询文本多行,则更容易发现。
"UPDATE InvoiceHeader " +
"SET AccountCode = {0}," +
"Day = '{1}'," +
"TotalSell = {2}, " +
"Vat = {3}, " +
"TotalCost = {4}, " + // <-- here
"WHERE InvoiceNo = {5};"
“ Day”一词可能是系统保留的单词,请尝试在其周围添加方括号,例如...
Sql = String.Format("INSERT INTO InvoiceHeader(AccountCode,[Day],TotalSell,Vat,TotalCost) " + "VALUES " + "({0}," + "'{1}'," + "{2}," + "{3}," + "{4});", Account, dates, TotalSells, Vats, TotalCosts);
Sql = String.Format("UPDATE InvoiceHeader " + "SET AccountCode = {0}," + "[Day] = '{1}'," + "TotalSell = {2}, " + "Vat = {3}, " + "TotalCost = {4}, " + "WHERE InvoiceNo = {5};", Account, dates, TotalSells, Vats, TotalCosts, Invoice);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.