0
我将一个值从form1传递给form2,并将该值用作where条件,但是我似乎无法修复它。我正在更新一张桌子btw。任何帮助将不胜感激。更新查询和连接属性尚未初始化
SqlConnection cn = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=inventory2;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adptr = new SqlDataAdapter();
DataSet dt = new DataSet();
private void button1_Click(object sender, EventArgs e)
{
if (this.Text == "EDIT")
{
cmd.CommandText = string.Format("Update Items Set (Barcode='" + txtcode.Text + "' ,Productname= '" + txtitemname.Text + "',Prices= '" + txtPrices.Text + "' ,Quantity= '" + txtquantity.Text + "' ,Brand= '" + txtbrand.Text + "',Expiry= '" + txtexpiry.Text + "',Description='" + txtdescription.Text + "' ,Critical= '" + txtcritical.Text + "' where Barcode = '" + txtTry.Text + "')", cn);
cmd.ExecuteNonQuery();
MessageBox.Show("Records Updated!");
txtcode.Text = "";
txtitemname.Text = "";
txtPrices.Text = "";
txtquantity.Text = "";
txtbrand.Text = "";
txtexpiry.Text = "";
txtdescription.Text = "";
txtcritical.Text = "";
}
else
{
MessageBox.Show("Invalid");
}
您将连接传递给String.Format调用,而不是将它分配给命令对象。 **请**使用SqlParameters而不是字符串连接。 – stuartd
是的,先生使用参数。我已经添加了“cmd.Connection = cn;”现在新的错误是“System.Data.SqlClient.SqlException发生在System.Data.dll 类型'的未处理的异常'其他信息:在执行查询行上'('。''附近的语法不正确 – FutureDev
您得到该错误,因为你的SQL是无效的:'set'组不应该有括号:'更新项目设置条形码='... – stuartd