(精华)2020年6月27日 C#类库 DbProviderFactoryHelper(Ado.net数据库封装)

using EFCore.Sharding;
using MySql.Data.MySqlClient;
using Npgsql;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;

namespace Core.Util
{
    /// 
    /// 数据库操作提供源工厂帮助类
    /// 
    public class DbProviderFactoryHelper
    {
        #region 外部接口

        /// 
        /// 获取提供工厂
        /// 
        /// 数据库类型
        /// 
        public static DbProviderFactory GetDbProviderFactory(DatabaseType dbType)
        {
            DbProviderFactory factory = null;
            switch (dbType)
            {
                case DatabaseType.SqlServer: factory = SqlClientFactory.Instance; break;
                case DatabaseType.MySql: factory = MySqlClientFactory.Instance; break;
                case DatabaseType.PostgreSql: factory = NpgsqlFactory.Instance; break;
                case DatabaseType.Oracle: factory = OracleClientFactory.Instance; break;

                default: throw new Exception("请传入有效的数据库!");
            }

            return factory;
        }

        /// 
        /// 获取DbConnection
        /// 
        /// 数据库类型
        /// 
        public static DbConnection GetDbConnection(DatabaseType dbType)
        {
            var con = GetDbProviderFactory(dbType).CreateConnection();

            return con;
        }

        /// 
        /// 获取DbCommand
        /// 
        /// 数据库类型
        /// 
        public static DbCommand GetDbCommand(DatabaseType dbType)
        {
            return GetDbProviderFactory(dbType).CreateCommand();
        }

        /// 
        /// 获取DbParameter
        /// 
        /// 数据库类型
        /// 
        public static DbParameter GetDbParameter(DatabaseType dbType)
        {
            return GetDbProviderFactory(dbType).CreateParameter();
        }

        /// 
        /// 获取DataAdapter
        /// 
        /// 数据库类型
        /// 
        public static DataAdapter GetDataAdapter(DatabaseType dbType)
        {
            return GetDbProviderFactory(dbType).CreateDataAdapter();
        }

        /// 
        /// 将数据库类型字符串转换为对应的数据库类型
        /// 
        /// 数据库类型字符串
        /// 
        public static DatabaseType DbTypeStrToDbType(string dbTypeStr)
        {
            if (dbTypeStr.IsNullOrEmpty())
                throw new Exception("请输入数据库类型字符串!");
            else
            {
                switch (dbTypeStr.ToLower())
                {
                    case "sqlserver": return DatabaseType.SqlServer;
                    case "mysql": return DatabaseType.MySql;
                    case "oracle": return DatabaseType.Oracle;
                    case "postgresql": return DatabaseType.PostgreSql;
                    default: throw new Exception("请输入合法的数据库类型字符串!");
                }
            }
        }

        /// 
        /// 将数据库类型转换为对应的数据库类型字符串
        /// 
        /// 
        public static string DbTypeToDbTypeStr(DatabaseType dbType)
        {
            if (dbType.IsNullOrEmpty())
                throw new Exception("请输入数据库类型!");
            else
            {
                switch (dbType)
                {
                    case DatabaseType.SqlServer: return "SqlServer";
                    case DatabaseType.MySql: return "MySql";
                    case DatabaseType.Oracle: return "Oracle";
                    case DatabaseType.PostgreSql: return "PostgreSql";
                    default: throw new Exception("请输入合法的数据库类型!");
                }
            }
        }

        #endregion
    }
}

你可能感兴趣的:(#,C#类库/数据库)