using System.Configuration;
using System.Data.OleDb ;
using System.Data .SqlClient ;
using System.Data ;
///<add key="msolepath" value="Provider=SQLOLEDB.1;DataSource=(local);Initial Catalog=XXX;uid=zm;pwd=zm" />
///<add key="mssqlpath" value="Server=(local);DataBase=XXX;Pwd=zm;Uid=zm;Max Pool Size=300;Min Pool Size=0;Connection Lifetime=300;packet size=4096" />

public string mssqlpath = ConfigurationSettings.AppSettings["mssqlpath"];   //MSSQL数据库路径
public string msolepath = ConfigurationSettings.AppSettings["msolepath"];

// OLEDB 数据库连接字符串
  private OleDbConnection mdbconn;
  // SQL 数据库连接字符串
  private SqlConnection sqlconn;

  private DataSet ds=null;
   
  private SqlDataAdapter Adpt=null;

  private SqlDataReader rd=null;
  
  private SqlCommand command=null;
 
  public OleDbConnection getOleDb()
  {
   mdbconn = new OleDbConnection(msolepath);
   mdbconn.Open();
   return mdbconn;
  }

  public SqlConnection getSqlDb()
  {
   sqlconn = new SqlConnection(mssqlpath);
   sqlconn.Open();
   return sqlconn;
  }

  public void sqlClose()
  {
   if (sqlconn != null)  
    try
    {
     sqlconn.Close() ;
     sqlconn.Dispose() ;
    }     
    catch (SqlException e)
    {throw new Exception(e.Message);}
  }

  public void dleClose()
  {
   if (mdbconn != null)  
    try
    {
     mdbconn.Close() ;
     mdbconn.Dispose() ;
    }     
    catch (SqlException e)
    {throw new Exception(e.Message);}
  }

  public DataSet getDs(string sql)
  {
   try
   {
    sqlconn=this.getSqlDb() ;
    Adpt = new SqlDataAdapter(sql, sqlconn);
    ds=new DataSet ();
    Adpt.Fill(ds);
   }
   catch (SqlException e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    if (sqlconn != null)  
     try
     {
      sqlconn.Close() ;
      sqlconn.Dispose() ;
     }     
     catch (SqlException e)
     {
      throw new Exception(e.Message);
     }
    if (Adpt != null)  
     try
     {
      Adpt.Dispose ();
     }     
     catch (SqlException e)
     {
      throw new Exception(e.Message);
     }
   }
   return ds;
  }
       
  public bool isEmpty(string sql)
  {
   bool flag=false;
   try
   {
    sqlconn=this.getSqlDb() ;
    command = new SqlCommand(sql, sqlconn);
    rd = command.ExecuteReader() ;
    if(!rd.Read())
    {
     flag=true;
    }
   }
   catch (SqlException e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    
    if (rd != null) 
     try {rd.Close();}
     catch (SqlException e) {throw new Exception(e.Message);}
    if (command != null) 
     try {command.Dispose();}
     catch (SqlException e) {throw new Exception(e.Message);}
    if (sqlconn != null)  
     try
     {
      sqlconn.Close() ;
      sqlconn.Dispose() ;
     }     
     catch (SqlException e)
     {throw new Exception(e.Message);}
   }
   return flag;

  }
       
  public bool UpdateData(string sql)
  {
   
   bool flag=false;
   try
   {
    sqlconn=this.getSqlDb() ;
    command = new SqlCommand(sql, sqlconn);
    command.ExecuteNonQuery();
    flag=true;
   }
   catch (SqlException e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    if (command != null) 
     try {command.Dispose();}
     catch (SqlException e) {throw new Exception(e.Message);}
    if (sqlconn != null)  
     try
     {
      sqlconn.Close() ;
      sqlconn.Dispose() ;
     }     
     catch (SqlException e)
     {throw new Exception(e.Message);}
   }
   return flag;
  }

  public SqlDataReader getReader(string sql)
  {
   try
   {
    sqlconn=this.getSqlDb() ;
    command = new SqlCommand(sql, sqlconn);
    rd = command.ExecuteReader() ; 
   }
   catch (SqlException e)
   {
    throw new Exception(e.Message);
   }
   finally
   {
    if (command != null) 
     try {command.Dispose();}
     catch (SqlException e) {throw new Exception(e.Message);}
    /*if (sqlconn != null)  
     try
     {
      sqlconn.Close() ;
      sqlconn.Dispose() ;
     }     
     catch (SqlException e)
     {throw new Exception(e.Message);}*/
   }
   return rd;
  }
 }