日期:2013-06-07  浏览次数:20339 次

以前经常用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);