(精华)2020年6月27日 C#类库 DbProviderFactoryHelper(Ado.net数据库封装)
【摘要】
using EFCore.Sharding;
using MySql.Data.MySqlClient;
using Npgsql;
using Oracle.ManagedDataAccess.Clie...
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
{
/// <summary>
/// 数据库操作提供源工厂帮助类
/// </summary>
public class DbProviderFactoryHelper
{
#region 外部接口
/// <summary>
/// 获取提供工厂
/// </summary>
/// <param name="dbType">数据库类型</param>
/// <returns></returns>
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;
}
/// <summary>
/// 获取DbConnection
/// </summary>
/// <param name="dbType">数据库类型</param>
/// <returns></returns>
public static DbConnection GetDbConnection(DatabaseType dbType)
{
var con = GetDbProviderFactory(dbType).CreateConnection();
return con;
}
/// <summary>
/// 获取DbCommand
/// </summary>
/// <param name="dbType">数据库类型</param>
/// <returns></returns>
public static DbCommand GetDbCommand(DatabaseType dbType)
{
return GetDbProviderFactory(dbType).CreateCommand();
}
/// <summary>
/// 获取DbParameter
/// </summary>
/// <param name="dbType">数据库类型</param>
/// <returns></returns>
public static DbParameter GetDbParameter(DatabaseType dbType)
{
return GetDbProviderFactory(dbType).CreateParameter();
}
/// <summary>
/// 获取DataAdapter
/// </summary>
/// <param name="dbType">数据库类型</param>
/// <returns></returns>
public static DataAdapter GetDataAdapter(DatabaseType dbType)
{
return GetDbProviderFactory(dbType).CreateDataAdapter();
}
/// <summary>
/// 将数据库类型字符串转换为对应的数据库类型
/// </summary>
/// <param name="dbTypeStr">数据库类型字符串</param>
/// <returns></returns>
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("请输入合法的数据库类型字符串!");
}
}
}
/// <summary>
/// 将数据库类型转换为对应的数据库类型字符串
/// </summary>
/// <returns></returns>
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
}
}
- 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
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/106980137
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)