游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2520|回复: 0

一个数据库连接类

[复制链接]

68

主题

710

帖子

719

积分

高级会员

Rank: 4

积分
719
QQ
发表于 2006-10-30 18:35:00 | 显示全部楼层 |阅读模式
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace sqlServer
{
    /// <summary>
    /// DataBase 的摘要说明。
    /// </summary>

    /// <summary>
    /// 数据库连接类型
    /// </summary>
    public enum dbType
    {
        /// <summary>
        /// SQL数据库
        /// </summary>
        sql,
        /// <summary>
        /// access数据库
        /// </summary>
        access
    }
   
    public class DataBase
    {
        // 连接数据源
        private SqlConnection con = null;
        private OleDbConnection conn = null;
        //连接数据类型
        dbType cType;
        //多线程sql字符串
        private string conString = null;
        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);
        }

        /// <summary>
        /// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns>DataSet</returns>
        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;

        }

        /// <summary>
        /// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <param name="sRecord">开始记录数</param>
        /// <param name="mRecord">最大记录数</param>
        /// <returns>DataSet</returns>
        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;

        }

        /// <summary>
        /// 对数据库的增,删,改的操作
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>是否成功</returns>
        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;
        }

        /// <summary>
        /// 获得该SQL查询返回的第一行第一列的值,如果没有查询到则返回NULL
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns>返回的第一行第一列的值</returns>
        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;
        }
        /// <summary>
        /// 多线程版本获得该SQL查询返回的第一行第一列的值,如果没有查询到则返回NULL
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns>返回的第一行第一列的值</returns>
        public void getValue()
        {
            string str = null;
            try
            {
                if (cType == dbType.sql)
                {
                    SqlCommand cmd = new SqlCommand(conString, con);
                    this.Open();
                    str = cmd.ExecuteScalar().ToString();

                }
                else if (cType == dbType.access)
                {
                    OleDbCommand cmd = new OleDbCommand(conString, conn);
                    this.Open();
                    str = cmd.ExecuteScalar().ToString();
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                this.Close();
            }

            //return str;
        }



        /// <summary>
        ///  获得该SQL查询返回DataTable,如果没有查询到则返回NULL
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns></returns>
        public DataTable getTable(string sql)
        {
            DataTable tb = null;
            DataSet ds = this.returnDS(sql);
            if (ds != null)
            {
                tb = ds.Tables["tempTable"];
            }

            return tb;
        }

        /// <summary>
        /// 打开数据库连接.
        /// </summary>
        public 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();
                }
            }
        }

        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public void Close()
        {
            if (cType == dbType.sql)
            {
                if (con != null)
                {
                    con.Close();
                }
            }
            else if (cType == dbType.access)
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        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;
                }
            }

        }

    }
}

sf_20061030183445.rar

1.39 KB, 下载次数:

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-25 18:12

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表