用Dataset操作数据源的 类 (总结)

using System;
using System.Data;
using System.Data.OleDb ;
namespace Tools
{
 ///


 /// Summary description for DataBaseCls.
 ///

 public class DataBaseCls

 {

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

  private string _connString;

  private string _strSql;


 
  private OleDbCommandBuilder CmdBuilder;

  private DataSet ds = new DataSet();

  private OleDbDataAdapter da;

  public DataBaseCls(string connString)

  {

   this._connString=connString;
   
  }


 
  private OleDbConnection GetConn()

  {

   try

   {

    OleDbConnection Connection = new OleDbConnection(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 OleDbDataAdapter(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 OleDbDataAdapter(this._strSql,this.GetConn());

    this.CmdBuilder = new OleDbCommandBuilder(da);

    this.da.Update(changedDs,tableName);

    changedDs.AcceptChanges();

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

   }

   catch (Exception ex)

   {

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

    throw;

   }

  }
 }
}
 

你可能感兴趣的:(C#.NET)