日期:2012-05-16  浏览次数:20541 次

 

#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>