独上高楼网站
  •    你所在位置:首页 VS.netC#C#编程〉c#数据库连接类
  • c#数据库连接类
  • 作者:佚名  文章来源:http://www.cnsdn.com.cn/  发布日期:2007-08-01  浏览次数:1160
  • 打印这篇文章
  • using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.OleDb;

    namespace DB
    {
        /// 
        /// DataBase 的摘要说明。
        /// 

        
        /// 
        /// 数据库连接类型
        /// 

        public enum dbType
        {
            /// 
            /// SQL数据库
            /// 

            sql,
            /// 
            /// access数据库
            /// 

            access
        }
        public class DataBase
        {
            // 连接数据源
            private SqlConnection con = null;
            private OleDbConnection conn = null;
            //连接数据类型
            dbType cType;
            public DataBase(string conStr,dbType type)
            {
                this.cType = type;
                if(type == dbType.sql)
                    con = new SqlConnection(conStr);
                else if(type == dbType.access)
                    conn = new OleDbConnection(conStr);
            }

            /// 
            /// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
            /// 

            /// 查询语句
            /// DataSet
            public DataSet returnDS(string sql)
            {
                
                DataSet ds=new DataSet();
                try
                {
                    if(cType == dbType.sql)
                    {
                        SqlCommand cmd =new SqlCommand(sql,con);
                        cmd.CommandTimeout=20;
                        this.Open();
                        SqlDataAdapter adapter=new SqlDataAdapter(cmd);
                        adapter.Fill(ds,"tempTable");
                    
                    }
                    else if(cType == dbType.access)
                    {
                        OleDbCommand cmd = new OleDbCommand(sql,conn);
                        cmd.CommandTimeout=20;
                        this.Open();
                        System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(ds,"tempTable");
                    }
                }
                catch(Exception e)
                {
                    throw(e);
                    ds = null;
                }
                finally
                {
                    this.Close();    
                }

                return ds;
              
            }

            /// 
            /// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
            /// 

            /// 查询语句
            /// 开始记录数
            /// 最大记录数
            /// DataSet
            public DataSet returnDS(string sql,int sRecord,int mRecord)
            {
                
                DataSet ds=new DataSet();
                try
                {
                    if(cType == dbType.sql)
                    {
                        SqlCommand cmd =new SqlCommand(sql,con);
                        cmd.CommandTimeout=20;
                        this.Open();
                        SqlDataAdapter adapter=new SqlDataAdapter(cmd);
                        adapter.Fill(ds,sRecord,mRecord,"tempTable");
                    
                    }
                    else if(cType == dbType.access)
                    {
                        OleDbCommand cmd = new OleDbCommand(sql,conn);
                        cmd.CommandTimeout=20;
                        this.Open();
                        System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(ds,sRecord,mRecord,"tempTable");
                    }
                }
                catch(Exception e)
                {
                    throw(e);
                    ds = null;
                }
                finally
                {
                    this.Close();    
                }

                return ds;
              
            }

            /// 
            /// 对数据库的增,删,改的操作
            /// 

            /// SQL语句
            /// 是否成功
            public bool OperateDB(string sql)
            {
                bool succeed=false;
                int cnt = 0;
                try
                {
                    if(cType == dbType.sql)
                    {
                        SqlCommand cmd = new SqlCommand(sql,con);
                        cmd.CommandTimeout = 20;
                        this.Open();
                        cnt = cmd.ExecuteNonQuery();
                    
                    }
                    else if(cType == dbType.access)
                    {
                        OleDbCommand cmd = new OleDbCommand(sql,conn);
                        cmd.CommandTimeout=20;
                        this.Open();
                        cnt = cmd.ExecuteNonQuery();
                    }
                }
                catch(Exception e)
                {
                    throw(e);
                }
                finally
                {
                    if(cnt>0)
                    {
                        succeed=true;
                    }
                    this.Close();
                }
                    
                return succeed;
            }

            /// 
            /// 获得该SQL查询返回的第一行第一列的值,如果没有查询到则返回NULL
            /// 

            /// 查询语句
            /// 返回的第一行第一列的值
            public string getValue(string sql)
            {
                string str=null;
                try
                {
                    if(cType == dbType.sql)
                    {
                        SqlCommand cmd = new SqlCommand(sql,con);
                        this.Open();
                        str = cmd.ExecuteScalar().ToString();
                    
                    }
                    else if(cType == dbType.access)
                    {
                        OleDbCommand cmd = new OleDbCommand(sql,conn);
                        this.Open();
                        str = cmd.ExecuteScalar().ToString();
                    }
                }
                catch(Exception e)
                {
                    throw(e);
                }
                finally
                {
                    this.Close();
                }
                
                return str;
            }        


            /// 
            ///  获得该SQL查询返回DataTable,如果没有查询到则返回NULL
            /// 

            /// 查询语句
            /// 
            public DataTable getTable(string sql)
            {
                DataTable tb = null;
                DataSet ds = this.returnDS(sql);
                if(ds != null)
                {
                    tb = ds.Tables["tempTable"];
                }

                return tb;
            }
            
            /// 
            /// 打开数据库连接.
            /// 

            private void Open() 
            {
                if(cType == dbType.sql)
                {
                    if(con.State == System.Data.ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    else if(con.State == System.Data.ConnectionState.Broken)
                    {
                        con.Close();
                        con.Open();
                    }
                }
                else if(cType == dbType.access)
                {
                    if(conn.State == System.Data.ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    else if(conn.State == System.Data.ConnectionState.Broken)
                    {
                        conn.Close();
                        conn.Open();
                    }
                }
            }

            /// 
            /// 关闭数据库连接
            /// 

            public void Close() 
            {
                if(cType == dbType.sql)
                {
                    if (con != null)
                    {
                        con.Close();
                    }
                }
                else if(cType == dbType.access)
                {
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }

            /// 
            /// 释放资源
            /// 

            public void Dispose() 
            {
                if(cType == dbType.sql)
                {
                    // 确认连接是否已经关闭
                    if (con != null) 
                    {
                        con.Dispose();
                        con = null;
                    }  
                }
                else if(cType == dbType.access)
                {
                    if (conn != null) 
                    {
                        conn.Dispose();
                        conn = null;
                    }  
                }
      
            }

        }
    }
  • 打印这篇文章
  • 与本文主题相关的文章
  • 返回首页