using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
namespace DAL
{
   public class DbConn
    {
        
     ///<summary>
     ///数据库连接类
     ///</summary>
       public static OleDbConnection conn = null;
        public static string connectString = ConfigurationManager.ConnectionStrings["conn"].ToString();
      
        // 构造函数
        public static OleDbConnection getConn()
        {
           return conn = new OleDbConnection(connectString);
           
        }

        //静态返回连接字符串
       public static string GetConnectionString()
       {
           return connectString;
       
       }


        // 获取数据集或数据
        public static DataSet getData(string sqlString)
        {
            OleDbConnection conn = getConn();
            conn.Open();
            OleDbDataAdapter oda = new OleDbDataAdapter(sqlString, conn);
            DataSet ds = new DataSet();
            oda.Fill(ds);

            conn.Close();
            return ds;
        }
       public static DataSet getData(string sqlString,params OleDbParameter[] SqlParams)
       {
           OleDbConnection conn = getConn();
           conn.Open();
           OleDbDataAdapter oda = new OleDbDataAdapter(sqlString, conn);
           DataSet ds = new DataSet();
           if (SqlParams != null)
           { 
             foreach(OleDbParameter p in SqlParams)
             {
                 oda.SelectCommand.Parameters.Add(p);
             }
             oda.Fill(ds); 
           }
           conn.Close();
           return ds;
       }
       public static OleDbDataReader getReader(string sqlString)
       {
           OleDbConnection conn = getConn();
           conn.Open();
           OleDbCommand cmd = new OleDbCommand(sqlString, conn);
           OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
           
           return reader;
       }
       public static OleDbDataReader getReader(string sqlString, params OleDbParameter[] SqlParams)
       {
           OleDbConnection conn = getConn();
           conn.Open();
           OleDbCommand cmd = new OleDbCommand(sqlString, conn);
           OleDbDataReader reader=null;
           if (SqlParams != null)
           {
               foreach (OleDbParameter p in SqlParams)
               {
                   cmd.Parameters.Add(p);
               }
               reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
               
           }
         
           return reader;
           
       }
    
    }
}


//////返回DataReader对象时要注意关闭数据库。。。。。。。。。。。。