• 自己写的GeneralDatabaseLayer


    菜鸟一个 在校大二学生 奉老师旨意写了个通用数据库层 各位大哥大姐的看过的给点意见 本人实在是没什么经验 指点指点我啊

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.Common;
    using System.Configuration;

    namespace GeneralDatabaseLayer
    {
        public class GeneralDatabaseLayer
        {
            /// <summary>
            /// ADO.NET数据库操作的简单封装
            /// Author:kklldog
            /// Create Date:2008-06-25
            /// </summary>
            ///


            string DBType;//数据库类型
            string connectionString;//连接字符串

            public GeneralDatabaseLayer()
            {
                this.GetDBType();
                this.GetConnectionString();
            }

            /// <summary>
            /// 从配置文件里读取数据库类型赋值给DBType,并返回DBType的值
            /// </summary>
            /// <returns>DBType的值</returns>
            /// Create Date:2008-07-1
            public string GetDBType()
            {
                DBType = ConfigurationManager.AppSettings["DBType"];
                return DBType;
            }

            /// <summary>
            /// 不从配置文件里获取数据库的类型,直接通过参数DBType赋值。
            /// </summary>
            /// <param name="DBType">数据库类型</param>
            /// <returns>返回void</returns>
            /// Create Date:2008-07-1
            public void SetDBType(string DBType)
            {
                this.DBType = DBType;
            }

            /// <summary>
            /// 从配置文件里读取连接字符串,赋值给connectionString并返回connectionString的值
            /// </summary>
            /// <returns>connectionString的值</returns>
            /// Create Date:2008-07-1
            public string GetConnectionString()
            {
                connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                return connectionString;
            }
            /// <summary>
            /// 不从配置文件里读取连接字符串,直接通过参数赋值给connectionString
            /// </summary>
            /// <returns>void</returns>
            /// Create Date:2008-07-1
            public void SetConnectionString(string connectionString)
            {
                this.connectionString = connectionString;
            }


            /// <summary>
            /// 跟据SQL语句返回相应的DataSet
            /// </summary>
            /// <param name="strSql">要执行的SQL查询语句</param>
            /// <returns>返回得到的DataSet</returns>
            /// Create Date:2008-06-25
            public DataSet GetDataSet(string strSql)
            {
                DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);
                DbConnection DbConn = DbProvider.CreateConnection();
                DbDataAdapter DbAdapter = DbProvider.CreateDataAdapter();
                DbCommand DbCMD = DbConn.CreateCommand();
                DbConn.ConnectionString = this.connectionString;
                DbCMD.Connection = DbConn;
                DbCMD.CommandText = strSql;
                DbAdapter.SelectCommand = DbCMD;
                DataSet dataset = new DataSet();
                try
                {
                    DbConn.Open();
                    DbAdapter.Fill(dataset);
                }
                catch (DbException Ex)
                {
                    throw new Exception(Ex.Message);
                }
                finally
                {
                    if (DbConn != null)
                        DbConn.Close();
                }
                return dataset;
            }


            /// <summary>
            ///执行带参数的SQL查询语句
            /// </summary>
            /// <param name="strSql">要执行的SQL查询语句</param>
            /// <param name="Parameters">参数集</param>
            /// <returns>返回得到的DataSet</returns>
            /// Create Date:2008-07-5
            public DataSet GetDataSet(string strSql, DbParameter[] Parameters)
            {
                DataSet dataset = new DataSet();
                DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);
                DbConnection DbConn = DbProvider.CreateConnection();
                DbDataAdapter DbAdapter = DbProvider.CreateDataAdapter();
                DbCommand DbCMD = DbConn.CreateCommand();
                try
                {
                    DbConn.ConnectionString = this.connectionString;
                    DbCMD.Connection = DbConn;
                    DbCMD.CommandText = strSql;
                    DbCMD.Parameters.Clear();
                    DbCMD.Parameters.AddRange(Parameters);
                    DbAdapter.SelectCommand = DbCMD;
                    DbConn.Open();
                    DbAdapter.Fill(dataset);
                }
                catch (DbException Ex)
                {
                    throw new Exception(Ex.Message);
                }
                finally
                {
                    if (DbConn != null)
                        DbConn.Close();
                }
                return dataset;
            }

            /// <summary>
            /// 执行SQL操作语句返回受影响的记录条数,根据类变量connectionString的值来创建连接字符串
            /// </summary>
            /// <param name="strSql">要执行的SQL操作语句</param>
            /// <returns>返回受影响的记录条数</returns>
            /// Create Date:2008-06-25
            public int ExecuteNonQuery(string strSql)
            {
                int iCount = -1;
                DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂

                DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
                DbConn.ConnectionString = this.connectionString;
                try
                {
                    DbCommand cmd = DbProvider.CreateCommand();
                    cmd.CommandText = strSql;
                    cmd.Connection = DbConn;
                    DbConn.Open();
                    iCount = cmd.ExecuteNonQuery();
                }
                catch (DbException Ex)
                {
                    throw new Exception(Ex.Message);
                }
                finally
                {
                    if (DbConn != null)
                        DbConn.Close();
                }
                return iCount;
            }

            /// <summary>
            /// 执行带参数的SQL操作语句
            /// </summary>
            /// <param name="strSql">要执行的SQL操作语句</param>
            /// <param name="Parameters">参数集</param>
            /// <returns>返回受影响的记录条数</returns>
            /// Create Date:2008-06-25
            public int ExecuteNonQuery(string strSql, DbParameter[] Parameters)
            {
                int iCount = -1;
                DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂

                DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象

                try
                {
                    DbConn.ConnectionString = this.connectionString;
                    DbCommand cmd = DbProvider.CreateCommand();
                    cmd.CommandText = strSql;
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddRange(Parameters);
                    cmd.Connection = DbConn;
                    DbConn.Open();
                    iCount = cmd.ExecuteNonQuery();
                }
                catch (DbException Ex)
                {
                    throw new Exception(Ex.Message);
                }
                finally
                {
                    if (DbConn != null)
                        DbConn.Close();
                }
                return iCount;
            }


            /// <summary>
            /// 执行不带参数的存储过程,并返回受影响的行数
            /// </summary>
            /// <param name="ProcedureName">存储过程名称</param>
            /// <returns>受影响的行数</returns>
            /// Create Date:2008-07-1
            public int ExecuteProcedure(string ProcedureName)
            {
                int iCount = -1;
                DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂
                DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
                DbCommand cmd = DbProvider.CreateCommand();
                try
                {
                    DbConn.ConnectionString = this.connectionString;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = ProcedureName;
                    cmd.Connection = DbConn;
                    DbConn.Open();
                    iCount = cmd.ExecuteNonQuery();
                }
                catch (DbException Ex)
                {
                    throw new Exception(Ex.Message);
                }
                finally
                {
                    if (DbConn != null)
                        DbConn.Close();
                }
                return iCount;

            }


            /// <summary>
            /// 执行带参数的存储过程,并返回受影响的行数
            /// </summary>
            /// <param name="ProcedureName">存储过程名称</param>
            /// <param name="Patameters">存储过程的参数集</param>
            /// <returns>受影响的记录条数</returns>
            /// Create Date:2008-07-1
            public int ExecuteProcedure(string ProcedureName, DbParameter[] Parameters)
            {

                int iCount = -1;

                DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType); //创建数据库工厂

                DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
                DbConn.ConnectionString = this.connectionString;
                DbCommand cmd = DbProvider.CreateCommand();
                try
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = ProcedureName;
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddRange(Parameters);
                    cmd.Connection = DbConn;
                    DbConn.Open();
                    iCount = cmd.ExecuteNonQuery();
                }
                catch (DbException Ex)
                {
                    throw new Exception(Ex.Message);
                }
                finally
                {
                    if (DbConn != null)
                        DbConn.Close();
                }
                return iCount;

            }


            /// <summary>
            /// 执行不带参数的存储过程,并返回DataSet
            /// </summary>
            /// <param name="ProcedureName">存储过程名称</param>
            /// <returns>DataSet</returns>
            /// Create Date:2008-07-5
            public DataSet ExectueProcGetDS(string ProcedureName)
            {
                DataSet DS = new DataSet();

                DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂

                DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
                DbConn.ConnectionString = this.connectionString;
                DbCommand cmd = DbProvider.CreateCommand();
                DbDataAdapter DbAdapater = DbProvider.CreateDataAdapter();
                try
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = ProcedureName;
                    cmd.Connection = DbConn;
                    DbAdapater.SelectCommand = cmd;
                    DbConn.Open();
                    DbAdapater.Fill(DS);
                }
                catch (DbException Ex)
                {
                    throw new Exception(Ex.Message);
                }
                finally
                {
                    if (DbConn != null)
                        DbConn.Close();
                }
                return DS;
            }


            /// <summary>
            /// 执行带参数的存储过程,并返回DataSet
            /// </summary>
            /// <param name="ProcedureName">存储过程名称</param>
            /// <param name="Patameters">存储过程的参数集</param>
            /// <returns>DataSet</returns>
            /// Create Date:2008-07-5
            public DataSet ExectueProcGetDS(string ProcedureName, DbParameter[] Parameters)
            {
                DataSet DS = new DataSet();

                DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂

                DbConnection DbConn = DbProvider.CreateConnection(); //创建数据库连接对象
                DbConn.ConnectionString = this.connectionString;
                DbCommand cmd = DbProvider.CreateCommand();
                DbDataAdapter DbAdapater = DbProvider.CreateDataAdapter();
                try
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = ProcedureName;
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddRange(Parameters);
                    cmd.Connection = DbConn;
                    DbAdapater.SelectCommand = cmd;
                    DbConn.Open();
                    DbAdapater.Fill(DS);
                }
                catch (DbException Ex)
                {
                    throw new Exception(Ex.Message);
                }
                finally
                {
                    if (DbConn != null)
                        DbConn.Close();
                }
                return DS;
            }

        }
    }

    QQ群:1022985150 VX:kklldog 一起探讨学习.NET技术
    作者:Agile.Zhou(kklldog)
    出处:http://www.cnblogs.com/kklldog/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    蚂蚁和木杆问题
    计算二进制中1的个数(转)
    strlen源码剖析(转)
    bashrc
    smb.conf配置文件(Centos)
    配置VNC+PuTTY+SSH Tunnel访问Linux
    标题: BASH最常见的激活模式
    KMP算法
    字符串中单词逆序
    Java中堆内存和栈内存详解
  • 原文地址:https://www.cnblogs.com/kklldog/p/1236951.html
Copyright © 2020-2023  润新知