我在 C# 中执行更新语句时遇到了一些问题。好吧,如果我在查询字符串中写一些特定的值,比如
string querystr = "UPDATE Users SET User_FirstName='My Firstname', User_LastName='My Lastname' WHERE User_Username='username'";
但是当我使用parameter.Add
写入查询字符串时,它不起作用。所以我遇到了一些麻烦,是如何让参数在我的查询字符串中工作。
到目前为止,这是我的脚本
SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=Break; Integrated Security=True");
SqlCommand command = new SqlCommand("SELECT * FROM Users", connection);
command.Connection.Open();
string querystr = "UPDATE Users SET User_FirstName = @FirstName, User_LastName= @LastName WHERE User_Username = @Username";
SqlCommand query = new SqlCommand(querystr, connection);
string User_Username = Session["Username"].ToString();
string User_FirstName = FirstNameEdit.Text;
string User_LastName = LastNameEdit.Text;
query.Parameters.Add("@Username", User_Username);
query.Parameters.Add("@FirstName", User_FirstName);
query.Parameters.Add("@LastName", User_LastName);
query.ExecuteNonQuery();
Session.Add("FirstName", User_FirstName);
Session.Add("LastName", User_LastName);
StatusMessage.Text = "Updated!";
command.Connection.Close();