1

我在 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();
4

4 回答 4

2

您正在SqlParameter添加Add. 改用AddWithValue

query.Parameters.AddwithValue("@Username", User_Username);
于 2013-11-04T09:01:00.010 回答
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();
于 2013-11-04T09:01:15.633 回答
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!";
于 2013-11-04T09:25:46.477 回答
0
SqlCommand query = new SqlCommand(querystr, connection);
query.CommandType = CommandType.Text //try to add this line.

and also try to open a new sqlconnection for the second sqlcommand.

于 2013-11-04T09:16:38.927 回答