【问题标题】:My primary key is not getting updated我的主键没有更新
【发布时间】: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。如果setwhere 子句都使用file_NameTextBox.Text 作为part 列,您将如何更改该值?如果您检查 ExecuteNonQuery 返回的值,您可能会在尝试更改部件名称时发现它为零,因为没有更新任何行。
  • 好吧,如果您使用SqlCommand Parameters,就很容易找出问题所在。好像您没有更新 Part Number 列。同样正如@HABO 所说,ExecuteNonQuery 不应该与Select 查询一起使用。
  • 拜托,请给我一个代码帮助。最近两天我一直在烦恼这个。我试了这么多天..没办法!!!

标签: c# winforms visual-studio-2010 visual-studio datagridview


【解决方案1】:

首先,使用参数。见Bobby Tables

其次,您的update 语句对setwhere 子句使用相同的输入:

@"update cncinfo set part='" + this.file_NameTextBox.Text + "',...' where part='" + this.file_NameTextBox.Text + "';"

当您只选择已经具有新值的行时,您打算如何更改part 的值?您需要同时使用旧值和新值:

@"update cncinfo set part='" + this.file_NameTextBox.Text + "',...' where part='" + oldPart + "' ;"

最后,ExecuteNonQuery 返回受影响的行数。如果您检查该值,您会发现当您更新一行时它是1,当您尝试更改part 的值时它是0,除非已经有一行具有新的part 值(其中如果您更新该行而不是在重复部分上生成错误)。

您还应该考虑为您的连接和命令使用using statement

【讨论】:

  • 这次我到达了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);,它只换了第一行。
  • 我假设当您告诉它更新与dataGridView1.Rows[0] 对应的数据库行时,您希望它始终更新“第一”行。如果你不是故意的,你不会指定0
  • 用户是选择一行来编辑,还是他们只是输入一些建议的值来删除现有的表?如果他们选择一行,那么您可以将 col[0] 值保存在变量中,例如oldPart,几乎在您将它复制到 file_NameTextBox 的同时,这样您就可以在它更改为其他内容之前知道它是什么。
  • 我也做过dataGridView1.Column[0].Value.ToString() 这也是兄弟.. 它的显示列不包含上下文
  • 您似乎对您尝试使用的工具缺乏一些相当基本的了解:C#、.NET、SQL、调试器,...。恐怕这将是您关于 SO 的第五个问题,它不会导致可接受的答案,因为您期望大量的手来引导您编写应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多