ADO.net如何对没有DataAdapter的DataSet的更新

对于ADO.net接触不久,在项目中使用了DAAB。在数据访问层向逻辑层传递时,传递的是用DAAB中的ExecuteDataSet方法得到的DataSet,然后将DataSet和界面中的DataGrid绑定。在数据更新的时候,我只有一个DataSet,没有产生这个DataSet的DataAdapter。数据如何更新呢?在MSDN中写到:

private void UpdateDataSet(DataSet myDataSet){
   // Check for changes with the HasChanges method first.
   if(!myDataSet.HasChanges(DataRowState.Modified)) return;
   // Create temporary DataSet variable.
   DataSet xDataSet;
   // GetChanges for modified rows only.
   xDataSet = myDataSet.GetChanges(DataRowState.Modified);
   // Check the DataSet for errors.
   if(xDataSet.HasErrors){
      // Insert code to resolve errors.
   }
   // After fixing errors, update the data source with the DataAdapter
   // used to create the DataSet.
   myOleDbDataAdapter.Update(xDataSet);
}
但是问题是我这边没有myOleDbDataAdapter。一种方法是用SQL语句进行更新update更新。但是我的DataGrid的字段太长了,如果写这样的语句我没有勇气写下去。最后终于在同事的帮助下解决了这个问题。就是为了更新DataSet,再生成一个DataAdapter。用这个DataAdapter作为通道,进行DataSet更新,代码如下

private void UpdateDataSet(DataSet myDataSet)

{

 // Check for changes with the HasChanges method first.
   if(!myDataSet.HasChanges(DataRowState.Modified)) return;

    sql="myDataSet所用的SQL语句";

    OleDbConnection cn=new OleDbConnection(连接字符串);
    OleDbDataAdapter da=new OleDbDataAdapter(sql,cn);
    OleDbCommandBuilder cb = new OleDbCommandBuilder(da);

    // Create temporary DataSet variable.
    DataSet xDataSet;
    // GetChanges for modified rows only.   

    xDataSet = myDataSet.GetChanges(DataRowState.Modified);
    // Check the DataSet for errors.
     if(xDataSet.HasErrors){
        // Insert code to resolve errors.
      }
   // After fixing errors, update the data source with the DataAdapter
   // used to create the DataSet.
    da.Update(xDataSet);

    myDataSet.AcceptChanges();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值