读书人

c# datagridview 使用SqlCommandBui

发布时间: 2013-07-01 12:33:04 作者: rapoo

c# datagridview 使用SqlCommandBuilder更新数据 怎么不能更新到数据库中去 怎么解决
删除使用

SqlCommandBuilder
还行 修改数据和增加一行新的数据就无法更新到数据库
//删除

try
{
ds.Tables[0].Rows[dataGridView1.CurrentRow.Index].Delete();
sdr.Update(ds.Tables[0].GetChanges());
MessageBox.Show("数据删除成功");
ds.Tables[0].AcceptChanges();
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
//修改
try
{

// sdr.Update(ds.Tables["tb_course"].GetChanges());
sdr.Update(ds.Tables[0]);
MessageBox.Show("数据修改成功");
//ds.Tables["tb_course"].AcceptChanges();
ds.Tables[0].AcceptChanges();
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
// 修改数据时可以在界面上修改 但无法更新到数据库 该怎么解决
C# DataGridView 数据库
[解决办法]
你必须使用SqlDataAdapter这个类的update方法来更新数据库中的数据,具体实现可以参考下面文章的代码:
http://www.cnblogs.com/bobo41/archive/2013/05/24/3096467.html
[解决办法]
表是否有主键。

读书人网 >C#

热点推荐