-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDBFactory.cs
197 lines (185 loc) · 7.83 KB
/
IDBFactory.cs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Data.SqlClient;
using System.IO;
namespace DBUtil
{
/// <summary>
/// 根据不同的数据库创建数据库访问对象:IDBAccess
/// </summary>
public class IDBFactory
{
/// <summary>
/// 创建IDB对象,注意.netcore中不支持oledb,这里也不再支持oledb、access
/// <para>
/// 示例:DBUtil.IDbAccess iDb = DBUtil.IDBFactory.CreateIDB("Data Source=.;Initial Catalog=JACKOA;User ID=sa;Password=sa;","SQLSERVER");
/// </para>
/// </summary>
/// <param name="connStr">
/// <para>连接字符串:</para>
/// <para>SQLSERVER: Data Source=.;Initial Catalog=JACKOA;User ID=sa;Password=xx;</para>
/// <para>ORACLE: Data Source=ORCLmyvm2;Password=sys123;User ID=sys;DBA Privilege=SYSDBA;</para>
/// <para>MYSQL: Data Source=localhost;Initial Catalog=test;User ID=root;Password=xxxx;</para>
/// <para>POSTGRESQL: Server=localhost;Port=5432;UserId=postgres;Password=xxxx;Database=test</para>
/// <para>SQLITE: Data Source=f:\demo.db;</para>
/// </param>
/// <param name="DBType">数据库类型:SQLSERVER、ORACLE、MYSQL、SQLITE、ACCESS、POSTGRESQL</param>
/// <returns></returns>
public static IDbAccess CreateIDB(string connStr, string DBType)
{
DBType = (DBType ?? "").ToUpper();
if (DBType == "SQLSERVER")
{
SqlConnection conn = new SqlConnection(connStr);
IDbAccess iDb = new SqlServerIDbAccess()
{
conn = conn,
ConnectionString = connStr,
DataBaseType = DataBaseType.SQLSERVER
};
return iDb;
}
else if (DBType == "MYSQL")
{
//使用单独一个方法,防止在下面代码访问不到的情况下仍会因没有mysql组件而报错
return CreateMySql(connStr);
}
else if (DBType == "ORACLE")
{
//使用单独一个方法,防止在下面代码访问不到的情况下仍会因没有oracle组件而报错
return CreateOracle(connStr);
}
else if (DBType == "SQLITE")
{
//使用单独一个方法,防止在下面代码访问不到的情况下仍会因没有sqlite组件而报错
return CreateSQLite(connStr);
}
else if (DBType == "POSTGRESQL")
{
//使用单独一个方法,防止在下面代码访问不到的情况下仍会因没有postgresql组件而报错
return CreatePostgreSql(connStr);
}
else
{
throw new Exception("暂不支持这种(" + DBType + ")数据库!");
}
}
/// <summary>
/// 创建IDB对象,注意.netcore中不支持oledb,这里也不再支持oledb、access
/// <para>
/// 示例:DBUtil.IDbAccess iDb = DBUtil.IDBFactory.CreateIDB("Data Source=.;Initial Catalog=JACKOA;User ID=sa;Password=sa;","SQLSERVER");
/// </para>
/// </summary>
/// <param name="connStr">
/// <para>连接字符串:</para>
/// <para>SQLSERVER: Data Source=.;Initial Catalog=JACKOA;User ID=sa;Password=xx;</para>
/// <para>ORACLE: Data Source=ORCLmyvm2;Password=sys123;User ID=sys;DBA Privilege=SYSDBA;</para>
/// <para>MYSQL: Data Source=localhost;Initial Catalog=test;User ID=root;Password=xxxx;</para>
/// <para>POSTGRESQL: Server=localhost;Port=5432;UserId=postgres;Password=xxxx;Database=test</para>
/// <para>SQLITE: Data Source=f:\demo.db;</para>
/// </param>
/// <param name="DBType">数据库类型:SQLSERVER、ORACLE、MYSQL、SQLITE、ACCESS、POSTGRESQL</param>
/// <returns></returns>
public static IDbAccess CreateIDB(string connStr, DataBaseType DBType)
{
string dbtype = DBType.ToString();
return CreateIDB(connStr, dbtype);
}
private static IDbAccess CreateOracle(string connStr)
{
Oracle.ManagedDataAccess.Client.OracleConnection conn = new Oracle.ManagedDataAccess.Client.OracleConnection(connStr);
IDbAccess iDb = new OracleIDbAccess()
{
conn = conn,
ConnectionString = connStr,
DataBaseType = DataBaseType.ORACLE
};
return iDb;
}
private static IDbAccess CreateMySql(string connStr)
{
MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connStr);
IDbAccess iDb = new MySqlIDbAccess()
{
conn = conn,
ConnectionString = connStr,
DataBaseType = DataBaseType.MYSQL
};
return iDb;
}
private static IDbAccess CreatePostgreSql(string connStr)
{
Npgsql.NpgsqlConnection conn = new Npgsql.NpgsqlConnection(connStr);
IDbAccess iDb = new PostgreSqlIDbAccess()
{
conn = conn,
ConnectionString = connStr,
DataBaseType = DataBaseType.PostgreSql
};
return iDb;
}
/// <summary>
/// 创建Sqlite数据库文件,如果已存在就报错
/// <para>
/// 示例:IDBFactory.CreateSQLiteDB(@"D:\demo.db");
/// </para>
/// </summary>
/// <param name="absPath">文件绝对路径</param>
public static void CreateSQLiteDB(string absPath)
{
if (File.Exists(absPath))
{
throw new Exception("要创建的数据库文件已存在,请核对:" + absPath);
}
System.Data.SQLite.SQLiteConnection.CreateFile(absPath);
}
/// <summary>
/// 获取Sqlite数据库连接方式
/// <para>
/// 示例:IDBFactory.GetSQLiteConnectionString(@"D:\demo.db");//返回"Data Source=D:\demo.db"
/// </para>
/// </summary>
/// <param name="absPath">文件绝对路径</param>
/// <returns></returns>
public static string GetSQLiteConnectionString(string absPath)
{
return GetSQLiteConnectionString(absPath, null);
}
/// <summary>
/// 获取Sqlite数据库连接方式
/// <para>
/// 示例:IDBFactory.GetSQLiteConnectionString(@"D:\demo.db","123456");//返回"Data Source=D:\demo.db;Password=123456"
/// </para>
/// </summary>
/// <param name="absPath">文件绝对路径</param>
/// <param name="pwd">sqlite文件密码</param>
/// <returns></returns>
public static string GetSQLiteConnectionString(string absPath, string pwd)
{
string str;
if (string.IsNullOrWhiteSpace(pwd))
{
str = "Data Source=" + absPath;
}
else
{
str = "Data Source=" + absPath + ";Password=" + pwd;
}
return str;
}
private static IDbAccess CreateSQLite(string connStr)
{
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connStr);
IDbAccess iDb = new SQLiteIDbAccess()
{
conn = conn,
ConnectionString = connStr,
DataBaseType = DataBaseType.SQLITE
};
return iDb;
}
/// <summary>
/// 数据库字段ID和自动编号生成器,一般不要在程序运行环境中修改此值(如果需要扩展,可以在应用程序启动时进行赋值)
/// </summary>
public static IDSNOManager IDSNOManage = new SimpleIDSNOManager();
}
}