我试图更新我的datagridview。 (编辑)c#使用sqlcommand更新datagridview
我的代码:
private void button1_Click(object sender, EventArgs e)
{
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=tcp:SHEN-PC,49172\\SQLEXPRESS;Initial Catalog=LSEStock;Integrated Security=True";
con.Open();
if (dataGridView1.Rows.Count > 0)
{
int nRowIndex = dataGridView1.Rows.Count-2;
if (dataGridView1.Rows[nRowIndex].Cells[1].Value != null)
{
textBox2.Text = Convert.ToString(dataGridView1.Rows[nRowIndex].Cells[1].Value);
String updateData = "UPDATE CostPrice SET SupplierName = @SupplierName, CostPrice = @CostPrice WHERE PartsID = '" +textBox1.Text+"'";
SqlCommand update = new SqlCommand(updateData, con);
SqlDataAdapter adapter = new SqlDataAdapter(updateData, con);
update.Parameters.Add("@SupplierName", SqlDbType.NVarChar, 50, "SupplierName");
update.Parameters.Add("@CostPrice", SqlDbType.NVarChar, 50, "CostPrice");
adapter.UpdateCommand = update;
//update.ExecuteNonQuery();
if (update != null)
{
update.Dispose();
update = null;
}
}
else
{
MessageBox.Show("NULL");
}
}
con.Close();
}
它说的ExecuteNonQuery未初始化。我的代码有什么问题?
我正在使用SqlCommand进行更新,但是我从互联网上看到几乎每个人都在使用SqlDataAdapter,有什么区别?提前致谢。
如果你有更好的代码,我想从中学习。谢谢!
但后来它为我“附近有语法错误 '' 在。执行查询行 – 2012-08-03 04:47:59