gpt4 book ai didi

C# 更新 SQL Server 表

转载 作者:行者123 更新时间:2023-11-30 14:31:36 26 4
gpt4 key购买 nike

我已经尝试编写一个 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 查询有关。

最佳答案

到底是什么问题?

我认为在使用参数化查询指定参数时,您不需要再使用引号,因此

string querystr = "UPDATE Users SET User_FirstName='@User_FirstName', User_LastName='@User_LastName' WHERE User_ID='@User_ID'";

应该变成

string querystr = "UPDATE Users SET User_FirstName=@User_FirstName, User_LastName=@User_LastName WHERE User_ID=@User_ID";

关于C# 更新 SQL Server 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19723545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com