-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
459 lines (391 loc) · 19.6 KB
/
Program.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
452
453
454
455
456
457
458
459
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
static class Program
{
static HashSet<string> hsh = new HashSet<string>();
/// <summary>
/// Creates a SQL script that creates a table where the columns matches that of the specified DataTable.
/// </summary>
public static string BuildCreateTableScript(DataTable Table)
{
StringBuilder result = new StringBuilder();
if (!hsh.Contains(Table.TableName))
{
result.AppendFormat("IF OBJECT_ID('dbo.[{0}]', 'U') IS NOT NULL DROP TABLE dbo.[{0}];\r\n", Table.TableName);
hsh.Add(Table.TableName);
}
result.AppendFormat("CREATE TABLE [{1}] ({0} ", Environment.NewLine, Table.TableName);
bool FirstTime = true;
foreach (DataColumn column in Table.Columns.OfType<DataColumn>())
{
if (FirstTime) FirstTime = false;
else
result.Append(" ,");
result.AppendFormat("[{0}] {1} {2} {3}",
column.ColumnName, // 0
GetSQLTypeAsString(column.DataType), // 1
column.AllowDBNull ? "NULL" : "NOT NULL", // 2
Environment.NewLine // 3
);
}
result.AppendFormat(") ON [PRIMARY]{0}{0}{0}", Environment.NewLine);
// Build an ALTER TABLE script that adds keys to a table that already exists.
if (Table.PrimaryKey.Length > 0)
result.Append(BuildKeysScript(Table));
return result.ToString();
}
/// <summary>
/// Builds an ALTER TABLE script that adds a primary or composite key to a table that already exists.
/// </summary>
private static string BuildKeysScript(DataTable Table)
{
// Already checked by public method CreateTable. Un-comment if making the method public
// if (Helper.IsValidDatatable(Table, IgnoreZeroRows: true)) return string.Empty;
if (Table.PrimaryKey.Length < 1) return string.Empty;
StringBuilder result = new StringBuilder();
if (Table.PrimaryKey.Length == 1)
result.AppendFormat("ALTER TABLE {1}{0} ADD PRIMARY KEY ({2}){0}GO{0}{0}", Environment.NewLine, Table.TableName, Table.PrimaryKey[0].ColumnName);
else
{
List<string> compositeKeys = Table.PrimaryKey.OfType<DataColumn>().Select(dc => dc.ColumnName).ToList();
string keyName = compositeKeys.Aggregate((a, b) => a + b);
string keys = compositeKeys.Aggregate((a, b) => string.Format("{0}, {1}", a, b));
result.AppendFormat("ALTER TABLE {1}{0}ADD CONSTRAINT pk_{3} PRIMARY KEY ({2}){0}GO{0}{0}", Environment.NewLine, Table.TableName, keys, keyName);
}
return result.ToString();
}
static TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
/// <summary>
/// Returns the SQL data type equivalent, as a string for use in SQL script generation methods.
/// </summary>
private static string GetSQLTypeAsString(Type DataType)
{
string pcase = textInfo.ToTitleCase(DataType.Name);
switch (pcase)
{
case "Boolean": return "[bit]";
case "Char": return "[char]";
case "SByte": return "[tinyint]";
case "Int16": return "[smallint]";
case "Int32": return "[int]";
case "Int64": return "[bigint]";
case "Byte": return "[tinyint] UNSIGNED";
case "UInt16": return "[smallint] UNSIGNED";
case "UInt32": return "[int] UNSIGNED";
case "UInt64": return "[bigint] UNSIGNED";
case "Single": return "[float]";
case "Double": return "[float]";
case "Decimal": return "[decimal]";
case "DateTime": return "[datetime]";
case "Guid": return "[uniqueidentifier]";
case "Object": return "[variant]";
case "String": return "[nvarchar](4000)";
default: return "[nvarchar](MAX)";
}
}
public static List<DataTable> access(string filePath)
{
// string baglantiCumlesi = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Database1.accdb;Persist Security Info=False;";
string strConn;
if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".accdb")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";;Persist Security Info=False;";
else
strConn = "??";
OleDbConnection conn = new OleDbConnection(strConn);
List<DataTable> dtt = new List<DataTable>();
try
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string fn = System.IO.Path.GetFileNameWithoutExtension(filePath);
//Looping Total Sheet of Xl File
foreach (DataRow schemaRow in schemaTable.Rows)
{
DataTable dtexcel = new DataTable();
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
string query = "SELECT * FROM [" + sheet + "]";
OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn);
dtexcel.Locale = CultureInfo.CurrentCulture;
if (dtexcel.Rows.Count > 0)
{
daexcel.Fill(0, 1000, dtexcel);
dtexcel.TableName = fn + '_' + sheet;
dtt.Add(dtexcel);
}
}
}
}
finally
{
conn.Close();
}
return dtt;
}
private static OleDbConnection _conn = null;
public static OleDbConnection GetOleConn(string filePath)
{
if (_conn != null) return _conn;
bool hasHeaders = true;
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
_conn = new OleDbConnection(strConn);
_conn.Open();
return _conn;
}
public static List<DataTable> exceldata(string filePath, bool assingle = false)
{
OleDbConnection conn = GetOleConn(filePath);
List<DataTable> dtt = new List<DataTable>();
try
{
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string fn = System.IO.Path.GetFileNameWithoutExtension(filePath);
DataTable dtexcelSingle = new DataTable(fn);
dtexcelSingle.Locale = CultureInfo.CurrentCulture;
//Looping Total Sheet of Xl File
foreach (DataRow schemaRow in schemaTable.Rows)
{
//Looping a first Sheet of Xl File
// schemaRow = schemaTable.Rows[0];
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
string query = "SELECT * FROM [" + sheet + "]";
OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn);
if (assingle)
{
daexcel.Fill(dtexcelSingle);
}
else
{
DataTable dtexcel = new DataTable();
dtexcel.Locale = CultureInfo.CurrentCulture;
daexcel.Fill(0, 1000, dtexcel);
if (dtexcel.Rows.Count > 0)
{
// dtexcel.Locale = CultureInfo.CurrentCulture;
// dtexcel.TableName = fn + '_' + sheet;
dtexcel.TableName = sheet;
dtt.Add(dtexcel);
}
}
}
}
if (assingle) dtt.Add(dtexcelSingle);
}
finally
{
// conn.Close();
}
return dtt;
}
public enum Ftype
{
xls, csv, mdb
}
public static char[] sepchars = { '\t', ',', '|', ';' };
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
bool singletable = false;
char csvSeperatorChar = '\t';
Console.WriteLine("HolyOne csv 2 mssql importer, by Aytek Ustundag, www.tahribat.com");
if (args.Length < 2)
{
Console.WriteLine("USAGE:");
Console.WriteLine("csv2mssql.exe <ConnectionString> <Filename>");
Console.WriteLine("EXAMPLE:");
Console.WriteLine(@"csv2mssql.exe ""Data Source=(local);Initial Catalog=dbname;Integrated Security=SSPI"" ""data.csv""");
Console.WriteLine(@"csv2mssql.exe ""Data Source=(local);Initial Catalog=dbname;Integrated Security=SSPI"" ""excelfile.xlsx""");
Console.WriteLine("");
return;
}
string filename = args[1];
string connstr = args[0];
if (args.Length >= 3)
singletable = args[2].Equals("/single", StringComparison.InvariantCultureIgnoreCase);
if (!System.IO.File.Exists(filename))
{
Console.WriteLine(@"Input file ""{0}"" not found", filename);
return;
}
//string baglantiCumlesi = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Database1.accdb;Persist Security Info=False;";
// foreach (string filename in args)
{
//string filename = "x.csv";
/* string bulk_data_filename = "x.csv";
StreamReader file = new StreamReader(bulk_data_filename);
CsvReader csv = new CsvReader(file, true, ',');
SqlBulkCopy copy = new SqlBulkCopy(conn);
copy.DestinationTableName = System.IO.Path.GetFileNameWithoutExtension(bulk_data_filename);
copy.WriteToServer(csv);
*/
string tablename = System.IO.Path.GetFileNameWithoutExtension(filename);
string ext = System.IO.Path.GetExtension(filename);
List<DataTable> dts = new List<DataTable>();
Ftype mode = Ftype.csv;
if (ext.Equals(".xls", StringComparison.InvariantCultureIgnoreCase) || ext.Equals(".xlsx", StringComparison.InvariantCultureIgnoreCase))
{
mode = Ftype.xls;
dts = exceldata(filename, singletable);
}
else
if (ext.Equals(".accdb", StringComparison.InvariantCultureIgnoreCase) || ext.Equals(".mdb", StringComparison.InvariantCultureIgnoreCase))
{
mode = Ftype.mdb;
dts = access(filename);
}
else
{
//csv mode
using (var csvStreamReader = new StreamReader(filename))
using (LumenWorks.Framework.IO.Csv.CsvReader csvReader = new LumenWorks.Framework.IO.Csv.CsvReader(csvStreamReader, true))
{
DataTable dt = new DataTable(tablename);
int tmpcnt = 0;
string myline = "";
while (tmpcnt < 100)
{
myline = csvStreamReader.ReadLine();
if (myline == null) break;
if (tmpcnt == 0)
{//first line
Dictionary<char, int> charcounter = new Dictionary<char, int>();
foreach (char cx in sepchars)
{
int chcount = myline.Count(f => f == cx);
charcounter.Add(cx, chcount);
}
charcounter = charcounter.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
csvSeperatorChar = charcounter.First().Key;
Console.WriteLine("Resolving seperator char:" + ((csvSeperatorChar == '\t') ? "<TAB>" : (csvSeperatorChar.ToString())));
}
tmpcnt++;
string[] cells = myline.Replace(@"""", "").Split(new char[] { csvSeperatorChar });
while (dt.Columns.Count < cells.Length)
{
dt.Columns.Add(cells[dt.Columns.Count]);
}
dt.Rows.Add(cells);
}
// dt.Load(csvReader);
dts.Add(dt);
}
}
if (dts.Count == 0)
{
Console.WriteLine("No data table found to import");
return;
}
foreach (DataTable dt in dts)
{
string str = BuildCreateTableScript(dt);
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
try
{
using (SqlCommand cmd = new SqlCommand(str, conn))
{
cmd.ExecuteNonQuery();
}
}
catch (Exception exx)
{
Console.WriteLine("\tWarning:" + exx.Message + " ,Appending...");
}
DataTable dtexcelSingle = new DataTable(tablename);
SqlTransaction transaction = conn.BeginTransaction();
try
{
int batchsize = 0;
Console.WriteLine("Importing table {0}", dt.TableName);
if (mode == Ftype.csv)
using (StreamReader file = new StreamReader(filename))
{
using (LumenWorks.Framework.IO.Csv.CsvReader csv = new LumenWorks.Framework.IO.Csv.CsvReader(file, true, csvSeperatorChar))
// using (CsvReader csv = new CsvReader(file, true, csvSeperatorChar,'\0','\0','#', ValueTrimmingOptions.None))
{
csv.SkipEmptyLines = true;
csv.SupportsMultiline = true;
csv.MissingFieldAction = LumenWorks.Framework.IO.Csv.MissingFieldAction.ReplaceByNull;
// csv.DefaultParseErrorAction = ParseErrorAction.AdvanceToNextLine;
SqlBulkCopy copy = new SqlBulkCopy(conn, SqlBulkCopyOptions.KeepIdentity, transaction);
// SqlBulkCopy copy = new SqlBulkCopy(connstr, SqlBulkCopyOptions.KeepIdentity );
copy.BulkCopyTimeout = 9999999;
copy.DestinationTableName = tablename;
copy.WriteToServer(csv);
batchsize = copy.RowsCopiedCount();
transaction.Commit();
}
}
else
{
string sheet = dt.TableName;
if (sheet.EndsWith("_"))
{
continue;
}
OleDbConnection oconn = GetOleConn(filename);
try
{
{
{
string query = "SELECT * FROM [" + sheet + "]";
OleDbDataAdapter daexcel = new OleDbDataAdapter(query, oconn);
using (OleDbCommand cmd = new OleDbCommand(query, oconn))
{
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
SqlBulkCopy copy = new SqlBulkCopy(conn, SqlBulkCopyOptions.KeepIdentity, transaction);
// SqlBulkCopy copy = new SqlBulkCopy(connstr, SqlBulkCopyOptions.KeepIdentity );
copy.BulkCopyTimeout = 9999999;
copy.DestinationTableName = dt.TableName;
copy.WriteToServer(rdr);
batchsize = copy.RowsCopiedCount();
transaction.Commit();
}
}
}
}
}
finally
{
}
}
Console.WriteLine("Finished inserting {0} records.", batchsize);
}
catch (Exception ex)
{
transaction.Rollback();
Console.WriteLine("Err:" + ex.Message);
}
finally
{
conn.Close();
}
}
}
// Console.ReadKey();
}
}
}