-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCore.cs
451 lines (440 loc) · 15.1 KB
/
Core.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace miniORM
{
/// <summary>
/// 实体基类
/// </summary>
public abstract class BaseEntity
{
/// <summary>
/// 实现将数据库字段映射到对象中字段
/// </summary>
/// <param name="reader"></param>
internal abstract void SetEntity(IDataReader reader, Dictionary<string, int> lowerFields);
/// <summary>
/// 获取数据库字段数据
/// </summary>
/// <typeparam name="ObjectType"></typeparam>
/// <param name="reader"></param>
/// <param name="field"></param>
/// <returns></returns>
internal ObjectType GetField<ObjectType>(IDataReader reader, int index)
{
if (reader.IsDBNull(index))
return default(ObjectType);
object obj = reader.GetValue(index);
Type dtype = typeof(ObjectType);
if (!dtype.IsGenericType)
return (ObjectType)Convert.ChangeType(obj, dtype);
if (dtype.GetGenericTypeDefinition() == typeof(Nullable<>))
return (ObjectType)Convert.ChangeType(obj, Nullable.GetUnderlyingType(dtype));
throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", obj.GetType().FullName, dtype.FullName));
}
}
/// <summary>
/// 实体映射帮助类
/// </summary>
/// <typeparam name="T"></typeparam>
public class EntityHelper<T>
where T : BaseEntity, new()
{
public static List<T> SetValues(IDataReader reader)
{
try
{
List<T> list = new List<T>();
Dictionary<string, int> lowerFields = new Dictionary<string, int>();
bool first = true;
while (reader.Read())
{
if (first)
{
for (int i = 0; i < reader.FieldCount; i++)
{
lowerFields.Add(reader.GetName(i).ToLower(), i);
}
first = false;
}
T item = new T();
item.SetEntity(reader, lowerFields);
list.Add(item);
}
return list;
}
finally
{
reader.Close();
}
}
public static T SetValue(IDataReader reader)
{
try
{
Dictionary<string, int> lowerFieldIndex = new Dictionary<string, int>();
bool first = true;
if (reader.Read())
{
if (first)
{
for (int i = 0; i < reader.FieldCount; i++)
{
lowerFieldIndex.Add(reader.GetName(i).Trim().ToLower(), i);
}
first = false;
}
T item = new T();
item.SetEntity(reader, lowerFieldIndex);
return item;
}
}
finally
{
reader.Close();
}
return null;
}
}
/// <summary>
/// reader映射帮助类
/// </summary>
public class EntityHelper
{
public static T To<T>(object reader) where T : IConvertible
{
if (reader == DBNull.Value)
{
return default(T);
}
string type = typeof(T).Name;
TypeCode typecode;
if (!Enum.TryParse(type, out typecode))
throw new ArgumentException("could not convert!");
return (T)Convert.ChangeType(reader, typecode);
}
}
/// <summary>
/// 数据库操作基类
/// </summary>
public abstract class DBHelper : IDisposable
{
private IDbConnection dbconnection;
/// <summary>
/// 实现构造函数
/// </summary>
/// <param name="conn">必须是集成自ConnString的类</param>
public DBHelper(string connstring)
{
dbconnection = CreateConnection(connstring);
}
public void Dispose()
{
if (dbconnection != null)
{
dbconnection.Close();
}
}
protected abstract IDbConnection CreateConnection(string connstring);
protected abstract IDbDataParameter CreateParameter();
protected abstract IDbDataAdapter CreateDataAdapter();
/// <summary>
///
/// </summary>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
/// <returns></returns>
public void ExecuteNonQuery(string cmdText, params IDbDataParameter[] commandParameters)
{
ExecuteNonQuery(CommandType.Text, cmdText, commandParameters);
}
/// <summary>
///
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
/// <returns></returns>
public void ExecuteNonQuery(CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
{
try
{
using (IDbCommand _dbcommand = dbconnection.CreateCommand())
{
PrepareCommand(_dbcommand, null, cmdType, cmdText, commandParameters);
_dbcommand.ExecuteNonQuery();
_dbcommand.Parameters.Clear();
}
}
finally
{
dbconnection.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
/// <returns></returns>
public IDataReader ExecuteReader(string cmdText, params IDbDataParameter[] commandParameters)
{
return ExecuteReader(CommandType.Text, cmdText, commandParameters);
}
/// <summary>
///
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
/// <returns></returns>
public IDataReader ExecuteReader(CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
{
using (IDbCommand _dbcommand = dbconnection.CreateCommand())
{
PrepareCommand(_dbcommand, null, cmdType, cmdText, commandParameters);
IDataReader rdr = _dbcommand.ExecuteReader(CommandBehavior.CloseConnection);
_dbcommand.Parameters.Clear();
return rdr;
}
}
/// <summary>
///
/// </summary>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
/// <returns></returns>
public object ExecuteScalar(string cmdText, params IDbDataParameter[] commandParameters)
{
return ExecuteScalar(CommandType.Text, cmdText, commandParameters);
}
/// <summary>
///
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
/// <returns></returns>
public object ExecuteScalar(CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
{
try
{
using (IDbCommand _dbcommand = dbconnection.CreateCommand())
{
PrepareCommand(_dbcommand, null, cmdType, cmdText, commandParameters);
object obj = _dbcommand.ExecuteScalar();
_dbcommand.Parameters.Clear();
return obj;
}
}
finally
{
dbconnection.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
/// <returns></returns>
public DataTable ExecuteTable(CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
{
try
{
using (IDbCommand _dbcommand = dbconnection.CreateCommand())
{
PrepareCommand(_dbcommand, null, cmdType, cmdText, commandParameters);
using (DataSet st = new DataSet())
{
IDbDataAdapter ap = CreateDataAdapter();
ap.SelectCommand = _dbcommand;
ap.Fill(st);
_dbcommand.Parameters.Clear();
return st.Tables[0];
}
}
}
finally
{
dbconnection.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
/// <returns></returns>
public DataTable ExecuteTable(string cmdText, params IDbDataParameter[] commandParameters)
{
return ExecuteTable(CommandType.Text, cmdText, commandParameters);
}
public DataSet ExecuteDataSet(string cmdText, params IDbDataParameter[] commandParameters)
{
return ExecuteDataSet(CommandType.Text, cmdText, commandParameters);
}
public DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
{
try
{
using (IDbCommand _dbcommand = dbconnection.CreateCommand())
{
PrepareCommand(_dbcommand, null, cmdType, cmdText, commandParameters);
using (DataSet st = new DataSet())
{
IDbDataAdapter ap = CreateDataAdapter();
ap.SelectCommand = _dbcommand;
ap.Fill(st);
_dbcommand.Parameters.Clear();
return st;
}
}
}
finally
{
dbconnection.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="parameterName"></param>
/// <returns></returns>
public IDbDataParameter CreateParameter(string parameterName)
{
IDbDataParameter p = CreateParameter();
p.ParameterName = parameterName;
return p;
}
/// <summary>
///
/// </summary>
/// <param name="parameterName"></param>
/// <param name="value"></param>
/// <returns></returns>
public IDbDataParameter CreateParameter(string parameterName, object value)
{
IDbDataParameter p = CreateParameter();
p.ParameterName = parameterName;
p.Value = value ?? (object)DBNull.Value;
return p;
}
/// <summary>
///
/// </summary>
/// <param name="parameterName"></param>
/// <param name="dbType"></param>
/// <param name="size"></param>
/// <param name="value"></param>
/// <returns></returns>
public IDbDataParameter CreateParameter(string parameterName, DbType dbType, int size, object value)
{
IDbDataParameter p = CreateParameter();
p.ParameterName = parameterName;
p.DbType = dbType;
p.Size = size;
p.Value = value ?? (object)DBNull.Value;
return p;
}
/// <summary>
///
/// </summary>
/// <param name="parameterName"></param>
/// <param name="dbType"></param>
/// <param name="value"></param>
/// <returns></returns>
public IDbDataParameter CreateParameter(string parameterName, DbType dbType, object value)
{
IDbDataParameter p = CreateParameter();
p.ParameterName = parameterName;
p.DbType = dbType;
p.Value = value ?? (object)DBNull.Value;
return p;
}
/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
/// <param name="value"></param>
public void SetParameter(IDbDataParameter parameter, object value)
{
parameter.Value = value ?? (object)DBNull.Value;
}
/// <summary>
/// Prepare a command for execution
/// </summary>
/// <param name="cmd">IDbCommand Object</param>
/// <param name="trans">IDbTransaction Object</param>
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
/// <param name="cmdText">Command text, e.g. Select * from Products</param>
/// <param name="cmdParms">IDbDataParameters to use in the command</param>
private static void PrepareCommand(IDbCommand cmd, IDbTransaction trans, CommandType cmdType, string cmdText, IDbDataParameter[] cmdParms)
{
if (cmd.Connection.State != ConnectionState.Open)
{
cmd.Connection.Open();
}
cmd.CommandText = cmdText;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = cmdType;
if (cmdParms != null && cmdParms.Length > 0)
{
foreach (IDbDataParameter parm in cmdParms)
{
cmd.Parameters.Add(parm);
}
}
}
}
/// <summary>
/// mssql数据操作接口类
/// </summary>
public class MSSQLHelper : DBHelper
{
public MSSQLHelper(string connstring)
: base(connstring)
{
}
protected override IDbConnection CreateConnection(string connstring)
{
return new SqlConnection(connstring);
}
protected override IDbDataParameter CreateParameter()
{
return new SqlParameter();
}
protected override IDbDataAdapter CreateDataAdapter()
{
return new SqlDataAdapter();
}
}
/// <summary>
/// mysql数据操作接口类
/// </summary>
public class MYSQLHelper : DBHelper
{
public MYSQLHelper(string connstring)
: base(connstring)
{
}
protected override IDbConnection CreateConnection(string connstring)
{
return new MySqlConnection(connstring);
}
protected override IDbDataParameter CreateParameter()
{
return new MySqlParameter();
}
protected override IDbDataAdapter CreateDataAdapter()
{
return new MySqlDataAdapter();
}
}
}