-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneralDataAccess.cs
321 lines (298 loc) · 12.6 KB
/
GeneralDataAccess.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
using System;
using System.Data;
using System.Data.SqlClient;
using PlanetaryDBLib.Logger;
namespace PlanetaryDBLib.DataAccess
{
public class SQLOps
{
//FJC = First Join Column
//SJC = Second Join Column
public static DataTable Join(DataTable first, DataTable second, DataColumn[] fjc, DataColumn[] sjc)
{
//Create Empty Table
var table = new DataTable("Join");
// Use a DataSet to leverage DataRelation
using (var ds = new DataSet())
{
//Add Copy of Tables
ds.Tables.AddRange(new[] {first.Copy(), second.Copy()});
//Identify Joining Columns from first
var parentcolumns = new DataColumn[fjc.Length];
for (int i = 0; i < parentcolumns.Length; i++)
{
parentcolumns[i] = ds.Tables[0].Columns[fjc[i].ColumnName];
}
//Identify Joining Columns from second
var childcolumns = new DataColumn[sjc.Length];
for (int i = 0; i < childcolumns.Length; i++)
{
childcolumns[i] = ds.Tables[1].Columns[sjc[i].ColumnName];
}
//Create DataRelation
var r = new DataRelation(string.Empty, parentcolumns, childcolumns, false);
ds.Relations.Add(r);
//Create Columns for JOIN table
for (int i = 0; i < first.Columns.Count; i++)
{
table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);
}
for (int i = 0; i < second.Columns.Count; i++)
{
//Beware Duplicates
if (!table.Columns.Contains(second.Columns[i].ColumnName))
table.Columns.Add(second.Columns[i].ColumnName, second.Columns[i].DataType);
else
table.Columns.Add(second.Columns[i].ColumnName + "_Second", second.Columns[i].DataType);
}
//Loop through first table
table.BeginLoadData();
foreach (DataRow firstrow in ds.Tables[0].Rows)
{
//Get "joined" rows
DataRow[] childrows = firstrow.GetChildRows(r);
if (childrows != null && childrows.Length > 0)
{
object[] parentarray = firstrow.ItemArray;
foreach (DataRow secondrow in childrows)
{
object[] secondarray = secondrow.ItemArray;
var joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
}
table.EndLoadData();
}
return table;
}
public static DataTable Join(DataTable first, DataTable second, DataColumn fjc, DataColumn sjc)
{
return Join(first, second, new[] {fjc}, new[] {sjc});
}
public static DataTable Join(DataTable first, DataTable second, string fjc, string sjc)
{
return Join(first, second, new[] {first.Columns[fjc]}, new[] {second.Columns[sjc]});
}
}
public class DataAccess
{
//private
//**************************************
//* Purpose: Accessing SQL database
//*Methods:
//*GetDataSet
//*RunProc
//*GetDataReader
//*GetDataView
//* *************************************
public bool BulkCopyData(string strConnect, string bulkCopyTable, SqlDataReader sourcedata)
{
//********************************
//* Purpose: Performs bulk copy from one data source to another
//* Input parameters:
//* bulkCopytable ---the target table to bulk data into
//* sourcedata ---the SqlDataReader holding the data to bulk insert
//* Returns :
//* nothing
//**************************************************
bool err = false;
//create source connection
var sourceConnection = new SqlConnection(strConnect);
sourceConnection.Open();
// Create SqlBulkCopy
var bulkData = new SqlBulkCopy(strConnect, SqlBulkCopyOptions.TableLock)
{
BatchSize = 1000,
BulkCopyTimeout = 360,
DestinationTableName = bulkCopyTable
};
//set number of records to process in one batch
//set timeout for a single bulk process
// Set destination table name
try
{
bulkData.WriteToServer(sourcedata);
}
catch (Exception e)
{
err = true;
PLOG.Write(e.Message, 1);
bulkData.Close();
sourceConnection.Close();
sourceConnection.Dispose();
}
finally
{
bulkData.Close();
sourceConnection.Close();
sourceConnection.Dispose();
}
return err;
}
public DataSet GetDataSet(string strConnect, string[] procName, string[] dataTable)
{
//********************************
//* Purpose: Returns Dataset for one or multi datatables
//* Input parameters:
//*procName() ---StoredProcedures name in array
//*dataTable()---dataTable name in array
//* Returns :
//*DataSet Object contains data
//**************************************************
SqlDataAdapter dadEorder;
var dstEorder = new DataSet();
var conn = new SqlConnection(strConnect);
try
{
int intCnt = procName.GetUpperBound(0);
// if one datatable and SP
if (intCnt == 0)
{
dadEorder = new SqlDataAdapter(procName[0], conn);
dadEorder.Fill(dstEorder, dataTable[0]);
}
// more than one datatable and one SP
else
{
conn.Open();
//add first data table and first SP
dadEorder = new SqlDataAdapter(procName[0], conn);
dadEorder.Fill(dstEorder, dataTable[0]);
// add second datatable and second SP onwards
for (int i = 1; i < (intCnt + 1); i++)
{
dadEorder.SelectCommand = new SqlCommand(procName[i], conn);
dadEorder.Fill(dstEorder, dataTable[i]);
}
}
return dstEorder;
}
catch (Exception e)
{
PLOG.Write(e.Message, 1);
throw;
}
}
public void RunProc(string strConnect, string procName)
{
//****************************************
//* Purpose: Executing Stored Procedures where UPDATE, INSERT
//*and DELETE statements are expected but does not
//*work for select statement is expected.
//* Input parameters:
//*procName ---StoredProcedures name
//* Returns :
//*nothing
//* ***************************************
string strCommandText = procName;
//create a new Connection object using the connection string
var objConnect = new SqlConnection(strConnect);
//create a new Command using the CommandText and Connection object
var objCommand = new SqlCommand(strCommandText, objConnect) {CommandTimeout = 3600};
//set the timeout in seconds
try
{
objConnect.Open();
objCommand.ExecuteNonQuery();
}
catch (Exception e)
{
PLOG.Write(e.Message, 1);
throw;
}
}
public SqlDataReader GetDataReader(string strConnect, string procName)
{
//**************************************
//* Purpose: Getting DataReader for the given Procedure
//* Input parameters:
//*procName ---StoredProcedures name
//* Returns :
//*DataReader contains data
//* ************************************
string strCommandText = procName;
SqlDataReader objDataReader;
//create a new Connection object using the connection string
var objConnect = new SqlConnection(strConnect);
//create a new Command using the CommandText and Connection object
var objCommand = new SqlCommand(strCommandText, objConnect) {CommandTimeout = 3600};
//set the timeout in seconds
try
{
//open the connection and execute the command
objConnect.Open();
//objDataAdapter.SelectCommand = objCommand
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
PLOG.Write(e.Message, 1);
throw;
}
return objDataReader;
}
public DataView GetDataView(string strConnect, string procName, string dataSetTable)
{
//*****************************************
//* Purpose: Getting DataReader for the given Procedure
//* Input parameters:
//* procName ---StoredProcedures name
//* dataSetTable--dataSetTable name sting
//* Returns :
//* DataView contains data
//* ****************************************
string strCommandText = procName;
DataView objDataView;
//create a new Connection object using the connection string
var objConnect = new SqlConnection(strConnect);
//create a new Command using the CommandText and Connection object
var objCommand = new SqlCommand(strCommandText, objConnect);
//declare a variable to hold a DataAdaptor object
var objDataAdapter = new SqlDataAdapter();
var objDataSet = new DataSet(dataSetTable);
//set the timeout in seconds
objCommand.CommandTimeout = 3600;
try
{
//open the connection and execute the command
objConnect.Open();
objDataAdapter.SelectCommand = objCommand;
objDataAdapter.Fill(objDataSet);
objDataView = new DataView(objDataSet.Tables[0]);
//objDataReader = objCommand.ExecuteReader()
}
catch (Exception e)
{
PLOG.Write(e.Message, 1);
throw;
}
return objDataView;
}
public DataTable GetDataTable(string strConnect, string procName)
{
//********************************
//* Purpose: Returns DataTable for one datatables
//* Input parameters:
//*procName ---StoredProcedure or query name
//* Returns :
//*DataTable Object contains data
//**************************************************
using (var dstEorder = new DataTable())
{
using (var conn = new SqlConnection(strConnect))
{
PLOG.Write(procName);
using (var dadEorder = new SqlDataAdapter(procName, conn))
{
dadEorder.Fill(dstEorder);
}
// more than one datatable and one SP
}
return dstEorder;
}
}
}
}