| 
 
积分40186好友记录主题帖子听众收听 
 | 
 
 发表于 2013-8-22 12:53:23
|
显示全部楼层 
| 
 namespace chaxun3
 {
 public partial class Form2 : Form
 {
 private DataGridViewRow _row;
 public Form2()
 {
 InitializeComponent();
 }
 
 
 
 private void button1_Click(object sender, EventArgs e)
 {
 SqlConnection con = new SqlConnection("server=192.168.1.51;uid=sa;password=sa@123456;Database=StudentSystem");
 string str1 = "select * from M_STUDENT ";
 con.Open();
 SqlDataAdapter adapter = new SqlDataAdapter(str1, con);//讲查询到的数据输出
 
 DataSet DS = new DataSet();//创建临时记录表
 adapter.Fill(DS, "M_STUDENT");//填充数ju
 DataTable mytable = DS.Tables[0];
 
 mytable.Rows[0][0] = textBox1.Text ;
 mytable.Rows[0][1] = textBox2.Text ;
 mytable.Rows[0][2] = textBox3.Text ;
 mytable.Rows[0][3] = textBox4.Text ;
 mytable.Rows[0][4] = textBox5.Text ;
 mytable.Rows[0][5] = textBox6.Text ;
 mytable.Rows[0][6] = textBox7.Text ;
 
 SqlCommand cmd = new SqlCommand("Update  M_STUDENT  ", con);
 
 
 SqlCommandBuilder sb = new SqlCommandBuilder(adapter);
 adapter.Update(mytable);
 MessageBox.Show("保存成功");
 Form1 frm = new Form1();
 frm.ShowDialog();
 this.Close();
 
 con.Close();
 }
 
 
 
 
 
 
 private void Form2_Load(object sender, EventArgs e)
 {
 SqlConnection con = new SqlConnection("server=192.168.1.51;uid=sa;password=sa@123456;Database=StudentSystem");//数据库连接
 con.Open();
 string str = "select * from  M_STUDENT ";  //查询的数据内容
 SqlDataAdapter adapter = new SqlDataAdapter(str, con);//讲查询到的数据输出
 DataSet DS = new DataSet();//创建临时记录表
 adapter.Fill(DS, "M_STUDENT");//填充数ju
 
 
 }
 
 private void button3_Click(object sender, EventArgs e)
 {
 this.Close();
 }
 public void Getrow(DataGridViewRow row)
 {
 this.textBox1.Text = row.Cells["学号"].Value.ToString();
 this.textBox2.Text = row.Cells["姓名"].Value.ToString();
 this.textBox3.Text = row.Cells["性别"].Value.ToString();
 this.textBox4.Text = row.Cells["电话"].Value.ToString();
 this.textBox5.Text = row.Cells["地址"].Value.ToString();
 this.textBox6.Text = row.Cells["生日"].Value.ToString();
 this.textBox7.Text = row.Cells["备注"].Value.ToString();
 
 
 }
 }
 }
 报错:制約 'PK_M_STUDENT' の PRIMARY KEY 違反。オブジェクト 'dbo.M_STUDENT' には重複したキーを挿入できません。
 ステートメントは終了されました。
 提问:编译执行时只能修改更新数据库的第一行数据,改其他数据行的时候就报错了,我该怎么修改呢?
 
 
 你这是什么语言啊看不懂,不过看着像是主键冲突,有可能是学号重复了,不能写入,
 你看看是不是写入的学号已存在了。
 | 
 |