executenonquery是我的问题,此代码适用于不同datagridview
中的其他按钮这是我在删除按钮的代码
private void button4_Click_2(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=XXYZZ\SQLEXPRESS;Initial Catalog=rick_inventiory;Integrated Security=True");
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Delete from tbl_Orders where CustomersID2 = '" + dataGridView5.SelectedRows[0].Cells[0].Value.ToString() + "'";
con.Open();
cmd.Parameters.AddWithValue("@CustomerID2", txtCustomerID2.Text);
cmd.ExecuteNonQuery();
con.Close();
disp_data();
MessageBox.Show("Deleted Successfully");
}
更新代码仍然执行sa代码但没有更新它 并且我的代码是更新按钮
SqlConnection con = new SqlConnection(@"Data Source=XXYZZ\SQLEXPRESS;Initial Catalog=rick_inventiory;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Update tbl_Products SET ProductName='" + txtProName.Text +
"',Stocks='" + txtStocks.Text + "',Price='" + txtPrice.Text + "',Description='" +
txtDesc.Text + "',CategoryName='" + txtCat.Text + "' where ProductID ='" + txtProID.Text + "';";
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter("Select * from tbl_Products", con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
MessageBox.Show("Successfuly Updated");
con.close();
答案 0 :(得分:1)
在更新中,存在语法问题,删除更新查询的内侧半冒号
在删除时,您想要更改行
这
cmd.Parameters.AddWithValue(“@ CustomerID2”,txtCustomerID2.Text);
到
cmd.Parameters.AddWithValue(“@ CustomerID2”,'“+ dataGridView5.SelectedRows [0] .Cells [0] .Value.ToString()+”');