首页上一页 1 下一页尾页 1 条记录 1/1页
C#更新数据库数据
发表在C#图书答疑
2016-03-12
是否精华
是
否
版块置顶:
是
否
SqlClass.cn.Close();//cn为sqlconnection实例
dataGridView1.ReadOnly = false;
dataGridView1.CurrentRow.Cells[0].Value = textBox1.Text;
dataGridView1.CurrentRow.Cells[1].Value = textBox2.Text;
dataGridView1.CurrentRow.Cells[3].Value = Int32.Parse(textBox3.Text);
if (radioButton1.Checked == true)
dataGridView1.CurrentRow.Cells[2].Value = "男";
if (radioButton2.Checked == true)
dataGridView1.CurrentRow.Cells[2].Value = "女";
DataTable tb = dataGridView1.DataSource as DataTable;
string sql = "select * from employee";
SqlDataAdapter sda = new SqlDataAdapter(sql, SqlClass.cn);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(tb);
MessageBox.Show("修改成功");
dataGridView1.ReadOnly = true;
SqlClass.cn.Open();
//以上更新方法在直接修改datagridview内数据可用,想更改成利用文本框数据来更改数据库数据时datagridview会更改,但不会更改数据库内数据,且没有异常可catch
dataGridView1.ReadOnly = false;
dataGridView1.CurrentRow.Cells[0].Value = textBox1.Text;
dataGridView1.CurrentRow.Cells[1].Value = textBox2.Text;
dataGridView1.CurrentRow.Cells[3].Value = Int32.Parse(textBox3.Text);
if (radioButton1.Checked == true)
dataGridView1.CurrentRow.Cells[2].Value = "男";
if (radioButton2.Checked == true)
dataGridView1.CurrentRow.Cells[2].Value = "女";
DataTable tb = dataGridView1.DataSource as DataTable;
string sql = "select * from employee";
SqlDataAdapter sda = new SqlDataAdapter(sql, SqlClass.cn);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(tb);
MessageBox.Show("修改成功");
dataGridView1.ReadOnly = true;
SqlClass.cn.Open();
//以上更新方法在直接修改datagridview内数据可用,想更改成利用文本框数据来更改数据库数据时datagridview会更改,但不会更改数据库内数据,且没有异常可catch