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

using EFCore.Sharding;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data.Common;

namespace Core.Util
{
    /// 
    /// Oracle数据库操作帮助类
    /// 
    public class OracleHelper : DbHelper
    {
        #region 构造函数

        /// 
        /// 构造函数
        /// 
        /// 完整连接字符串
        public OracleHelper(string conString)
            : base(DatabaseType.Oracle, conString)
        {
        }

        #endregion

        #region 私有成员

        protected override Dictionary<string, Type> DbTypeDic { get; } = new Dictionary<string, Type>()
        {
            { "bfile", typeof(byte[]) },
            { "blob", typeof(byte[]) },
            { "char", typeof(string) },
            { "clob", typeof(string) },
            { "date", typeof(DateTime) },
            { "float", typeof(decimal) },
            { "integer", typeof(decimal) },
            { "interval year to month", typeof(int) },
            { "interval day to second", typeof(TimeSpan) },
            { "long", typeof(string) },
            { "long raw", typeof(string[]) },
            { "nchar", typeof(string) },
            { "nclob", typeof(string) },
            { "number", typeof(decimal) },
            { "nvarchar2", typeof(string) },
            { "raw", typeof(byte[]) },
            { "rowid", typeof(string) },
            { "timestamp", typeof(DateTime) },
            { "timestamp with local time zone", typeof(DateTime) },
            { "timestamp with time zone", typeof(DateTime) },
            { "unsigned integer", typeof(decimal) },
            { "varchar2", typeof(string) }
        };

        #endregion

        #region 外部接口

        /// 
        /// 获取数据库中的所有表
        /// 
        /// 模式(架构)
        /// 
        public override List<DbTableInfo> GetDbAllTables(string schemaName = null)
        {
            string sql = @"SELECT A.TABLE_NAME AS TABLENAME, B.comments AS DESCRIPTION
  FROM USER_TABLES A, USER_TAB_COMMENTS B
 WHERE A.table_name = B.table_name(+)";
            return GetListBySql<DbTableInfo>(sql);
        }

        /// 
        /// 通过连接字符串和表名获取数据库表的信息
        /// 
        /// 表名
        /// 
        public override List<TableInfo> GetDbTableInfo(string tableName)
        {
            string sql = @"
SELECT A.COLUMN_NAME AS NAME,
       A.DATA_TYPE   AS TYPE,
       NVL2(D.CONSTRAINT_TYPE,1,0) AS ISKEY,
       DECODE(A.NULLABLE,'Y',1,0) AS ISNULLABLE,
       B.COMMENTS AS DESCRIPTION
  FROM USER_TAB_COLUMNS A, USER_COL_COMMENTS B, USER_IND_COLUMNS C, USER_CONSTRAINTS D
 WHERE A.TABLE_NAME = B.TABLE_NAME(+)
   AND A.COLUMN_NAME = B.COLUMN_NAME(+)
   AND A.TABLE_NAME = C.TABLE_NAME(+)
   AND A.COLUMN_NAME = C.COLUMN_NAME(+)
   AND C.INDEX_NAME = D.INDEX_NAME(+)
   AND 'P' = D.CONSTRAINT_TYPE(+)
   AND A.TABLE_NAME= :tableName
 ORDER BY A.COLUMN_ID";
            return GetListBySql<TableInfo>(sql, new List<DbParameter> { new OracleParameter(":table_name", tableName) });
        }

        /// 
        /// 生成实体文件
        /// 
        /// 表字段信息
        /// 表名
        /// 表描述信息
        /// 文件路径(包含文件名)
        /// 实体命名空间
        /// 架构(模式)名
        public override void SaveEntityToFile(List<TableInfo> infos, string tableName, string tableDescription, string filePath, string nameSpace, string schemaName = null)
        {
            base.SaveEntityToFile(infos, tableName, tableDescription, filePath, nameSpace, schemaName);
        }

        #endregion
    }
}

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