gpt4 book ai didi

c# - 为什么不更新我的数据?

转载 作者:太空宇宙 更新时间:2023-11-03 19:11:10 25 4
gpt4 key购买 nike

更新组时出错。必须声明标量变量“@SubnameWHERE”...为什么显示此错误......

protected void cmdUpdate_Click(object sender, EventArgs e)
{
string updateSQL;
updateSQL = "UPDATE tblnewgroup SET ";
updateSQL += "Groupname=@Groupname, ";
updateSQL += "Slno=@Slno, Subname=@Subname";
updateSQL += "WHERE Groupno=@Groupno_original";

SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(updateSQL, con);


cmd.Parameters.AddWithValue("@Groupname", txtgname.Text);
cmd.Parameters.AddWithValue("@Slno", txtsl.Text);
cmd.Parameters.AddWithValue("@Subname", txtsn.Text);
cmd.Parameters.AddWithValue("@Groupno_original", lstAuthor.SelectedItem.Value);

int updated = 0;
try
{
con.Open();
updated = cmd.ExecuteNonQuery();
lblResults.Text = updated.ToString() + " record updated.";
}
catch (Exception err)
{
lblResults.Text = "Error updating Group. ";
lblResults.Text += err.Message;
}
finally
{
con.Close();
}

if (updated > 0)
{
FillAuthorList();
}

}

最佳答案

Subname=@Subname 后需要一个空格

改变你的

updateSQL += "Slno=@Slno, Subname=@Subname";

updateSQL += "Slno=@Slno, Subname=@Subname ";

作为更好的可视化,你可以像这样使用它;

string updateSQL = @"UPDATE tblnewgroup
SET Groupname=@Groupname, Slno=@Slno, Subname=@Subname
WHERE Groupno=@Groupno_original";

您还应该使用 using阻止处理你的 SqlConnection,例如;

using(SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
updated = cmd.ExecuteNonQuery();
...
}

关于c# - 为什么不更新我的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20490649/

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