简单实用的DataSet更新数据库的类+总结(c#)

以前经常用sql语句(update)更新数据库,有使用用起来不是很方便,特别是数据量比较大的情况下(比如数据表)很麻烦~~后来感觉用DataSet更新数据库是不错的选择.于是急着写了一个ataSet更新数据库的类

如下:(后面有使用说明,总结)

using System;

using System.Data;

using System.Data.SqlClient;

using System.Windows.Forms;

namespace winApplication

{

     public class sqlAccess

     {

         //与SQL Server的连接字符串设置

         private string _connString;

         private string _strSql;

 

         private SqlCommandBuilder sqlCmdBuilder;

         private DataSet ds = new DataSet();

         private SqlDataAdapter da;

         public sqlAccess(string connString,string strSql)

         {

              this._connString=connString;

         }

 

         private SqlConnection GetConn()

         {

              try

              {

                   SqlConnection Connection = new SqlConnection(this._connString);

                   Connection.Open();

                   return Connection;

              }

              catch (Exception ex)

              {

                   MessageBox.Show(ex.Message,"数据库连接失败");

                   throw;

              }

         }

 

         //根据输入的SQL语句检索数据库数据

         public DataSet SelectDb(string strSql,string strTableName)

         {

              try

              {

              this._strSql = strSql;

              this.da = new SqlDataAdapter(this._strSql,this.GetConn());

              this.ds.Clear();

              this.da.Fill(ds,strTableName);

              return ds;//返回填充了数据的DataSet,其中数据表以strTableName给出的字符串命名

              }

              catch (Exception ex)

              {

                   MessageBox.Show(ex.Message,"数据库操作失败");

                   throw;

              }

         }

 

         //数据库数据更新(传DataSet和DataTable的对象)

         public DataSet UpdateDs(DataSet changedDs,string tableName)

         {

              try

              {

              this.da = new SqlDataAdapter(this._strSql,this.GetConn());

              this.sqlCmdBuilder = new SqlCommandBuilder(da);

              this.da.Update(changedDs,tableName);

              changedDs.AcceptChanges();

              return changedDs;//返回更新了的数据库表

              }

              catch (Exception ex)

              {

                   MessageBox.Show(ex.Message,"数据库更新失败");

                   throw;

              }

                    }

使用说明总结

1. GetConn方法创建一个数据库连接,返回SqlConnection。

2.使用的select命令中必须包含主键,这点大家都知道的!

3. this.da.Fill(ds,strTableName) 填充数据集

4.构造CommandBuilder对象时,DataAdapter对象作为构造函数参数传入:

  this.sqlCmdBuilder = new SqlCommandBuilder(da);

5. 在调用UpdateDs()更新数据库前,请检查changedDs是否已经被更新过,用changedDs.[tableName] GetChanges() != null;

6.this.da.Update(changedDs,tableName)方法更新数据,然后调用changedDs.AcceptChanges()才能真正的更新数据库,调用 changedDs.RejectChanges() 取消更新。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值