ccyhbzgm 发表于 2016-6-11 14:59:09

C#通过数据库来修改信息,出现违反了 PRIMARY KEY 约束

RT 错误信息如下:


违反了 PRIMARY KEY 约束 'PK_ruku_1'。不能在对象 'dbo.ruku_1' 中插入重复键。
语句已终止。


源码:
private void tushuxiugai_Load(object sender, EventArgs e)
      {
            // TODO: 这行代码将数据加载到表“yonghu1DataSet2.ruku_1”中。您可以根据需要移动或删除它。
            this.ruku_1TableAdapter.Fill(this.yonghu1DataSet2.ruku_1);

      }

      private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
      {
            int index = this.dataGridView1.CurrentCell.RowIndex;
            this.textBox1.Text = this.dataGridView1.Rows.Cells.Value.ToString();
            this.textBox2.Text = this.dataGridView1.Rows.Cells.Value.ToString();
            this.textBox3.Text = this.dataGridView1.Rows.Cells.Value.ToString();
            this.textBox4.Text = this.dataGridView1.Rows.Cells.Value.ToString();
            this.textBox5.Text = this.dataGridView1.Rows.Cells.Value.ToString();
      }

      private void button1_Click(object sender, EventArgs e)
      {
            lianjiecs conn = new lianjiecs();
            SqlConnection myconn = conn.GetConnection();
            SqlCommand mycomm = myconn.CreateCommand();
            myconn.Open();
            if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "" && textBox4.Text == "" && textBox5.Text == "")
            {
                MessageBox.Show("修改信息不能为空!");
            }
            else
            {
                  mycomm.CommandText = "update ruku_1 set bianhao='" + textBox1.Text + "', shuming = '" + textBox2.Text + "',zuozhe = '" + textBox3.Text + "',rikushujian ='" + textBox4.Text + "',jiage='" + textBox5.Text + "'";
                  mycomm.ExecuteNonQuery();
                  mycomm.Dispose();
                  myconn.Close();
                  MessageBox.Show("更改成功!");
                  textBox5.Text = "";
                  textBox4.Text = "";
                  textBox3.Text = "";
                  textBox2.Text = "";
                  textBox1.Text = "";
            }

      }
            
               
      

      private void button2_Click(object sender, EventArgs e)
      {
            this.Close();
            main a = new main();
            a.Show();
      }

数据库信息:



求解 这种问题该怎么解决。。。
还有如果需要添加一个删除信息的数据库窗体又该怎么写。。。。
求大神。。求大牛。。。 求大腿缺挂件的。。。。。

ccyhbzgm 发表于 2016-6-11 15:01:20

在线等。。。。{:9_240:}

/aiq____害__虫/ 发表于 2016-6-15 08:26:20

。。大哥,你到是加个where限制下啊,这样不是所有条都改了么,肯定报主键冲突啊。。加个where bianbao = '';

/aiq____害__虫/ 发表于 2016-6-15 08:27:12

= '';里面要有值
页: [1]
查看完整版本: C#通过数据库来修改信息,出现违反了 PRIMARY KEY 约束