我在c#中执行update语句遇到了一些问题。好吧,如果我在查询字符串中写一些特定的值,如
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();
答案 0 :(得分:2)
您正在使用SqlParameter
添加Add
。请改用AddWithValue
:
query.Parameters.AddwithValue("@Username", User_Username);
答案 1 :(得分:1)
首先尝试按照您在字符串中定义的顺序定义参数。 在过去,我遇到了问题,但我不得不说我此时使用过ADO Connection。 但这可能是一次尝试。 也许参数不会在内部被名称引用。
(但我希望这不是问题。希望这已经修复了)
像这样:
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;
// changed the order of adding values here
query.Parameters.Add("@FirstName", User_FirstName);
query.Parameters.Add("@LastName", User_LastName);
query.Parameters.Add("@Username", User_Username);
query.ExecuteNonQuery();
Session.Add("FirstName", User_FirstName);
Session.Add("LastName", User_LastName);
StatusMessage.Text = "Updated!";
command.Connection.Close();
答案 2 :(得分:0)
SqlCommand query = new SqlCommand(querystr, connection);
query.CommandType = CommandType.Text //try to add this line.
并尝试为第二个sqlcommand打开一个新的sqlconnection。
答案 3 :(得分:0)
尝试隔离更新查询并添加SqlParameters的新实例。还要检查User_Username
变量是否获得数据库中存在的值。
string User_Username = Session["Username"].ToString();
string User_FirstName = FirstNameEdit.Text;
string User_LastName = LastNameEdit.Text;
string cnnStr = "Data Source=localhost;Initial Catalog=Break; Integrated Security=True";
using (SqlConnection connection = new SqlConnection(cnnStr))
{
//Commented as not using
//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);
//Add a new SqlParameter()...
query.Parameters.Add(new SqlParameter("@Username", SqlDbType.NVarChar)).Value = User_Username;
query.Parameters.Add(new SqlParameter("@Firstname", SqlDbType.NVarChar)).Value = User_FirstName;
query.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar)).Value = User_LastName;
connection.Open();
query.ExecuteNonQuery();
}
//Rest of the code
Session.Add("FirstName", User_FirstName);
Session.Add("LastName", User_LastName);
StatusMessage.Text = "Updated!";