我试图编写一个update
语句来更新我的 SQL Server 表中的一些信息
到目前为止,这是我的代码,我看不到问题。
protected void Page_Load(object sender, EventArgs e)
{
FirstNameEdit.Text = Session["FirstName"].ToString();
LastNameEdit.Text = Session["LastName"].ToString();
}
protected void SubmitEdit_Click(object sender, EventArgs e)
{
if (FirstNameEdit.Text == "")
{
StatusMessage.Text = "Indtast venligst dit fornavn. ";
}
else
{
if (LastNameEdit.Text == "")
{
StatusMessage.Text = "Indtast venligst dit efternavn. ";
}
else
{
try
{
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='@User_FirstName', User_LastName='@User_LastName' WHERE User_ID='@User_ID'";
SqlCommand query = new SqlCommand(querystr, connection);
string User_ID = Session["ID"].ToString();
string User_FirstName = FirstNameEdit.Text;
string User_LastName = LastNameEdit.Text;
query.Parameters.Add("@User_ID", User_ID);
query.Parameters.Add("@User_FirstName", User_FirstName);
query.Parameters.Add("@User_LastName", User_LastName);
query.ExecuteNonQuery();
string FirstName = FirstNameEdit.Text;
Session.Add("FirstName", FirstName);
string LastName = LastNameEdit.Text;
Session.Add("LastName", LastName);
StatusMessage.Text = "Din profil er opdateret";
command.Connection.Close();
}
catch
{
StatusMessage.Text = "Noget er galt, prøv lidt senere";
}
}
}
}
我也搜索了很多,但它与我发现的完全相同。也许这与 SQL 查询有关。