-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilityBCP.cs
794 lines (670 loc) · 22 KB
/
UtilityBCP.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Text;
using System.Web;
using System.Xml;
namespace WordEngineering
{
/// <summary>UtilityBCPArgument</summary>
public class UtilityBCPArgument
{
///<summary>characterType</summary>
public bool characterType = UtilityBCP.CharacterType;
///<summary>keepIdentityValue</summary>
public bool keepIdentityValue = UtilityBCP.KeepIdentityValue;
///<summary>keepNullValue</summary>
public bool keepNullValue = UtilityBCP.KeepNullValue;
///<summary>characterType</summary>
public bool nativeType = UtilityBCP.NativeType;
///<summary>trustedConnection</summary>
public bool trustedConnection = UtilityBCP.TrustedConnection;
///<summary>batchSize</summary>
public int batchSize = UtilityBCP.BatchSize;
///<summary>maximumError</summary>
public int maximumError = UtilityBCP.MaximumError;
///<summary>directoryDatafile</summary>
public String directoryDatafile = UtilityBCP.DirectoryDataFile;
///<summary>databaseName</summary>
public String[] databaseName = null; //UtilityBCP.DatabaseName.Split( UtilityBCP.DelimiterCharArrayComma );
///<summary>password</summary>
public String password = null;
///<summary>serverName</summary>
public String serverName = null;
///<summary>tableName</summary>
public String[] tableName = null; //UtilityBCP.TableName.Split( UtilityBCP.DelimiterCharArrayComma );
///<summary>userName</summary>
public String userName = null;
///<summary>files</summary>
[DefaultCommandLineArgument(CommandLineArgumentType.MultipleUnique)]
public String[] files = null;
/// <summary>Constructor.</summary>
public UtilityBCPArgument():this
(
UtilityBCP.BatchSize,
UtilityBCP.CharacterType, //Character type
UtilityBCP.DatabaseName.Split( UtilityBCP.DelimiterCharArrayComma ),
UtilityBCP.DirectoryDataFile,
UtilityBCP.KeepIdentityValue,
UtilityBCP.KeepNullValue,
UtilityBCP.MaximumError,
UtilityBCP.NativeType, //Native type
null, //Password
null, //Server name
UtilityBCP.TableName.Split( UtilityBCP.DelimiterCharArrayComma ),
UtilityBCP.TrustedConnection, //Trusted connection
null //User name
)
{
}//public UtilityBCPArgument()
/// <summary>Constructor.</summary>
public UtilityBCPArgument
(
int batchSize,
bool characterType,
String[] databaseName,
String directoryDatafile,
bool keepIdentityValue,
bool keepNullValue,
int maximumError,
bool nativeType,
String password,
String serverName,
String[] tableName,
bool trustedConnection,
String userName
)
{
this.batchSize = batchSize;
this.characterType = characterType;
this.databaseName = databaseName;
this.directoryDatafile = directoryDatafile;
this.keepNullValue = keepNullValue;
this.keepIdentityValue = keepIdentityValue;
this.maximumError = maximumError;
this.nativeType = nativeType;
this.password = password;
this.serverName = serverName;
this.tableName = tableName;
this.trustedConnection = trustedConnection;
this.userName = userName;
}//public UtilityBCPArgument()
///<summary>Property.</summary>
///<value>BatchSize.</value>
public int BatchSize
{
get
{
return ( batchSize );
}//get
set
{
batchSize = value;
}//set
}//BatchSize
///<summary>Property.</summary>
///<value>CharacterType.</value>
public bool CharacterType
{
get
{
return ( characterType );
}//get
set
{
characterType = value;
}//set
}//CharacterType
///<summary>Property.</summary>
///<value>DirectoryDataFile.</value>
public String DirectoryDataFile
{
get
{
return ( directoryDatafile );
}//get
set
{
directoryDatafile = value.Trim();
if ( directoryDatafile == null || directoryDatafile == String.Empty )
{
directoryDatafile = UtilityBCP.DirectoryDataFile;
}//if ( directoryDatafile == null || directoryDatafile == String.Empty )
}//set
}//DirectoryDataFile
///<summary>Property.</summary>
///<value>DatabaseName.</value>
public String[] DatabaseName
{
get
{
return ( databaseName );
}//get
set
{
if ( value != null )
{
for ( int valueIndex = 0; valueIndex < value.Length; ++valueIndex )
{
value[valueIndex] = value[valueIndex].Trim();
}//for ( int valueIndex = 0; valueIndex <= value.Length; ++valueIndex )
}//if ( value != null )
this.databaseName = value;
}//set
}//DatabaseName
///<summary>Property.</summary>
///<value>KeepIdentityValue.</value>
public bool KeepIdentityValue
{
get
{
return ( keepIdentityValue );
}//get
set
{
keepIdentityValue = value;
}//set
}//KeepIdentityValue
///<summary>Property.</summary>
///<value>KeepNullValue.</value>
public bool KeepNullValue
{
get
{
return ( keepNullValue );
}//get
set
{
keepNullValue = value;
}//set
}//KeepNullValue
///<summary>Property.</summary>
///<value>MaximumError.</value>
public int MaximumError
{
get
{
return ( maximumError );
}//get
set
{
maximumError = value;
}//set
}//MaximumError
///<summary>Property.</summary>
///<value>NativeType.</value>
public bool NativeType
{
get
{
return ( nativeType );
}//get
set
{
nativeType = value;
}//set
}//NativeType
///<summary>Property.</summary>
///<value>Password.</value>
public String Password
{
get
{
return ( password );
}//get
set
{
password = value.Trim();
}//set
}//Password
///<summary>Property.</summary>
///<value>ServerName.</value>
public String ServerName
{
get
{
return ( serverName );
}//get
set
{
serverName = value.Trim();
}//set
}//ServerName
///<summary>Property.</summary>
///<value>TableName.</value>
public String[] TableName
{
get
{
return ( tableName );
}//get
set
{
if ( value != null )
{
for ( int valueIndex = 0; valueIndex < value.Length; ++valueIndex )
{
value[valueIndex] = value[valueIndex].Trim();
}//for ( int valueIndex = 0; valueIndex <= value.Length; ++valueIndex )
}//if ( value != null )
this.tableName = value;
}//set
}//tableName
///<summary>Property.</summary>
///<value>TrustedConnection.</value>
public bool TrustedConnection
{
get
{
return ( trustedConnection );
}//get
set
{
trustedConnection = value;
}//set
}//TrustedConnection
///<summary>Property.</summary>
///<value>UserName.</value>
public String UserName
{
get
{
return ( userName );
}//get
set
{
userName = value.Trim();
}//set
}//UserName
///<summary>CapitalLetters.</summary>
public StringBuilder CapitalLetters()
{
StringBuilder sb = new StringBuilder();
if ( BatchSize >= 1 )
{
sb.Append( " -b " );
sb.Append( BatchSize );
}//if ( BatchSize >= 1 )
if ( CharacterType )
{
sb.Append( " -c " );
}//if ( CharacterType )
if ( KeepIdentityValue )
{
sb.Append( " -E " );
}//if ( keepIdentityValue )
if ( KeepNullValue )
{
sb.Append( " -k " );
}//if ( keepNullValue )
if ( MaximumError >= 1 )
{
sb.Append( " -m " );
sb.Append( MaximumError );
}//if ( MaximumError >= 1 )
if ( NativeType )
{
sb.Append( " -n " );
}//if ( NativeType )
if ( Password != null )
{
sb.Append( " -P " );
sb.Append( Password );
}//if ( Password != null )
if ( ServerName != null )
{
sb.Append( " -S " );
sb.Append( ServerName );
}//if ( ServerName != null )
if ( TrustedConnection )
{
sb.Append( " -T " );
}//if ( TrustedConnection )
if ( UserName != null )
{
sb.Append( " -U " );
sb.Append( UserName );
}//if ( UserName != null )
UtilityDebug.Write
(
String.Format
(
"Capital Letters: {0}",
sb
)
);
return ( sb );
}//public StringBuilder CapitalLetters()
/*
/// <summary>ToString().</summary>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append( serverName );
return sb.ToString();
}//public override string ToString()
*/
}//public class UtilityBCPArgument
/// <summary>UtilityBCP.</summary>
public class UtilityBCP
{
///<summary>CharacterType.</summary>
public static bool CharacterType = true;
///<summary>NativeType.</summary>
public static bool NativeType = false;
///<summary>RankDatabaseNamingConventionDatabaseName.</summary>
public static int RankDatabaseNamingConventionDatabaseName = 0;
///<summary>RankDatabaseNamingConventionOwnerName.</summary>
public static int RankDatabaseNamingConventionOwnerName = 1;
///<summary>RankDatabaseNamingConventionTableName.</summary>
public static int RankDatabaseNamingConventionTableName = 2;
///<summary>TrustedConnection.</summary>
public static bool TrustedConnection = true;
///<summary>BatchSize.</summary>
public static int BatchSize = 10;
///<summary>BCP.</summary>
public static String[] CommandBCP = new String[] { "BCP" };
///<summary>The connection string database.</summary>
public static String ConnectionStringDatabase = @"Provider=SQLOLEDB; Data Source=localhost; Integrated Security=SSPI; Initial Catalog=WordEngineering";
/// <summary>DatabaseName</summary>
public static String DatabaseName = null;
/// <summary>DelimiterStringComma</summary>
public static String DelimiterStringComma = ",";
/// <summary>DelimiterString</summary>
public static String DelimiterStringFullStop = ".";
/// <summary>DelimiterCharArrayComma</summary>
public static char[] DelimiterCharArrayComma = DelimiterStringComma.ToCharArray();
/// <summary>DelimiterCharArrayFullStop</summary>
public static char[] DelimiterCharArrayFullStop = DelimiterStringFullStop.ToCharArray();
/// <summary>DirectoryDataFile</summary>
public static String DirectoryDataFile = @"\BCP";
/// <summary>DomainName.</summary>
public static String DomainName = null;
/// <summary>The configuration XML filename.</summary>
public static String FilenameConfigurationXml = @"WordEngineering.config";
/// <summary>FormatBCPCommandOut</summary>
public static String FormatBCPCommandOut = @"{0} OUT {1} -e {2} {3}";
/// <summary>FormatBCPDirectory</summary>
public static String FormatBCPDirectory = @"{0}\{1}\{2}";
/// <summary>FormatBCPDatafile</summary>
public static String FormatBCPDataFile = @"{0}\{1}{2}.txt";
/// <summary>FormatBCPErrorfile</summary>
public static String FormatBCPErrorFile = @"{0}\{1}{2}.err";
/// <summary>FormatDate</summary>
public static String FormatDate = "yyyyMMddHHmm";
///<summary>KeepIdentityValue.</summary>
public static bool KeepIdentityValue = true;
///<summary>KeepNullValue.</summary>
public static bool KeepNullValue = true;
///<summary>MaximumError.</summary>
public static int MaximumError = 1000;
/// <summary>Password.</summary>
public static String Password = null;
/// <summary>TableName</summary>
public static String TableName = null;
/// <summary>UserName.</summary>
public static String UserName = null;
/// <summary>The XPath BCPDatabase.</summary>
public static String XPathBCPDatabase = @"/word/bcp/database";
/// <summary>The XPath BCPDirectoryDataFile.</summary>
public static String XPathBCPDirectoryDataFile = @"/word/bcp/directoryDatafile";
/// <summary>The XPath BCPTable.</summary>
public static String XPathBCPTable = @"/word/bcp/table";
/// <summary>The XPath Connection String Database Format.</summary>
public static String XPathConnectionStringDatabase = @"/word/database/sqlServer/wordEngineering/databaseConnectionString";
/// <summary>The XPath Connection String Database Format.</summary>
public static String XPathConnectionStringDatabaseFormat = @"/word/database/sqlServer/{0}/databaseConnectionString";
/// <summary>The entry point for the application.</summary>
/// <param name="argv">A list of command line arguments</param>
public static void Main
(
String[] argv
)
{
Boolean booleanParseCommandLineArguments = false;
String exceptionMessage = null;
UtilityBCPArgument utilityBCPArgument = null;
utilityBCPArgument = new UtilityBCPArgument();
booleanParseCommandLineArguments = UtilityParseCommandLineArgument.ParseCommandLineArguments
(
argv,
utilityBCPArgument
);
if ( booleanParseCommandLineArguments == false )
{
// error encountered in arguments. Display usage message
UtilityDebug.Write
(
UtilityParseCommandLineArgument.CommandLineArgumentsUsage( typeof ( UtilityBCPArgument ) )
);
return;
}//if ( booleanParseCommandLineArguments == false )
BCP
(
ref utilityBCPArgument,
ref exceptionMessage
);
}//public static void Main()
/// <summary>BCP.</summary>
public static void BCP
(
ref UtilityBCPArgument utilityBCPArgument,
ref String exceptionMessage
)
{
XmlNodeList xmlNodeListDatabase = null;
ArrayList arrayListTableName = null;
BCP
(
ref FilenameConfigurationXml,
ref xmlNodeListDatabase,
ref utilityBCPArgument,
ref arrayListTableName,
ref exceptionMessage
);
}//public static void BCP()
/// <summary>BCP.</summary>
public static void BCP
(
ref String filenameConfigurationXml,
ref XmlNodeList xmlNodeListDatabase,
ref UtilityBCPArgument utilityBCPArgument,
ref ArrayList arrayListTableName,
ref String exceptionMessage
)
{
Boolean redirectStandardOutputError = true;
String BCPCommandOut;
String BCPDataFile;
String BCPDirectory;
String BCPErrorFile;
String[] databaseOwnerTableName;
String dateTimeNow = DateTime.Now.ToString(FormatDate);
String redirectStandardOutput = null;
String redirectStandardError = null;
String verb = null;
StringBuilder capitalLetters;
HttpContext httpContext = HttpContext.Current;
arrayListTableName = new ArrayList();
if ( utilityBCPArgument.DatabaseName == null || utilityBCPArgument.DatabaseName.Length == 0 )
{
utilityBCPArgument.DatabaseName = DatabaseName.Split( DelimiterCharArrayComma );
}//if ( utilityBCPArgument.DatabaseName == null || utilityBCPArgument.DatabaseName.Length == 0 )
if ( utilityBCPArgument.TableName == null || utilityBCPArgument.TableName.Length == 0 )
{
utilityBCPArgument.TableName = UtilityBCP.TableName.Split( UtilityBCP.DelimiterCharArrayComma );
}//if ( utilityBCPArgument.TableName == null || utilityBCPArgument.TableName.Length == 0 )
if ( utilityBCPArgument.TableName != null && utilityBCPArgument.TableName.Length != 0 )
{
foreach ( String tableNameCurrent in utilityBCPArgument.TableName )
{
if ( tableNameCurrent.Trim() == String.Empty )
{
continue;
}//if ( tableNameCurrent.Trim() == String.Empty )
arrayListTableName.Add( tableNameCurrent );
}//foreach ( String tableNameCurrent in utilityBCPArgument.tableName )
}//if ( utilityBCPArgument.TableName != null && utilityBCPArgument.TableName.Length != 0 )
UtilityDatabase.PopulateDatabaseTable
(
ref filenameConfigurationXml,
ref xmlNodeListDatabase,
ref utilityBCPArgument.databaseName,
ref arrayListTableName,
ref exceptionMessage
);
capitalLetters = utilityBCPArgument.CapitalLetters();
foreach (object databaseOwnerTableNameCurrent in arrayListTableName)
{
databaseOwnerTableName = ((String) (databaseOwnerTableNameCurrent)).Split( DelimiterCharArrayFullStop );
BCPDirectory = String.Format
(
FormatBCPDirectory,
utilityBCPArgument.DirectoryDataFile.ToString(),
databaseOwnerTableName[RankDatabaseNamingConventionDatabaseName],
databaseOwnerTableName[RankDatabaseNamingConventionTableName]
);
BCPDataFile = String.Format
(
FormatBCPDataFile,
BCPDirectory,
databaseOwnerTableName[RankDatabaseNamingConventionTableName],
dateTimeNow
);
BCPErrorFile = String.Format
(
FormatBCPErrorFile,
BCPDirectory,
databaseOwnerTableName[RankDatabaseNamingConventionTableName],
dateTimeNow
);
BCPCommandOut = String.Format
(
FormatBCPCommandOut,
databaseOwnerTableNameCurrent,
BCPDataFile,
BCPErrorFile,
capitalLetters
);
UtilityDebug.Write
(
String.Format
(
"BCP Directory: {0} | BCPDataFile: {1} | BCPErrorFile: {2} | BCPCommandOut: {3}",
BCPDirectory,
BCPDataFile,
BCPErrorFile,
BCPCommandOut
)
);
UtilityDirectory.CreateDirectory
(
ref BCPDirectory,
ref exceptionMessage
);
UtilityProcess.FileStart
(
ref CommandBCP,
ref BCPCommandOut,
ref verb,
ref redirectStandardOutputError,
ref redirectStandardOutput,
ref redirectStandardError
);
}//foreach (object databaseOwnerTableNameCurrent in arrayListTableName)
}//public static void BCP()
/// <summary>ConfigurationXml.</summary>
public static void ConfigurationXml
(
ref String filenameConfigurationXml
)
{
String exceptionMessage = null;
ConfigurationXml
(
ref filenameConfigurationXml,
ref exceptionMessage,
ref ConnectionStringDatabase,
ref DatabaseName,
ref DirectoryDataFile,
ref TableName,
ref UserName,
ref DomainName,
ref Password
);
}//public static void ConfigurationXml()
/// <summary>ConfigurationXml.</summary>
public static void ConfigurationXml
(
ref String filenameConfigurationXml,
ref String exceptionMessage,
ref String connectionStringDatabase,
ref String databaseName,
ref String directoryDatafile,
ref String tableName,
ref String userName,
ref String domainName,
ref String password
)
{
UtilityXml.XmlDocumentNodeInnerText
(
filenameConfigurationXml,
ref exceptionMessage,
XPathConnectionStringDatabase,
ref connectionStringDatabase
);
UtilityXml.XmlDocumentNodeInnerText
(
filenameConfigurationXml,
ref exceptionMessage,
XPathBCPDatabase,
ref databaseName
);
UtilityXml.XmlDocumentNodeInnerText
(
filenameConfigurationXml,
ref exceptionMessage,
XPathBCPDirectoryDataFile,
ref directoryDatafile
);
UtilityXml.XmlDocumentNodeInnerText
(
filenameConfigurationXml,
ref exceptionMessage,
XPathBCPTable,
ref tableName
);
UtilityImpersonate.GetUsernamePasswordDomainName
(
ref filenameConfigurationXml,
ref userName,
ref domainName,
ref password,
ref exceptionMessage
);
UtilityDebug.Write
(
String.Format
(
"filenameConfigurationXml: {0} | connectionStringDatabase: {1} | databaseName: {2} | directoryDatafile: {3} | tableName: {4}",
filenameConfigurationXml,
connectionStringDatabase,
databaseName,
directoryDatafile,
tableName
)
);
}//public static void ConfigurationXml()
static UtilityBCP()
{
ConfigurationXml
(
ref FilenameConfigurationXml
);
}//static UtilityBCP()
}//public class UtilityBCP
}//namespace WordEngineering