[英]C# Access Db update query not Updating when data is changed
[英]C# Access Db update query not working
我正在嘗試使用下面提到的代碼更新訪問表。 但是,更新不會執行。 它不會給我任何錯誤,但不會更新數據庫。 有什么建議么?
string Const = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\Db\\test.accdb";
OleDbCommand Cmd;
OleDbConnection con22 = new OleDbConnection(Const );
con22.Open();
string sql = "UPDATE CostT SET tFormSent='" + Selection1.Text + "',TName='" + UserName.Text + "',FormDate='" + FormDate.Text + "',where ReqNum=" + ReqNum.Text;
cmd = new OleDbCommand(sql, con22);
cmd.ExecuteNonQuery();
con22.Close();
MessageBox.Show("Form has been Updated");
您的查詢存在語法錯誤: WHERE
子句前有一個逗號,該逗號不屬於該逗號。
但更重要的是:您的代碼可以進行SQL注入 ! 請不要將用戶輸入直接插入查詢中,而應使用參數化查詢!
嘗試更改查詢
至
string sql = "UPDATE CostT SET tFormSent = @selection1,TName = @UserName,FormDate = @FormDate where ReqNum = @ReqNum";
cmd = new OleDbCommand(sql, con22);
cmd.Parameters.Add("@selection1", Selection1.Text);
cmd.Parameters.Add("@UserName", UserName.Text);
cmd.Parameters.Add("@FromDate", FromDate.Text);
cmd.Parameters.Add("@ReqNum", ReqNum.Text);
cmd.ExecuteNonQuery();
con22.Close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.