尝试从用户从文本框输入的数据执行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的执行方法:
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();
}
答案 0 :(得分:2)
它可能是查询文本中的额外逗号。
$query_str = "
SELECT company_id
FROM technologies
";
$query = mysqli_query($con, $query_str);
while ($result = mysqli_fetch_assoc($query)) {
$comp = $result['company_id'];
$query_str2 = "
SELECT *
FROM companies
WHERE company_id <> '".$comp."'
";
$query2 = mysqli_query($con, $query_str2);
}
如果您的查询文本在多行上,则更容易发现。
"UPDATE InvoiceHeader " + "SET AccountCode = {0}," + "Day = '{1}'," + "TotalSell = {2}, " + "Vat = {3}, " + "TotalCost = {4}, " + "WHERE InvoiceNo = {5};"
答案 1 :(得分:2)
“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);