数据库类(Access专用) 未测试

using  System;
using  System.Data;
using  System.Configuration;
using  System.Data.OleDb;


///  
///  通用数据库类(Access专用)未测试
///   ZhengFei
///   2.0版
///   2007-5-10
///  

namespace  AccessDataBase
{
    
public   class  AccDataBase
    {
        
string  _ConnStr  =   "" ;
        
///  
        
///  不带参数的构造函数(默认取web.config中的连接字符串)
        
///  

         public  AccDataBase()
        {
            _ConnStr 
=  ConfigurationManager.ConnectionStrings[ " AccConn " ].ToString();
        }
        
///  
        
///  构造函数(重载,带连接字符串)
        
///  

        
///   数据库连接字符串
         public  AccDataBase( string  connstr)
        {
            
try
            {
                
this ._ConnStr  =  connstr;
            }
            
catch  (Exception ep)
            {
                
throw  ep;
            }
        }
        
///  
        
///  数据库链接字符串
        
///  

         public   string  ConnStr
        {
            
get
            {
                
return  _ConnStr;
            }
            
set
            {
                _ConnStr 
=  value;
            }
        }
        
///  
        
///  获得一个已连接数据库的OleDbConnection对象
        
///  

        
///   返回一个已连接数据库的OleDbConnection对象
         public  OleDbConnection GetConn()
        {
            OleDbConnection Conn 
=   new  OleDbConnection(_ConnStr);
            
try
            {
                Conn.Open();
            }
            
catch
            {
                
throw ;
            }
            
return  Conn;
        }
        
///  
        
///  释放资源,并回收垃圾
        
///  

        
///  
         public   void  Dispose(OleDbConnection Conn)
        {
            
if  (Conn  !=   null )
            {
                Conn.Close();
                Conn.Dispose();
            }
            GC.Collect();
        }
        
///  
        
///  生成Command对象
        
///  

        
///   SQL语句
        
///   OledbConnection对象
        
///  
         public  OleDbCommand CreateCommand( string  SQL, OleDbConnection Conn)
        {
            
if  (Conn.State  !=  ConnectionState.Open)
            {
                
try
                {
                    Conn.Open();

                }
                
catch
                {
                    
throw ;
                }
            }
            OleDbCommand Cmd 
=   new  OleDbCommand(SQL, Conn);
            
return  Cmd;
        }

        
///  
        
///  返回adapter对象
        
///  

        
///   SQL语句
        
///   SqlConnection连接对象
        
///   返回adapter对象
         public  OleDbDataAdapter CreateDataAdapter( string  SQL, OleDbConnection Conn)
        {
            
if  (Conn.State  !=  ConnectionState.Open)
            {
                
try
                {
                    Conn.Open();
                }
                
catch
                {
                    
throw ;
                }
            }
            OleDbDataAdapter Da 
=   new  OleDbDataAdapter(SQL, Conn);
            
return  Da;
        }
        
public  OleDbDataAdapter CreateDataAdapter(OleDbCommand cmd)
        {
            OleDbDataAdapter Da 
=   new  OleDbDataAdapter(cmd);
            
return  Da;
        }
        
///  
        
///  验证是否有数据
        
///  

        
///   SQL语句
        
///  
         public   bool  ExistDate( string  SQL)
        {
            OleDbConnection Conn 
=   this .GetConn();
            OleDbDataReader dr;
            dr 
=   this .CreateCommand(SQL, Conn).ExecuteReader();
            
if  (dr.Read())
            {
                Dispose(Conn);
                
return   true ;
            }
            
else
            {
                Dispose(Conn);
                
return   false ;
            }
        }
        
///  
        
///  返回一个带参数的OledbCommand对象
        
///  

        
///   SQL语句
        
///   OLEDBConnection对象
        
///   OleDbParameter数组
        
///  
         public  OleDbCommand CreateCommand( string  SQL, OleDbConnection Conn, OleDbParameter[] sp)
        {
            
try
            {
                
if  (Conn.State  !=  ConnectionState.Open)
                {
                    Conn.Open();
                }
            }
            
catch
            {
                
throw ;
            }
            OleDbCommand Cmd 
=   new  OleDbCommand(SQL, Conn);
            
if  (sp  !=   null )
            {
                Cmd.Parameters.Clear();
                
foreach  (OleDbParameter s  in  sp)
                    Cmd.Parameters.Add(s);
            }
            
return  Cmd;
        }
        
///  
        
///  执行无参数的OLEDBCommand对象(无返回)
        
///  

        
///   SQL语句
         public   void  Execute( string  SQL)
        {
            OleDbConnection conn 
=   new  OleDbConnection(ConnStr);
            conn.Open();
            
try
            {
                CreateCommand(SQL, conn).ExecuteNonQuery();
            }
            
catch
            {
                
throw ;
            }
            
finally
            {
                Dispose(conn);
            }
            
return ;
        }
        
///  
        
///  执行有参数的OLEDBCommand对象(无返回)
        
///  

        
///   SQL语句
        
///   OleDbParameter参数数组
         public   void  Execute( string  SQL, OleDbParameter[] SP)
        {
            OleDbConnection conn 
=   new  OleDbConnection(ConnStr);
            conn.Open();
            
try
            {
                CreateCommand(SQL, conn, SP).ExecuteNonQuery();
            }
            
catch
            {
                
throw ;
            }
            
finally
            {
                Dispose(conn);
            }
            
return ;
        }
        
///  
        
///  执行SQL语句返回OledbDataReader(记得关闭)
        
///  

        
///  
        
///  
         public  OleDbDataReader ExecuteReader( string  SQL)
        {
            OleDbDataReader dr;
            OleDbConnection conn 
=   new  OleDbConnection(ConnStr);
            conn.Open();
            
try
            {
                dr 
=  CreateCommand(SQL, conn).ExecuteReader();
            }
            
catch
            {
                
throw ;
            }
            
return  dr;
        }
        
///  
        
///  执行SQL语句返回OledbDataReader(记得关闭)
        
///  

        
///   SQL语句
        
///   OleDbParameter数组
        
///  
         public  OleDbDataReader ExecuteReader( string  SQL, OleDbParameter[] SP)
        {
            OleDbDataReader dr;
            OleDbConnection conn 
=   new  OleDbConnection(ConnStr);
            conn.Open();
            
try
            {
                dr 
=  CreateCommand(SQL, conn, SP).ExecuteReader();
            }
            
catch
            {
                
throw ;
            }
            
return  dr;
        }
        
///  
        
///  执行SQL语句返回DataSet
        
///  

        
///  
        
///  
         public  DataSet ExecuteDataSet( string  SQL,  string  TableName)
        {
            DataSet ds 
=   new  DataSet();
            OleDbConnection conn 
=   new  OleDbConnection(ConnStr);
            conn.Open();
            
try
            {
                CreateDataAdapter(SQL, conn).Fill(ds, TableName);
            }
            
catch
            {
                
throw ;
            }
            
finally
            {
                Dispose(conn);
            }

            
return  ds;
        }
        
///  
        
///  执行SQL语句返回DataSet
        
///  

        
///   SQL语句
        
///   表名
        
///   参数数组
        
///  
         public  DataSet ExecuteDataSet( string  SQL,  string  TableName, OleDbParameter[] sp)
        {
            DataSet ds 
=   new  DataSet();
            OleDbConnection conn 
=   new  OleDbConnection(ConnStr);
            conn.Open();
            
try
            {
                CreateDataAdapter(CreateCommand(SQL, conn, sp)).Fill(ds, TableName);
            }
            
catch
            {
                
throw ;
            }
            
finally
            {
                Dispose(conn);
            }
            
return  ds;
        }
        
///  
        
///  创建输入参数
        
///  

        
///   参数名称
        
///   参数类型
        
///   参数大小
        
///   参数值
        
///   新的参数
         public  OleDbParameter MakeInputParameter( string  ParameterName, OleDbType DbType,  int  Size,  object  Value)
        {
            
return  MakeParameter(ParameterName, DbType, Size, ParameterDirection.Input, Value);
        }

        
///  
        
///  创建输出参数
        
///  

        
///   参数名称
        
///   参数类型
        
///   参数大小
        
///   新的参数
         public  OleDbParameter MakeOutParameter( string  ParameterName, OleDbType DbType,  int  Size)
        {
            
return  MakeParameter(ParameterName, DbType, Size, ParameterDirection.Output,  null );
        }

        
///  
        
///  创建储存过程参数
        
///  

        
///   参数名称
        
///   参数类型
        
///   参数大小
        
///   参数方法
        
///   参数值
        
///   新的参数
         public  OleDbParameter MakeParameter( string  ParameterName, OleDbType DbType, Int32 Size, ParameterDirection Direction,  object  Value)
        {
            OleDbParameter AccParam;
            
if  (Size  >   0 )
            {
                AccParam 
=   new  OleDbParameter(ParameterName, DbType, Size);
            }
            
else
            {
                AccParam 
=   new  OleDbParameter(ParameterName, DbType);
            }

            AccParam.Direction 
=  Direction;
            
if  ( ! (Direction  ==  ParameterDirection.Output  &&  Value  ==   null ))
            {
                AccParam.Value 
=  Value;
            }
            
return  AccParam;
        }
        
///  
        
///  查询指定数量数据,返回DataSet(用于分页)
        
///  

        
///   表名
        
///   主键
        
///   要查询的字段
        
///   排序字段
        
///   查询条件
        
///   每页大小
        
///   当前页码
        
///  
         public  DataSet GetDataSet( string  TableName,  string  PK,  string  Fields,  string  Sort,  string  Filter,  int  PageSize,  int  CurrentPage)
        {
            DataSet ds 
=   new  DataSet();
            OleDbConnection conn
= new  OleDbConnection(ConnStr);
            conn.Open();
            
string  sql  =   "" , and  =   "" ;
            
int  num  =   0 ;
            
if  (Fields  ==   "" ) Fields  =   " * " ;
            
if  (Sort  ==   "" )
            {
                Sort 
=   string .Format( " order by {0} asc " , PK);
            }
            
else
            {
                Sort 
=   string .Format( " order by {0} " , Sort);
            }
            
if  (PageSize  <=   0 ) PageSize  =   10 ;
            
if  (CurrentPage  <   1 ) CurrentPage  =   1 ;
            num 
=  (CurrentPage  -   1 *  PageSize;
            
if  (Filter  !=   "" )
            {
                Filter 
=   " where ( "   +  Filter  +   " ) " ;
                and 
=   " and " ;
            }
            
else
            {
                and 
=   " where " ;
            }
            
if  (CurrentPage  ==   1 )
            {
                sql 
=   string .Format( " select top {0} {1} from {2} {3} {4} " , PageSize, Fields, TableName, Filter, Sort);
            }
            
else
            {
                sql 
=   string .Format( " select top {0} {1} from {2} {3} {4} ({5} not in(select top {6} {7} from {8} {9} {10})) {11} " , PageSize, Fields, TableName, Filter, and, PK, num, PK, TableName, Filter, Sort, Sort);
            }
            ds 
=  ExecuteDataSet(sql, TableName);
            
return  ds;
        }
        
///  
        
///  查询指定数量数据,返回OleDbDataReader(用于分页)
        
///  

        
///   表名
        
///   主键
        
///   要查询的字段
        
///   排序字段
        
///   查询条件
        
///   每页大小
        
///   当前页码
        
///  
         public  OleDbDataReader GetDataReader( string  TableName,  string  PK,  string  Fields,  string  Sort,  string  Filter,  int  PageSize,  int  CurrentPage)
        {
            OleDbDataReader dr;
            OleDbConnection conn 
=   new  OleDbConnection(ConnStr);
            conn.Open();
            
string  sql  =   "" , and  =   "" ;
            
int  num  =   0 ;
            
if  (Fields  ==   "" ) Fields  =   " * " ;
            
if  (Sort  ==   "" )
            {
                Sort 
=   string .Format( " order by {0} asc " , PK);
            }
            
else
            {
                Sort 
=   string .Format( " order by {0} " , Sort);
            }
            
if  (PageSize  <=   0 ) PageSize  =   10 ;
            
if  (CurrentPage  <   1 ) CurrentPage  =   1 ;
            num 
=  (CurrentPage  -   1 *  PageSize;
            
if  (Filter  !=   "" )
            {
                Filter 
=   " where ( "   +  Filter  +   " ) " ;
                and 
=   " and " ;
            }
            
else
            {
                and 
=   " where " ;
            }
            
if  (CurrentPage  ==   1 )
            {
                sql 
=   string .Format( " select top {0} {1} from {2} {3} {4} " , PageSize, Fields, TableName, Filter, Sort);
            }
            
else
            {
                sql 
=   string .Format( " select top {0} {1} from {2} {3} {4} ({5} not in(select top {6} {7} from {8} {9} {10})) {11} " , PageSize, Fields, TableName, Filter, and, PK, num, PK, TableName, Filter, Sort, Sort);
            }
            dr 
=  ExecuteReader(sql);
            
return  dr;
        }
    }
}
 

你可能感兴趣的:(ASP.NET)