簡體   English   中英

OleDB參數未正確更新

[英]OleDB Parameter not updating properly

con.Open();
com.Connection = con;
String query2 = "update PointCard set Player_User=@a where PC_Pass=@b";
com.CommandText = query2;

com.Parameters.Add(new OleDbParameter("a", txtUser.Text));
com.Parameters.Add(new OleDbParameter("b", txtPass.Text));
MessageBox.Show("Thank you for your continuous support");
com.ExecuteNonQuery();
con.Close();



con.Open();
com.Connection = con;
String query1 = "update PointCard set PC_Status=@x where PC_Pass=@y";
com.CommandText = query1;

com.Parameters.Add(new OleDbParameter("x", txtInactive.Text));
com.Parameters.Add(new OleDbParameter("y", txtPass.Text));
com.ExecuteNonQuery();
MessageBox.Show("PointCard " + txtPass.Text + " has been credited to your account");
con.Close();

我從上一個問題中學到了使用參數化查詢的方法,現在我又感到困惑了。

我遇到的問題是,在執行整個txtUser.Text后,Access表中的PC_StatusPC_Status中的值結束。

即使使用query1重新定義CommandText屬性,為query2添加的參數集合仍然是命令的一部分。

更改OleDbCommand CommandText屬性不會清除或影響它的OleDbParameterCollection 他們將仍然是命令的一部分。

添加新參數時,只需清除它們即可;例如:

com.Parameters.Clear();
com.Parameters.Add(new OleDbParameter("x", txtInactive.Text));
com.Parameters.Add(new OleDbParameter("y", txtPass.Text));

或者,您可以為第二個查詢創建一個新的SqlCommand對象,例如;

String query1 = "update PointCard set PC_Status=@x where PC_Pass=@y";
var com = new OleDbCommand();
com.CommandText = query1;
...
...

另外,請使用using語句自動處理連接和命令,而不要手動Close方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM