就这几行代码,大家帮忙看看。
private void Form1_Load(object sender, System.EventArgs e)
{
da=new SqlDataAdapter("select * from groups",conn);
SqlCommandBuilder cb_g=new SqlCommandBuilder(da); //
ds=new DataSet();
da.Fill(ds,"groups");
Binding binding1=new Binding("Text",ds.Tables[0],ds.Tables[0].Columns["备注"].ColumnName);
this.richTextBox1.DataBindings.Clear();
this.richTextBox1.DataBindings.Add(binding1);
}
//修改richTextBox1的内容,然后点按钮保存。不报错,但数据库并没有更改。
private void button2_Click(object sender, System.EventArgs e)
{
try
{
da.Update(ds,ds.Tables[0].TableName);
}
catch(Exception e1)
{
MessageBox.Show(e1.Message);
return;
}
}
我做了很多测试,都不成功,不能将修改提交到数据库,为什么啊?
private void Form1_Load(object sender, System.EventArgs e)
{
da=new SqlDataAdapter("select * from groups",conn);
SqlCommandBuilder cb_g=new SqlCommandBuilder(da); //
ds=new DataSet();
da.Fill(ds,"groups");
Binding binding1=new Binding("Text",ds.Tables[0],ds.Tables[0].Columns["备注"].ColumnName);
this.richTextBox1.DataBindings.Clear();
this.richTextBox1.DataBindings.Add(binding1);
}
//修改richTextBox1的内容,然后点按钮保存。不报错,但数据库并没有更改。
private void button2_Click(object sender, System.EventArgs e)
{
try
{
da.Update(ds,ds.Tables[0].TableName);
}
catch(Exception e1)
{
MessageBox.Show(e1.Message);
return;
}
}
我做了很多测试,都不成功,不能将修改提交到数据库,为什么啊?
可以调试到ds.Tables[0].Rows[i][j]看看数据是否真的被改变!!
if(ds.HasChanges == false) return;DataTable dt = ds.Tables[0].GetChanges(DataRowState.Modified);
if(dt != null) da.Update(dt);
dt = ds.Tables[0].GetChanges(DataRowState.Added);
if(dt != null) da.Update(dt);
dt = ds.Tables[0].GetChanges(DataRowState.Deleted);
if(dt != null) da.Update(dt);
ds.AcceptChanges();