forked from silahian/VisualHFT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelperDB.cs
375 lines (325 loc) · 12.7 KB
/
HelperDB.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
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Configuration;
using System.Text.RegularExpressions;
using System.IO;
using LumenWorks.Framework.IO.Csv;
public static class HelperDB
{
public static DataTable ToDataTable<T>(this IEnumerable<T> collection)
{
DataTable dt = new DataTable("DataTable");
Type t = typeof(T);
System.Reflection.PropertyInfo[] pia = t.GetProperties();
//Inspect the properties and create the columns in the DataTable
foreach (System.Reflection.PropertyInfo pi in pia)
{
Type ColumnType = pi.PropertyType;
if ((ColumnType.IsGenericType))
{
ColumnType = ColumnType.GetGenericArguments()[0];
}
dt.Columns.Add(pi.Name, ColumnType);
}
//Populate the data table
foreach (T item in collection)
{
DataRow dr = dt.NewRow();
dr.BeginEdit();
foreach (System.Reflection.PropertyInfo pi in pia)
{
if (pi.GetValue(item, null) != null)
{
dr[pi.Name] = pi.GetValue(item, null);
}
}
dr.EndEdit();
dt.Rows.Add(dr);
}
return dt;
}
public static DataTable csvToDataTableFromFile(string filePath)
{
return csvToDataTableFromFile(filePath, true, ',');
}
public static DataTable csvToDataTableFromFile(string filePath, bool isRowOneHeader, char separator)
{
DataTable csvDataTable = new DataTable();
using (CsvReader csv = new CsvReader(new StreamReader(filePath), isRowOneHeader, separator, '"', '\\', '#', ValueTrimmingOptions.UnquotedOnly))
{
int fieldCount = csv.FieldCount;
//CREATE HEADERS
List<string> aHeaders = new List<string>();
if (isRowOneHeader)
{
foreach (string h in csv.GetFieldHeaders())
aHeaders.Add(h.Replace(" ", "_"));
}
else
{
for (int i = 0; i < fieldCount; i++)
aHeaders.Add("col" + i.ToString());
}
for (int i = 0; i < aHeaders.Count; i++)
csvDataTable.Columns.Add(aHeaders[i], typeof(string));
//ADD ROWS
try
{
while (csv.ReadNextRecord())
{
DataRow oRow = csvDataTable.NewRow();
for (int i = 0; i < fieldCount; i++)
{
try
{
oRow[i] = csv[i];
}
catch (Exception ex) { oRow[i] = ""; }
}
csvDataTable.Rows.Add(oRow);
}
}
catch { }
}
return csvDataTable;
}
public static DataTable csvToDataTable(string fileContent, bool isRowOneHeader)
{
return csvToDataTable(fileContent, isRowOneHeader, null, ",");
}
public static DataTable csvToDataTable(string fileContent, bool isRowOneHeader, List<Type> aColumnTypes)
{
return csvToDataTable(fileContent, isRowOneHeader, aColumnTypes, ",");
}
public static DataTable csvToDataTable(string fileContent, bool isRowOneHeader, List<Type> aColumnTypes, string separator)
{
string file = fileContent;
TextReader sr = new StringReader(file);
DataTable csvDataTable = new DataTable();
using (CsvReader csv = new CsvReader(sr, isRowOneHeader, separator.ToCharArray()[0]))
{
csv.MissingFieldAction = MissingFieldAction.ReplaceByEmpty;
csv.SkipEmptyLines = true;
int fieldCount = csv.FieldCount;
//CREATE HEADERS
List<string> aHeaders = new List<string>();
if (isRowOneHeader)
{
foreach (string h in csv.GetFieldHeaders())
aHeaders.Add(h.Replace(" ", "_"));
}
else
{
for (int i = 0; i < fieldCount; i++)
aHeaders.Add("col" + i.ToString());
}
for (int i = 0; i < aHeaders.Count; i++)
{
if (aColumnTypes == null)
csvDataTable.Columns.Add(aHeaders[i], typeof(string));
else
csvDataTable.Columns.Add(aHeaders[i], aColumnTypes[i]);
}
//ADD ROWS
try
{
while (csv.ReadNextRecord())
{
DataRow oRow = csvDataTable.NewRow();
for (int i = 0; i < fieldCount; i++)
{
try
{
oRow[i] = csv[i];
}
catch (Exception ex) { oRow[i] = ""; }
}
csvDataTable.Rows.Add(oRow);
}
}
catch { }
}
return csvDataTable;
//no try/catch - add these in yourselfs or let exception happen
//String[] csvData = File.ReadAllLines(HttpContext.Current.Server.MapPath(file));
String[] csvData = file.Split(Convert.ToChar("\n"));
//if no data in file ‘manually' throw an exception
if (csvData.Length == 0)
{
throw new Exception("CSV File Appears to be Empty");
}
String[] headings = null;
headings = Regex.Split(csvData[0], separator);
int index = 0; //will be zero or one depending on isRowOneHeader
if (isRowOneHeader) //if first record lists headers
{
index = 1; //so we won't take headings as data
//for each heading
for (int i = 0; i < headings.Length; i++)
{
//replace spaces with underscores for column names
headings[i] = headings[i].Replace(" ", "_");
headings[i] = headings[i].Replace("\r", "");
if (separator == "\"" || separator == "\",\"")
headings[i] = headings[i].Replace("\"", "").Replace(",\r", "");
//add a column for each heading
if (aColumnTypes == null)
csvDataTable.Columns.Add(headings[i], typeof(string));
else
csvDataTable.Columns.Add(headings[i], aColumnTypes[i]);
}
}
else //if no headers just go for col1, col2 etc.
{
for (int i = 0; i < headings.Length; i++)
{
//create arbitary column names
csvDataTable.Columns.Add("col" + (i + 1).ToString(), typeof(string));
}
}
//populate the DataTable
for (int i = index; i < csvData.Length; i++)
{
if (csvData[i] != "")
{
try
{
//test data
if (csvData[i].Split(separator.ToCharArray()[0]).Length != headings.Length && separator == ",")
continue;
//create new rows
DataRow row = csvDataTable.NewRow();
string memoField = "";
for (int j = 0; j < headings.Length; j++)
{
string data = "";
if (separator == "\"" || separator == "\",\"")
data = Regex.Split(csvData[i], separator)[j].Replace("\"", "").Replace(",\r", "");
else if (csvData[i].Substring(0, 1) == "\"")
{
int iPos = csvData[i].IndexOf("\"");
if (iPos > -1) //because a memo field exists (between quotes)
{
if (memoField == "")
memoField = csvData[i].Substring(iPos, csvData[i].IndexOf("\"", iPos + 1) - iPos).Replace("\"", "");
csvData[i] = csvData[i].Replace("\"" + memoField + "\"", "#memo");
data = Regex.Split(csvData[i], separator)[j];
data = data.Replace("#memo", memoField);
}
else
data = Regex.Split(csvData[i], separator)[j];
}
else
data = Regex.Split(csvData[i], separator)[j];
//fill them
if (csvDataTable.Columns[j].DataType == typeof(int))
data = data.ToInt().ToString();
if (csvDataTable.Columns[j].DataType == typeof(double))
data = data.ToDouble().ToString();
if (csvDataTable.Columns[j].DataType == typeof(DateTime))
data = data.ToDateTime().ToString();
row[j] = data.Replace("\r", "") ;
}
//add rows to over DataTable
csvDataTable.Rows.Add(row);
}
catch (Exception ex)
{
throw ex;
}
}
}
//return the CSV DataTable
return csvDataTable;
}
public static SqlDataReader ExecuteDR(string Query, int _timeOut)
{
if (ConfigurationManager.AppSettings["ConnectionString"] == null)
return null;
string sConnectionString = getConnectionString();
SqlConnection oConn = new SqlConnection(sConnectionString);
SqlDataReader dr = null;
SqlCommand oComm = new SqlCommand(Query);
oConn.Open();
oComm.Connection = oConn;
oComm.CommandTimeout = _timeOut;
try
{
dr = oComm.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
catch (Exception ex)
{
return null;
}
}
public static DataSet ExecuteSQL(string Query, int _timeOut)
{
if (ConfigurationManager.AppSettings["ConnectionString"] == null)
return null;
string sConnectionString = getConnectionString();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(Query, sConnectionString);
da.SelectCommand.CommandTimeout = _timeOut;
try
{
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
return null;
}
}
public static DataSet ExeuteSQL(string Query)
{
return ExecuteSQL(Query, 240);
}
public static void ExecuteNonQuery(string Query)
{
if (ConfigurationManager.AppSettings["ConnectionString"] == null)
return;
string sConnectionString = getConnectionString();
using (SqlConnection oConn = new SqlConnection(sConnectionString))
{
oConn.Open();
SqlCommand oComm = new SqlCommand(Query, oConn);
oComm.ExecuteNonQuery();
}
}
public static object ExecuteScalar(string Query)
{
if (ConfigurationManager.AppSettings["ConnectionString"] == null)
return -1;
object oRet = null;
string sConnectionString = getConnectionString();
SqlConnection oConn = new SqlConnection(sConnectionString);
oConn.Open();
SqlCommand oComm = new SqlCommand(Query, oConn);
oRet = oComm.ExecuteScalar();
oConn.Close();
return oRet;
}
private static string getConnectionString()
{
string sRet = "";
sRet = ConfigurationManager.AppSettings["ConnectionString"].ToString();
/*try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = "EXECONFIG_PATH" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
AppSettingsSection sectionApps = config.GetSection("appSettings") as AppSettingsSection;
if (!sectionApps.SectionInformation.IsProtected)
{
sectionApps.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();
}
if (sectionApps.Settings["ConnectionString"] != null)
sRet = sectionApps.Settings["ConnectionString"].Value;
}
catch { sRet = ConfigurationManager.AppSettings["ConnectionString"].ToString(); }*/
return sRet;
}
}