WPF的新手。我想编辑文本框中显示的数据库行的值。目前,我收到一个错误:“ ExecuteNonQuery:Connection属性尚未初始化”。当我删除where子句时,所有行都会更新,而不仅仅是选定的项。
private void btnEDIT_Click(object sender, RoutedEventArgs e)
{
try
{
sc.Open();
cmd = new SqlCommand("Update Rewards set Name = '" + this.txtName.Text + "', Cost= '" + this.txtCost.Text + "'where Name = '" + this.txtName.Text +"'");
cmd.ExecuteNonQuery();
MessageBox.Show("Update Successfull");
sc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
您尚未设置的Connection
属性SqlCommand
。因此,该命令不知道在哪里连接。使用构造函数重载从SqlCommand
,或将其分开这样
cmd.Connection = sc;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句