-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLConnector.cs
More file actions
451 lines (436 loc) · 18.3 KB
/
Copy pathSQLConnector.cs
File metadata and controls
451 lines (436 loc) · 18.3 KB
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;
namespace Common
{
public class SQLConnector : IDisposable
{
/// <summary>
/// Internal SQLConnection object
/// </summary>
private SqlConnection Conn { get; set; }
/// <summary>
/// Change the max pool size, default to 100
/// </summary>
public int MaxPoolSize { get; set; } = 100;
/// <summary>
/// Property to increase the default SQL Statement execution timeout period of 30 seconds.
/// </summary>
public int TimeOut { get; set; } = 30000;
/// <summary>
/// Cleans the curren connection if it exists.
/// </summary>
private void CleanConnection()
{
if (Conn != null)
{
if (Conn.State != System.Data.ConnectionState.Closed)
Conn.Close();
Conn.Dispose();
}
Conn = null;
}
/// <summary>
/// Connects to a databse using the given connection string
/// </summary>
/// <param name="ConnectionString">The Connection string for the database</param>
private void ConnectToDB(string ConnectionString)
{
CleanConnection();
try
{
Conn = new SqlConnection(ConnectionString);
Conn.Open();
}
catch (Exception ex)
{
// Failed to onnect, ohh well, send the error to the calling app.
throw ex;
}
}
/// <summary>
/// Create a connection to the database using standard SQL authenication.
/// </summary>
/// <param name="Server">Server name</param>
/// <param name="Database">Database</param>
/// <param name="Username">Username</param>
/// <param name="Password">Password</param>
public SQLConnector(string Server, string Database, string Username, string Password)
{
if (Server == null || Database == null || Server.Trim() == "" || Database.Trim() == "")
{
throw new Exception("No valid server / database was specified");
}
string Connection = "Data Source=" + Server + ";Initial Catalog=" + Database + ";" + (Username == null || Username.Trim() == "" ? "" : " User ID=" + Username + ";") + (Password == null || Password.Trim() == "" ? "" : " Password=" + Password + ";") + " Max Pool Size=" + MaxPoolSize.ToString() + ";";
ConnectToDB(Connection);
}
/// <summary>
/// Create a connection to the database using a connection string.
/// </summary>
/// <param name="ConnectionString">Connection String</param>
public SQLConnector(string ConnectionString)
{
ConnectToDB(ConnectionString);
}
/// <summary>
/// Disposer, so we can use withing a using statement.
/// </summary>
public void Dispose()
{
CleanConnection();
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
/// <summary>
/// Executes an SQL statement with no results
/// </summary>
/// <param name="strSQL">SQL Statement</param>
public void ExecuteNRSQL(string strSQL)
{
SqlCommand cmdCommand = null;
cmdCommand = Conn.CreateCommand();
cmdCommand.CommandText = strSQL;
cmdCommand.CommandTimeout = TimeOut;
cmdCommand.ExecuteNonQuery();
cmdCommand = null;
}
/// <summary>
/// Executes an SQL statement, returns a datatable object
/// </summary>
/// <param name="strSQL">SQL Statement</param>
/// <returns>DataTable results from the SQL Statement</returns>
public DataTable ExecuteSQL(string strSQL)
{
DataTable Response;
using (SqlCommand Command = Conn.CreateCommand())
{
Command.CommandText = strSQL;
Command.CommandTimeout = TimeOut;
SqlDataReader Reader = Command.ExecuteReader();
Response = new DataTable();
Response.Load(Reader);
Reader = null;
}
return Response;
}
/// <summary>
/// Returns an easily serialised object dataset from a stored procedure.
/// </summary>
/// <param name="StoredProcName">Name of the stored procedure</param>
/// <param name="ReturnValue">Result from the stored procedure</param>
/// <param name="Args">List of arguments</param>
/// <returns>Returns an object collection</returns>
public object GetObjectSetFromSp(string StoredProcName, ref int ReturnValue, params object[] Args)
{
DataTableCollection dt = GetDataSetFromSP(StoredProcName, ref ReturnValue, Args);
return GetRt(ref dt);
}
/// <summary>
/// Returns an easily serialised object dataset from a stored procedure.
/// </summary>
/// <param name="StoredProcName">Name of the stored procedure</param>
/// <param name="Args">List of arguments</param>
/// <returns>Returns an object collection</returns>
public object GetObjectSetFromSp(string StoredProcName, params object[] Args)
{
int ReturnValue = 0;
return GetObjectSetFromSp(StoredProcName, ref ReturnValue, Args);
}
/// <summary>
/// Creates a collection of objects from a datatable collection.
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private object GetRt(ref DataTableCollection dt)
{
List<List<object>> rt = null;
List<object> rt2 = null;
try
{
rt = ConvertToObject(ref dt);
if (((rt != null)))
if (rt.Count == 1)
foreach (List<object> item in rt)
{
rt2 = item;
break;
}
}
catch (Exception ex)
{
throw ex;
}
dt = null;
if (rt2 != null)
return rt2;
return rt;
}
/// <summary>
/// Execute a stored procedure on the connected server, no results returned
/// </summary>
/// <param name="StoredProcName">Name of stored procedure</param>
/// <param name="ReturnValue">Result from the stored procedure</param>
/// <param name="Args">List of arguments</param>
public void ExecuteSP(string StoredProcName, ref int ReturnValue, params object[] Args)
{
SqlCommand dbCommand = null;
SqlParameter parameter = null;
long lngCounter = 0;
try
{
dbCommand = new SqlCommand(StoredProcName, Conn)
{
CommandType = CommandType.StoredProcedure,
CommandTimeout = TimeOut
};
LoadParams(ref dbCommand);
lngCounter = 0;
// Find cached parameters
if ((dbCommand.Parameters.Count > 0))
foreach (SqlParameter parameter_loopVariable in dbCommand.Parameters)
{
parameter = parameter_loopVariable;
if (parameter.Direction != ParameterDirection.ReturnValue)
{
if (Args.Length > lngCounter)
parameter.Value = Args[lngCounter] ?? DBNull.Value;
lngCounter = lngCounter + 1;
}
}
// Shove indataSet
dbCommand.ExecuteNonQuery();
// Retrieve the Result?
foreach (SqlParameter parameter_loopVariable in dbCommand.Parameters)
{
parameter = parameter_loopVariable;
if ((parameter.Direction == ParameterDirection.ReturnValue))
{
ReturnValue = (int)parameter.Value;
break;
}
}
// Exit
}
catch (Exception ex)
{
throw ex;
}
// Clean up
dbCommand = null;
}
/// <summary>
/// Execute a stored procedure on the connected server, no results returned
/// </summary>
/// <param name="StoredProcName">Name of stored procedure</param>
/// <param name="Args">List of arguments</param>
public void ExecuteSP(string StoredProcName, params object[] Args)
{
int ReturnValue = 0;
ExecuteSP(StoredProcName, ref ReturnValue, Args);
}
/// <summary>
/// Executes a stored procedure from the current connection, returning a DataTableCollecion
/// </summary>
/// <param name="StoredProcName">Name of stored procedure</param>
/// <param name="ReturnValue">Result from the stored procedure</param>
/// <param name="Args">List of arguments</param>
/// <returns>DataTableCollection of the stored procedure results</returns>
public DataTableCollection GetDataSetFromSP(string StoredProcName, ref int ReturnValue, params object[] Args)
{
DataTableCollection functionReturnValue = null;
System.Data.SqlClient.SqlDataAdapter dbData = null;
System.Data.SqlClient.SqlCommand dbCommand = null;
System.Data.SqlClient.SqlParameter parameter = null;
long lngCounter = 0;
DataSet ds = null;
functionReturnValue = null;
try
{
dbCommand = new SqlCommand(StoredProcName, Conn)
{
CommandType = CommandType.StoredProcedure,
CommandTimeout = TimeOut
};
LoadParams(ref dbCommand);
lngCounter = 0;
// Search for previously cached parameters
if ((dbCommand.Parameters.Count > 0))
{
foreach (SqlParameter parameter_loopVariable in dbCommand.Parameters)
{
parameter = parameter_loopVariable;
if (parameter.Direction != ParameterDirection.ReturnValue)
{
if ((Args.Length > lngCounter))
parameter.Value = Args[lngCounter] ?? DBNull.Value;
lngCounter = lngCounter + 1;
}
}
}
// Execute what we need
dbData = new System.Data.SqlClient.SqlDataAdapter(dbCommand);
ds = new DataSet();
// Shove indataSet
dbData.Fill(ds);
functionReturnValue = ds.Tables;
// Exit
}
catch (Exception)
{
functionReturnValue = null;
}
try
{
// Retrieve the Result?
foreach (SqlParameter parameter_loopVariable in dbCommand.Parameters)
{
parameter = parameter_loopVariable;
if ((parameter.Direction == ParameterDirection.ReturnValue))
{
ReturnValue = (int)parameter.Value;
break;
}
}
}
catch (Exception)
{
}
// Clean up
dbCommand = null;
dbData = null;
return functionReturnValue;
}
/// <summary>
/// Executes a stored procedure from the current connection, returning a DataTableCollecion
/// </summary>
/// <param name="StoredProcName">Name of stored procedure</param>
/// <param name="Args">List of arguments</param>
/// <returns>DataTableCollection of the stored procedure results</returns>
public DataTableCollection GetDataSetFromSP(string StoredProcName, params object[] Args)
{
int ReturnValue = 0;
return GetDataSetFromSP(StoredProcName, ref ReturnValue, Args);
}
/// <summary>
/// Gets a list of the stored procedure parameters, for returning information to clients on the
/// schema of an SP, placed into a cache for later use.
/// </summary>
/// <param name="Procedure">Nam of the procedure</param>
/// <returns></returns>
public List<Dictionary<string, object>> GetSPParams(string Procedure)
{
SqlCommand dbCommand = null;
List<Dictionary<string, object>> returnItem = new List<Dictionary<string, object>>();
try
{
dbCommand = new SqlCommand(Procedure, Conn)
{
CommandType = CommandType.StoredProcedure
};
LoadParams(ref dbCommand);
SqlParameterCollection Params = dbCommand.Parameters;
if (Params == null)
throw new Exception("Unable to find information on the provided stored procedure");
// Convert the list into something that can be returned
foreach (SqlParameter item in Params)
{
if (item.ParameterName.ToLower() != "@return_value")
{
Dictionary<string, object> header = new Dictionary<string, object>
{
{ "Name", item.ParameterName.ToString() },
{ "Type", item.DbType.ToString() },
{ "IsNullable", true },
{ "Size", item.Size }
};
returnItem.Add(header);
}
}
Params = null;
}
catch (Exception ex)
{
throw ex;
}
if (dbCommand != null)
dbCommand.Dispose();
dbCommand = null;
return returnItem;
}
// //////////////////////////////////////////////////////////////////////////////////////////////
// Caches and loads cached params for a stored procedure.... saves on the call backs
private static Dictionary<string, List<SqlParameter>> static_LoadParams_dicParams;
private void LoadParams(ref System.Data.SqlClient.SqlCommand dbCommand)
{
string strProcName = dbCommand.CommandText;
List<SqlParameter> dbparameter = null;
SqlParameter parameter = null;
if (static_LoadParams_dicParams == null)
static_LoadParams_dicParams = new Dictionary<string, List<SqlParameter>> { };
// Find this itme in the list
if (static_LoadParams_dicParams.ContainsKey(strProcName))
{
dbparameter = static_LoadParams_dicParams[strProcName];
if (dbparameter != null)
{
foreach (SqlParameter parameter_loopVariable in dbparameter)
{
parameter = parameter_loopVariable;
dbCommand.Parameters.Add(((ICloneable)parameter).Clone());
}
}
}
else
{
System.Data.SqlClient.SqlCommandBuilder.DeriveParameters(dbCommand);
// now record the params
foreach (SqlParameter parameter_loopVariable in dbCommand.Parameters)
{
parameter = parameter_loopVariable;
if ((dbparameter == null))
{
dbparameter = new List<SqlParameter> { };
}
dbparameter.Add((SqlParameter)((ICloneable)parameter).Clone());
}
static_LoadParams_dicParams.Add(strProcName, dbparameter);
}
}
// ///////////////////////////////////////////////////////////////////////////////////////
// Converts a datatable item to a serialisable object... we hope.
private List<List<object>> ConvertToObject(ref DataTableCollection dts)
{
List<object> rt = null;
List<List<object>> tabs = null;
List<string> columns = null;
// Exit on anomolies
if (dts == null || dts.Count == 0)
return null;
tabs = new List<List<object>> { };
foreach (DataTable dt in dts)
{
// Creates an Object which contains the header details.
columns = null;
columns = new List<string> { };
foreach (DataColumn column in dt.Columns)
columns.Add(column.ColumnName);
rt = new List<object> { };
foreach (DataRow row in dt.Rows)
{
Dictionary<string, object> header = new Dictionary<string, object>();
int intColumnCount = columns.Count - 1;
for (int intCounter = 0; intCounter <= intColumnCount; intCounter++)
header.Add(columns[intCounter], row[intCounter]);
rt.Add(header);
}
tabs.Add(rt);
}
return tabs;
}
}
}