当前位置:数据库 > Access >>

中小系统.net DataAccess数据访问类

 

#region 引用对象
using System;
using System.Xml ;
using System.Data;
using System.Data.SqlClient;
using System.Web; 
#endregion
namespace SysClassLibrary
{
 /// <summary>
 /// DataAccess 的摘要说明。
 /// <author>wuchen</author>
 /// <date>2004-4-12</date>
 /// <email>scwuchen@263.net</email>
 /// <description>数据处理基类,调用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,ref DataSet ds); </description>
 /// </summary>
 public class DataAccess
 {

 #region  属性
   
  /// <summary>
  /// 是否必须关闭数据库连接
  /// </summary>
  public static bool mustCloseConnection
  {
   get
   {
    return _mustCloseConnection;
   }
   set
   {
    _mustCloseConnection=value;
   }
  }
  /// <summary>
  /// 连接字符串
  /// </summary>
  public static string connectionString
  {
   get
   {
    if(_connectionString ==string.Empty)
     return SysConfig.ConnectionString ;
    else
     return _connectionString;
   }
   set
   {
    _connectionString =value;
   }
  }
  /// <summary>
  /// 是否关闭数据库连接
  /// </summary>
  private static bool _mustCloseConnection = true; 
  private static string _connectionString =string.Empty ; 

  #endregion
 

  #region 类构造函数
  
  /// <summary>
  /// 构造函数
  /// </summary>
  public DataAccess()
  {
  }

  /// <summary>
  /// 析构函数,释放相应的对象
  /// </summary>
  ~DataAccess()
  {
  }

  #endregion

     #region  Method
  
  /// <summary>
  /// 执行Sql查询语句
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  ///<returns >i </returns>
  public static int ExecuteSql(string sqlstr){
   int i=0;
   using (SqlConnection conn =new SqlConnection(connectionString))
   {
    SqlCommand comm=new SqlCommand();
    comm.Connection =conn;
    comm.CommandType =CommandType.Text ;
    comm.CommandText =sqlstr;
    try
    {
     conn.Open();
     i=comm.ExecuteNonQuery();
    }
    catch(SqlException e)
    {
     new ErrorLog().SaveDataAccessError(e);
    }
    finally
    {
     conn.Close();
     comm.Dispose();
    }
   }
   return i;
  }

  /// <summary>
  /// 执行存储过程
  /// </summary>
  /// <param name="procName">存储过程名</param>
  /// <param name="coll">SqlParameters 集合</param>
  public static void ExecutePorcedure(string procName,SqlParameter[] coll)
  {
   using (SqlConnection conn =new SqlConnection(connectionString))
   {
    SqlCommand comm=new SqlCommand();
    comm.Connection =conn;
    comm.CommandType =CommandType.StoredProcedure ;
    ExecutePorcedure(procName,coll,conn,comm);
   }
  }

  public static void ExecutePorcedure(string procName,SqlParameter[] coll,ref DataSet ds)
  {
   using (SqlConnection conn =new SqlConnection(connectionString))
   {
    SqlCommand comm=new SqlCommand();
    comm.Connection =conn;
    comm.CommandType =CommandType.StoredProcedure ;
    ExecutePorcedure(procName,coll,conn,comm,ref ds);
   }
  }

  /// <summary>
  /// 执行存储过程类
  /// </summary>
  /// <param name="procName"></param>
  /// <param name="coll"></param>
  /// <param name="conn"></param>
  /// <param name="comm"></param>
  public static void ExecutePorcedure(string procName,SqlParameter[] coll,SqlConnection conn,SqlCommand comm)
  {
   if(procName ==null || procName=="")
    throw new SqlNullException();
   try
   {
    conn.Open(); 
    for(int i=0;i<coll.Length;i++)
    {
     comm.Parameters .Add(coll[i]);
    }
    comm.CommandType=CommandType.StoredProcedure ;
    comm.CommandText =procName;
    comm.ExecuteNonQuery();
   }
   catch(SqlException e)
   {
    new ErrorLog().SaveDataAccessError(e);
   }
   finally
   {
    comm.Parameters.Clear();
    conn.Close();
    comm.Dispose();
   }
  }

  public static void ExecutePorcedure(string procName,SqlParameter[] coll,SqlConnection conn,SqlCommand comm,ref DataSet ds)
  {
   if(procName ==null || procName=="")
    throw new SqlNullException();
   try
   {
    SqlDataAdapter da =new SqlDataAdapter();
    conn.Open(); 
    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(SqlException e)
   {
    new ErrorLog().SaveDataAccessError(e);
   }
   finally
   {
    comm.Parameters.Clear();
    conn.Close();
    comm.Dispose();
   }
  }


  /// <summary

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,