使用一个按钮更新两个表 [英] Updating two tables with one button
问题描述
我有两个不同的表我想用一个按钮更新这两个表.....
我的按钮工作只有一个表更新另一个表不是更新你可以任意一个......
我尝试过:
i have two different table i want to update these two tables with one button.....
my button is working only one table updated another table is not updating Can u plz Any One...
What I have tried:
private void button2_Click(object sender, EventArgs e)
{
cmd = new OleDbCommand("update InvoiceItems set Quantity = '" + txtQty.Text + "',UOM = '" + txtUOM.Text + "',UnitPrice ='" + txtUnitPrice.Text + "',GrossAmount ='" + txtGrossAmount.Text + "',VAT = '" + txtVAT.Text + "',VatAmount ='" + txtVatAmount.Text + "',Total ='" + txtTotal.Text + "' where InvoiceItemId = " + txtInvoiceNo.Text + "", conn);
conn.Open();
cmd.ExecuteNonQuery();
cmd = new OleDbCommand("update Products set ProductName = '" + txtProductName.Text + "' where ProductId =" + txtpid.Text + "",conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Sucess");
conn.Close();
// DisplayData();
conn.Open();
DataTable dt = new DataTable();
// adapter = new OleDbDataAdapter("select * from InvoiceItems where InvoiceNumber =" + txtInvoiceNo.Text + "", conn);
string query = "SELECT InvoiceNumber,ProductName,Quantity,UOM,UnitPrice,GrossAmount,VAT,VatAmount,Total FROM InvoiceItems INNER JOIN Products ON InvoiceItems.ProductId = Products.ProductId where InvoiceItems.InvoiceNumber= " + txtInvoiceNo.Text + "";
adapter = new OleDbDataAdapter(query, conn);
adapter.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
ClearData();
}
推荐答案
可能是因为您的查询有语法错误 -
May be because your query has syntax errors-
cmd = new OleDbCommand(""update InvoiceItems set Quantity = '" + txtQty.Text +"';,UOM = '" + txtUOM.Text + "',UnitPrice ='" + txtUnitPrice.Text + "',GrossAmount ='" + txtGrossAmount.Text + "',VAT = '" + txtVAT.Text + "',VatAmount ='" + txtVatAmount.Text + "',Total ='" + txtTotal.Text + "' where InvoiceItemId = " + txtInvoiceNo.Text + "", conn);
- 更新..
应为更新..
- Quantity ='+ txtQty.Text +';,
,此处不需要分号,如果是数字数据类型列单引号也不是必需的,因此应该 Quantity =+ txtQty.Text +,
- 其他不需要单引号列也像UnitPrice,GrossAmount,VatAmount,Total等。
非常非常重要:
您的代码容易受到 SQLInjection [ ^ ]
请按照以下链接了解如何防止SQL注入 -
如何:防止ASP.NET中的SQL注入 [ ^ ]
SQL注入攻击,其示例和预防机制ASP.Net中的技术和技术 [ ^ ]
希望,它有帮助:)
-- ""update..
should be "update..
-- Quantity = '" + txtQty.Text +"';,
, no semicolon required here and if it's a numeric datatype column single quote also not required, so should be Quantity = " + txtQty.Text +",
-- Single quotes not required for other columns too like UnitPrice, GrossAmount, VatAmount , Total etc.
Very very important:
Your code is vulnerable to SQLInjection[^]
Please follow below links to learn how to prevent SQL Injection-
How To: Protect From SQL Injection in ASP.NET[^]
SQL Injection Attack, its examples and Prevention mechanisms and Techniques in ASP.Net[^]
Hope, it helps :)
这篇关于使用一个按钮更新两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!