我不知道为什么每次我执行更新我的记录,我更新的查询不增加ID从0到1,总是需要0 ..我不知道“知道我如何增加我的ID设置为1,到目前为止..请解释一下..:/ ..我的代码是:更新记录c中的问题#
private void btnUpdate_Click(object sender, EventArgs e)
{
int CustomerID =0;
SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;
Initial Catalog=Gym;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Update Customer set Customer_Name = '" + tbName.Text + "',Cell_Number = '" + tbContactNumber.Text + "',Customer_Address = '" + tbAddress.Text + "' where CustomerID = " + CustomerID, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
BindGridView();
}
private void BindGridView()
{
SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Gym;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * from Customer", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dgView_CustomerInfo.DataSource = dt.DefaultView;
}
您知道UPDATE更新*现有*记录。如果ID是一个IDENTITY列,它只会在您插入记录时增加...... – 2010-12-14 08:48:06
首先,也是最重要的一点,请阅读SQL注入和参数化查询。其次,你在那里做的很腥......你将一个空的DataTable绑定到DataGridView ......两次。这似乎很奇怪。第三,如果你永远不增加它,为什么要增加ID?据我所知,你的意思是表格的Auto-Id,但是'Update'并不影响它。 – Bobby 2010-12-14 08:49:50
ohh ..我想更新现有的记录..我已初始化int CustomerID = 0,但..我没有得到,当用户点击存在于gridview中的现有记录时,如何更改id .. – 2010-12-14 08:55:55