DataGridview를 업데이트하려고했습니다. 그것은 ExecuteNonQuery는 초기화되지 않는다는 것을 말한다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();
}
:
내 코드 (편집). 내 코드에 무슨 문제가 있습니까?
저는 SqlCommand를 사용하여 업데이트하고 있습니다. 그러나 인터넷에서 볼 때 거의 모든 사람들이 SqlDataAdapter를 사용하고 있는데, 그 차이점은 무엇입니까? 미리 감사드립니다.
더 좋은 코드를 가지고 있다면, 그 점을 배우고 싶습니다. 감사! SqlCommand.Connection Property
또는이에 :
하지만 그것은에 가까운 "의 구문이 잘못되었습니다 ','나를 보여줍니다. executenonquery line –