forked from circler3/DatabaseInvoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlManipulation.cs
325 lines (302 loc) · 10.3 KB
/
SqlManipulation.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using System.Collections.Generic;
using System.Data.Odbc;
using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System.Data.SqlClient;
using System.Data.SQLite;
using Npgsql;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Common;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace DatabaseInvoke
{
/// <summary>
/// Add or remove the supported type by adding or removing the related GetConnection or GetDbDataAdapter method.
/// 可以根据支持的Sql类型增加或删除类型,需要增加或删除对应的GetConnection和GetDbDataAdapter方法。
/// </summary>
public enum SqlType
{
SqlServer,
MySql,
PostgresQL,
Oracle,
SQLite,
//(ODBC ONLY) host system must have the data driver installed. ODBC data source is necessary if DSN is used.
//对ODBC方式需要格外注意,目标系统必须预先安装有对应的数据驱动,如果使用DSN,那么还需要使用配置ODBC数据源
Odbc
}
/// <summary>
/// The class use ADO.NET access to database. It is thread safe if a single active object is used.
/// 使用ADO.NET控制对数据库的基本访问方法,对同一个活动对象(不关闭)线程安全。
/// </summary>
public class SqlManipulation : IDisposable
{
public SqlManipulation(string strDSN, SqlType sqlType)
{
_sqlType = sqlType;
_strDSN = strDSN;
}
#region private variables
private SqlType _sqlType;
private string _strDSN;
private DbConnection _conn;
private bool _disposed;
#endregion
#region private functions
private DbConnection GetConnection()
{
DbConnection conn;
switch (_sqlType)
{
case SqlType.SqlServer:
conn = new SqlConnection(_strDSN);
return conn;
case SqlType.MySql:
conn = new MySqlConnection(_strDSN);
return conn;
case SqlType.PostgresQL:
conn = new NpgsqlConnection(_strDSN);
return conn;
case SqlType.Oracle:
conn = new OracleConnection(_strDSN);
return conn;
case SqlType.SQLite:
conn = new SQLiteConnection(_strDSN);
return conn;
case SqlType.Odbc:
conn = new OdbcConnection(_strDSN);
return conn;
default:
return null;
}
}
private DbDataAdapter GetDbDataAdapter(string sql)
{
DbDataAdapter adp;
switch (_sqlType)
{
case SqlType.SqlServer:
adp = new SqlDataAdapter(sql, _conn as SqlConnection);
return adp;
case SqlType.MySql:
adp = new MySqlDataAdapter(sql, _conn as MySqlConnection);
return adp;
case SqlType.PostgresQL:
adp = new NpgsqlDataAdapter(sql, _conn as NpgsqlConnection);
return adp;
case SqlType.Oracle:
adp = new OracleDataAdapter(sql, _conn as OracleConnection);
return adp;
case SqlType.SQLite:
adp = new SQLiteDataAdapter(sql, _conn as SQLiteConnection);
return adp;
case SqlType.Odbc:
adp = new OdbcDataAdapter(sql, _conn as OdbcConnection);
return adp;
default:
return null;
}
}
private DbCommand GetCommand(DbConnection conn, string strSQL)
{
DbCommand command = conn.CreateCommand();
command.CommandText = strSQL;
return command;
}
private DbDataReader GetDataReader(DbConnection conn, string strSQL)
{
DbCommand command = GetCommand(conn, strSQL);
return command.ExecuteReader();
}
#endregion
/// <summary>
/// Init a connection and open it.
/// 初始化连接并打开
/// </summary>
/// <returns>true if successful</returns>
public bool Init()
{
try
{
_conn = GetConnection();
_conn.Open();
return true;
}
catch (Exception e)
{
//log and exit
ErrorHelper.Error(e);
return false;
}
}
/// <summary>
/// Execute the select query.
/// 执行SELECT查询语句,并返回DataTable对象。
/// </summary>
/// <param name="strSQL">
/// Sql to be executed.
/// 需要执行的sql语句
/// </param>
/// <returns>DataTable object</returns>
public DataTable ExcuteQuery(string strSQL)
{
DbDataAdapter adp = GetDbDataAdapter(strSQL);
DataTable dt = new DataTable();
try
{
adp.Fill(dt);
}
catch (Exception e)
{
//log error and return null
ErrorHelper.Error(e);
return null;
}
return dt;
}
/// <summary>
/// 执行非Select语句,包括UPDATE DELETE INSERT
/// </summary>
/// <param name="strSQL">需要执行的sql语句</param>
/// <returns>受影响的行数</returns>
public int ExcuteNonQuery(string strSQL)
{
//实例化OdbcCommand对象
DbCommand myCmd = GetCommand(_conn, strSQL);
try
{
//执行方法
return myCmd.ExecuteNonQuery();
}
catch (Exception e)
{
//记录日志,并返回0
ErrorHelper.Error(e);
return 0;
}
}
/// <summary>
/// 通过事务批量执行非查询SQL语句
/// </summary>
/// <param name="strSQLs">需要批量执行的SQL</param>
/// <returns>受影响的行数,发生回滚则返回-1</returns>
public int ExecuteNonQueryTransaction(List<string> strSQLs)
{
DbCommand myCmd = GetCommand(_conn, "");
int sumAffected = 0;
DbTransaction transaction = _conn.BeginTransaction();
myCmd.Transaction = transaction;
try
{
foreach (var n in strSQLs)
{
myCmd.CommandText = n;
sumAffected += myCmd.ExecuteNonQuery();
}
transaction.Commit();
return sumAffected;
}
catch (Exception e)
{
ErrorHelper.Error(e);
transaction.Rollback();
return -1;
}
}
/// <summary>
/// 执行非查询SQL,使用Oracle的Blob对象
/// </summary>
/// <param name="sql">需要执行的sql语句,语句中有且仅有一个‘:blob’参数</param>
/// <param name="path">需要插入的blob文件路径</param>
/// <returns>受影响的行数,执行不成功则返回-1</returns>
public int OracleBlobNonQuery(string sql, string path)
{
if (!(_conn is OracleConnection))
{
return -1;
}
//OracleCommand cmd = new OracleCommand("INSERT INTO TEST SET F2 =:blob", _conn as OracleConnection);
OracleCommand cmd = new OracleCommand(sql, _conn as OracleConnection);
cmd.Parameters.Add(new OracleParameter("blob", OracleDbType.Blob));
FileInfo fi = new FileInfo(path);
var mydata = File.ReadAllBytes(path);
cmd.Parameters["blob"].Value = mydata;
try
{
return cmd.ExecuteNonQuery();
}
catch (Exception e)
{
ErrorHelper.Error(e);
return -1;
}
}
/// <summary>
/// 执行非查询SQL,使用SQL Server的Image对象
/// </summary>
/// <param name="sql">需要执行的sql语句,语句中有且仅有一个‘@blob’参数</param>
/// <param name="path">需要插入的blob文件路径</param>
/// <returns>受影响的行数,执行不成功则返回-1</returns>
public int SqlImageNonQuery(string sql, string path)
{
if (!(_conn is SqlConnection))
{
return -1;
}
//SqlCommand cmd = new SqlCommand("INSERT INTO TEST SET F2 =@blob", _conn as SqlConnection);
SqlCommand cmd = new SqlCommand(sql, _conn as SqlConnection);
cmd.Parameters.Add(new SqlParameter("@blob", SqlDbType.Image));
FileInfo fi = new FileInfo(path);
var mydata = File.ReadAllBytes(path);
cmd.Parameters["@blob"].Value = mydata;
try
{
return cmd.ExecuteNonQuery();
}
catch (Exception e)
{
ErrorHelper.Error(e);
return -1;
}
}
#region 资源清理
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
//托管对象释放
}
//非托管对象释放
if (_conn != null)
{
if (_conn.State != ConnectionState.Closed)
{
_conn.Close();
}
else
{
_conn = null;
}
}
_disposed = true;
}
}
~SqlManipulation()
{
Dispose(false);
}
#endregion
}
}