【发布时间】:2014-10-19 05:05:09
【问题描述】:
当我尝试从文本框中更新我的主键时,它没有得到更新。其余列都更新得很好。请帮我。
我没有收到任何错误。什么都没有..当我尝试更新零件号时..它没有更新..只是回到以前的值。
我将值更新到数据库的代码:
private void Update_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=SREEJITHMOHA492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + this.file_NameTextBox.Text
+ "',drawings='" + this.drawingsTextBox.Text + "',draftpath='"
+ this.gcodeTextBox.Text + "',comments='" + this.commentsTextBox.Text
+ "' where part='" + dataGridView1.Rows[0].Cells[0].Value.ToString() + "' ;", con);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void load_table()
{
SqlConnection con = new SqlConnection(@"Data Source=SREEJITHMOHA492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select part as 'Part Number',drawings as 'Drawings',draftpath as 'G-Code Path',releasepath as 'Release Path',comments as 'Comments' from cncinfo ;", con);
cmd.ExecuteNonQuery();
con.Close();
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
dbddataset = new DataTable();
sda.Fill(dbddataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbddataset;
dataGridView1.DataSource = bSource;
sda.Update(dbddataset);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
提前致谢
【问题讨论】:
-
Bobby Tables。如果
set和where子句都使用file_NameTextBox.Text作为part列,您将如何更改该值?如果您检查ExecuteNonQuery返回的值,您可能会在尝试更改部件名称时发现它为零,因为没有更新任何行。 -
好吧,如果您使用
SqlCommand Parameters,就很容易找出问题所在。好像您没有更新Part Number列。同样正如@HABO 所说,ExecuteNonQuery不应该与Select查询一起使用。 -
拜托,请给我一个代码帮助。最近两天我一直在烦恼这个。我试了这么多天..没办法!!!
标签: c# winforms visual-studio-2010 visual-studio datagridview