搜集的很不错的数据访问类(sql server 数据库)

好久没添加新东西了,放假了,好久不用的.net也快忘了,赶紧回顾一下吧
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace zbeinfo
{
 /// <summary>
 /// DataAccess 的摘要说明。
 /// </summary>
 public class DataAccess
 {

     #region 定义
   protected static SqlConnection conn=new SqlConnection();
  protected static SqlCommand    comm=new SqlCommand();
  #endregion
  public DataAccess()
  {
   //init();
  }

  #region 打开数据库
  private static void openConnection()
  {
   if(conn.State==ConnectionState.Closed)
   {
    conn.ConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
    comm.Connection=conn;
    ////    try
    //    {
    conn.Open();
    //    }
    //    catch(Exception e)
    //    {throw new Exception(e.Message);}
   }
  }
  #endregion
  #region 关闭数据库
  private static void closeConnection()
  {
   if(conn.State==ConnectionState.Open)
   {
    conn.Close();
    conn.Dispose();
    comm.Dispose();
   }
  }
  #endregion
  #region 执行Sql查询语句
  /// <summary>
  /// 执行Sql查询语句
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  public static void ExecuteSql(string sqlstr)
  {
//   try
//   {
    openConnection();
    comm.CommandType=CommandType.Text;
    comm.CommandText=sqlstr;
    comm.ExecuteNonQuery();
//   }
//   catch(Exception e)
//   {
//    throw new Exception(e.Message);
//   }
//   finally
//   {
//    closeConnection();
//   }

  }
  #endregion
  #region 执行存储过程
  /// <summary>
  /// 执行存储过程
  /// </summary>
  /// <param name="procName">存储过程名</param>
  /// <param name="coll">SqlParameters 集合</param>
  public static void ExecutePorcedure(string procName,SqlParameter[] coll)
  {
   try
   {
    openConnection();
    for(int i=0;i<coll.Length;i++)
    {
     comm.Parameters .Add(coll[i]);
    }
    comm.CommandType=CommandType.StoredProcedure ;
    comm.CommandText =procName;
    comm.ExecuteNonQuery();
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    comm.Parameters.Clear();
    closeConnection();
   }
  }

  #endregion
  #region 执行存储过程并返回数据集
  /// <summary>
  /// 执行存储过程并返回数据集
  /// </summary>
  /// <param name="procName">存储过程名称</param>
  /// <param name="coll">SqlParameter集合</param>
  /// <param name="ds">DataSet </param>
  public static void ExecutePorcedure(string procName,SqlParameter[] coll,ref DataSet ds)
  {
   try
   {
    SqlDataAdapter da=new SqlDataAdapter();
    openConnection();
    for(int i=0;i<coll.Length;i++)
    {
     comm.Parameters .Add(coll[i]);
    }
    comm.CommandType=CommandType.StoredProcedure ;
    comm.CommandText =procName;
   
    da.SelectCommand =comm;
    da.Fill(ds);
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    comm.Parameters.Clear();
    closeConnection();
   }
  }

  #endregion
  #region 执行Sql查询语句并返回第一行的第一条记录,返回值为object 使用时需要拆箱操作 -> Unbox
  /// <summary>
  /// 执行Sql查询语句并返回第一行的第一条记录,返回值为object 使用时需要拆箱操作 -> Unbox
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <returns>object 返回值 </returns>
  public static object ExecuteScalar(string sqlstr)
  {
   object obj=new object();
   try
   {
    openConnection();
    comm.CommandType =CommandType.Text ;
    comm.CommandText =sqlstr;
    obj=comm.ExecuteScalar();
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    closeConnection();
   }
   return obj;
  }

  #endregion
  #region 执行Sql查询语句,同时进行事务处理
  /// <summary>
  /// 执行Sql查询语句,同时进行事务处理
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  public static void ExecuteSqlWithTransaction(string sqlstr)
  {
   SqlTransaction trans ;
   trans=conn.BeginTransaction();
   comm.Transaction =trans;
   try
   {
    openConnection();
    comm.CommandType =CommandType.Text ;
    comm.CommandText =sqlstr;
    comm.ExecuteNonQuery();
    trans.Commit();
   }
   catch
   {
    trans.Rollback();
   }
   finally
   {
    closeConnection();
   }
  }

  #endregion
  #region 返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接方法关闭数据库连接
  /// <summary>
  /// 返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
  /// 方法关闭数据库连接
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <returns>SqlDataReader对象</returns>
  public static SqlDataReader dataReader(string sqlstr)
  {
   SqlDataReader dr=null;
      try
      {
   openConnection();
   comm.CommandText =sqlstr;
   comm.CommandType =CommandType.Text ;
   dr=comm.ExecuteReader(CommandBehavior.CloseConnection);
      }
      catch
      {
       try
       {
        dr.Close();
        closeConnection();
       }
       catch
       {
       }
      }
   return dr;
  }

  #endregion
  #region (ref)返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接方法关闭数据库连接
  /// <summary>
  /// 返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
  /// 方法关闭数据库连接
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <param name="dr">传入的ref DataReader 对象</param>
  public static void dataReader(string sqlstr,ref SqlDataReader dr)
  {
   try
   {
    openConnection();
    comm.CommandText =sqlstr;
    comm.CommandType =CommandType.Text ;
    dr=comm.ExecuteReader(CommandBehavior.CloseConnection);
   }
   catch
   {
    try
    {
     if(dr!=null && !dr.IsClosed)
      dr.Close();
    }
    catch
    {
    }
    finally
    {
     closeConnection();
    }
   }
  }
  #endregion
  #region 返回指定Sql语句的DataSet
  /// <summary>
  /// 返回指定Sql语句的DataSet
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <returns>DataSet</returns>
  public static DataSet dataSet(string sqlstr)
  {
   DataSet ds= new DataSet();
   SqlDataAdapter da=new SqlDataAdapter();
   try
   {
    openConnection();
    comm.CommandType =CommandType.Text ;
    comm.CommandText =sqlstr;
    da.SelectCommand =comm;
    da.Fill(ds);
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    closeConnection();
   }
   return ds;
  }

  #endregion
  #region (ref)返回指定Sql语句的DataSet
  /// <summary>
  /// 返回指定Sql语句的DataSet
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <param name="ds">传入的引用DataSet对象</param>
  public static void dataSet(string sqlstr,ref DataSet ds)
  {
   SqlDataAdapter da=new SqlDataAdapter();
   try
   {
    openConnection();
    comm.CommandType =CommandType.Text ;
    comm.CommandText =sqlstr;
    da.SelectCommand =comm;
    da.Fill(ds);
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    closeConnection();
   }
  }

  #endregion
  #region 返回指定Sql语句的DataTable
  /// <summary>
  /// 返回指定Sql语句的DataTable
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <returns>DataTable</returns>
  public static DataTable dataTable(string sqlstr)
  {
   SqlDataAdapter da=new SqlDataAdapter();
   DataTable datatable=new DataTable();
   try
   {
    openConnection();
    comm.CommandType =CommandType.Text ;
    comm.CommandText =sqlstr;
    da.SelectCommand =comm;
    da.Fill(datatable);
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    closeConnection();
   }
   return datatable;
  }

  #endregion
  #region 执行指定Sql语句,同时给传入DataTable进行赋值
  /// <summary>
  /// 执行指定Sql语句,同时给传入DataTable进行赋值
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <param name="dt">ref DataTable dt </param>
  public static void dataTable(string sqlstr,ref DataTable dt)
  {
   SqlDataAdapter da=new SqlDataAdapter();
   try
   {
    openConnection();
    comm.CommandType =CommandType.Text ;
    comm.CommandText =sqlstr;
    da.SelectCommand =comm;
    da.Fill(dt);
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    closeConnection();
   }
  }

  #endregion
  #region 执行带参数存储过程并返回数据集合
  /// <summary>
  /// 执行带参数存储过程并返回数据集合
  /// </summary>
  /// <param name="procName">存储过程名称</param>
  /// <param name="parameters">SqlParameterCollection 输入参数</param>
  /// <returns></returns>
  public static DataTable dataTable(string procName,SqlParameterCollection parameters)
  {
   SqlDataAdapter da=new SqlDataAdapter();
   DataTable datatable=new DataTable();
   try
   {
    openConnection();
    comm.Parameters.Clear();
    comm.CommandType=CommandType.StoredProcedure ;
    comm.CommandText =procName;
    foreach(SqlParameter para in parameters)
    {
     SqlParameter p=(SqlParameter)para;
     comm.Parameters.Add(p);
    }
    da.SelectCommand =comm;
    da.Fill(datatable);
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    closeConnection();
   }
   return datatable;
  }

  #endregion
  #region 返回指定sql语句的 DataView
  public static DataView dataView(string sqlstr)
  {
   SqlDataAdapter da=new SqlDataAdapter();
   DataView dv=new DataView();
   DataSet ds=new DataSet();
   try
   {
    openConnection();
    comm.CommandType=CommandType.Text;
    comm.CommandText =sqlstr;
    da.SelectCommand =comm;
    da.Fill(ds);
    dv=ds.Tables[0].DefaultView;
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    closeConnection();
   }
   return dv;
  }

  #endregion
  }
 }

 

posted @ 2005-01-20 10:54  火火  阅读(937)  评论(2编辑  收藏  举报