(精华)2020年6月27日 C#类库 OracleHelper(Ado.net数据库封装)
【摘要】
using EFCore.Sharding;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.G...
using EFCore.Sharding;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data.Common;
namespace Core.Util
{
/// <summary>
/// Oracle数据库操作帮助类
/// </summary>
public class OracleHelper : DbHelper
{
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
/// <param name="conString">完整连接字符串</param>
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 外部接口
/// <summary>
/// 获取数据库中的所有表
/// </summary>
/// <param name="schemaName">模式(架构)</param>
/// <returns></returns>
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);
}
/// <summary>
/// 通过连接字符串和表名获取数据库表的信息
/// </summary>
/// <param name="tableName">表名</param>
/// <returns></returns>
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) });
}
/// <summary>
/// 生成实体文件
/// </summary>
/// <param name="infos">表字段信息</param>
/// <param name="tableName">表名</param>
/// <param name="tableDescription">表描述信息</param>
/// <param name="filePath">文件路径(包含文件名)</param>
/// <param name="nameSpace">实体命名空间</param>
/// <param name="schemaName">架构(模式)名</param>
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
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/106980785
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)