forked from microsoftarchive/data-access-application-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cs
2276 lines (2112 loc) · 114 KB
/
Database.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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2013 Microsoft Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Transactions;
using Microsoft.Practices.EnterpriseLibrary.Data.Properties;
namespace Microsoft.Practices.EnterpriseLibrary.Data
{
/// <summary>
/// Represents an abstract database that commands can be run against.
/// </summary>
/// <remarks>
/// The <see cref="Database"/> class leverages the provider factory model from ADO.NET. A database instance holds
/// a reference to a concrete <see cref="DbProviderFactory"/> object to which it forwards the creation of ADO.NET objects.
/// </remarks>
public abstract class Database
{
static readonly ParameterCache parameterCache = new ParameterCache();
static readonly string VALID_PASSWORD_TOKENS = Resources.Password;
static readonly string VALID_USER_ID_TOKENS = Resources.UserName;
readonly ConnectionString connectionString;
readonly DbProviderFactory dbProviderFactory;
/// <summary>
/// Initializes a new instance of the <see cref="Database"/> class with a connection string and a <see cref="DbProviderFactory"/>.
/// </summary>
/// <param name="connectionString">The connection string for the database.</param>
/// <param name="dbProviderFactory">A <see cref="DbProviderFactory"/> object.</param>
protected Database(string connectionString, DbProviderFactory dbProviderFactory)
{
if (string.IsNullOrEmpty(connectionString)) throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "connectionString");
if (dbProviderFactory == null) throw new ArgumentNullException("dbProviderFactory");
this.connectionString = new ConnectionString(connectionString, VALID_USER_ID_TOKENS, VALID_PASSWORD_TOKENS);
this.dbProviderFactory = dbProviderFactory;
}
/// <summary>
/// <para>Gets the string used to open a database.</para>
/// </summary>
/// <value>
/// <para>The string used to open a database.</para>
/// </value>
/// <seealso cref="DbConnection.ConnectionString"/>
public string ConnectionString
{
get { return connectionString.ToString(); }
}
/// <summary>
/// <para>Gets the connection string without the user name and password.</para>
/// </summary>
/// <value>
/// <para>The connection string without the user name and password.</para>
/// </value>
/// <seealso cref="ConnectionString"/>
protected string ConnectionStringNoCredentials
{
get { return connectionString.ToStringNoCredentials(); }
}
/// <summary>
/// Gets the connection string without credentials.
/// </summary>
/// <value>
/// The connection string without credentials.
/// </value>
public string ConnectionStringWithoutCredentials
{
get { return ConnectionStringNoCredentials; }
}
/// <summary>
/// <para>Gets the DbProviderFactory used by the database instance.</para>
/// </summary>
/// <seealso cref="DbProviderFactory"/>
public DbProviderFactory DbProviderFactory
{
get { return dbProviderFactory; }
}
/// <summary>
/// Adds a new In <see cref="DbParameter"/> object to the given <paramref name="command"/>.
/// </summary>
/// <param name="command">The command to add the in parameter.</param>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <remarks>
/// <para>This version of the method is used when you can have the same parameter object multiple times with different values.</para>
/// </remarks>
public void AddInParameter(DbCommand command,
string name,
DbType dbType)
{
AddParameter(command, name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, null);
}
/// <summary>
/// Adds a new In <see cref="DbParameter"/> object to the given <paramref name="command"/>.
/// </summary>
/// <param name="command">The command to add the parameter.</param>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <param name="value"><para>The value of the parameter.</para></param>
public void AddInParameter(DbCommand command,
string name,
DbType dbType,
object value)
{
AddParameter(command, name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, value);
}
/// <summary>
/// Adds a new In <see cref="DbParameter"/> object to the given <paramref name="command"/>.
/// </summary>
/// <param name="command">The command to add the parameter.</param>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the value.</para></param>
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
public void AddInParameter(DbCommand command,
string name,
DbType dbType,
string sourceColumn,
DataRowVersion sourceVersion)
{
AddParameter(command, name, dbType, 0, ParameterDirection.Input, true, 0, 0, sourceColumn, sourceVersion, null);
}
/// <summary>
/// Adds a new Out <see cref="DbParameter"/> object to the given <paramref name="command"/>.
/// </summary>
/// <param name="command">The command to add the out parameter.</param>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <param name="size"><para>The maximum size of the data within the column.</para></param>
public void AddOutParameter(DbCommand command,
string name,
DbType dbType,
int size)
{
AddParameter(command, name, dbType, size, ParameterDirection.Output, true, 0, 0, String.Empty, DataRowVersion.Default, DBNull.Value);
}
/// <summary>
/// Adds a new In <see cref="DbParameter"/> object to the given <paramref name="command"/>.
/// </summary>
/// <param name="command">The command to add the parameter.</param>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <param name="size"><para>The maximum size of the data within the column.</para></param>
/// <param name="direction"><para>One of the <see cref="ParameterDirection"/> values.</para></param>
/// <param name="nullable"><para>A value indicating whether the parameter accepts <see langword="null"/> (<b>Nothing</b> in Visual Basic) values.</para></param>
/// <param name="precision"><para>The maximum number of digits used to represent the <paramref name="value"/>.</para></param>
/// <param name="scale"><para>The number of decimal places to which <paramref name="value"/> is resolved.</para></param>
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the <paramref name="value"/>.</para></param>
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
/// <param name="value"><para>The value of the parameter.</para></param>
public virtual void AddParameter(DbCommand command,
string name,
DbType dbType,
int size,
ParameterDirection direction,
bool nullable,
byte precision,
byte scale,
string sourceColumn,
DataRowVersion sourceVersion,
object value)
{
if (command == null) throw new ArgumentNullException("command");
DbParameter parameter = CreateParameter(name, dbType, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, value);
command.Parameters.Add(parameter);
}
/// <summary>
/// <para>Adds a new instance of a <see cref="DbParameter"/> object to the command.</para>
/// </summary>
/// <param name="command">The command to add the parameter.</param>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <param name="direction"><para>One of the <see cref="ParameterDirection"/> values.</para></param>
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the <paramref name="value"/>.</para></param>
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
/// <param name="value"><para>The value of the parameter.</para></param>
public void AddParameter(DbCommand command,
string name,
DbType dbType,
ParameterDirection direction,
string sourceColumn,
DataRowVersion sourceVersion,
object value)
{
AddParameter(command, name, dbType, 0, direction, false, 0, 0, sourceColumn, sourceVersion, value);
}
void AssignParameterValues(DbCommand command,
object[] values)
{
int parameterIndexShift = UserParametersStartIndex(); // DONE magic number, depends on the database
for (int i = 0; i < values.Length; i++)
{
IDataParameter parameter = command.Parameters[i + parameterIndexShift];
// There used to be code here that checked to see if the parameter was input or input/output
// before assigning the value to it. We took it out because of an operational bug with
// deriving parameters for a stored procedure. It turns out that output parameters are set
// to input/output after discovery, so any direction checking was unneeded. Should it ever
// be needed, it should go here, and check that a parameter is input or input/output before
// assigning a value to it.
SetParameterValue(command, parameter.ParameterName, values[i]);
}
}
static DbTransaction BeginTransaction(DbConnection connection)
{
DbTransaction tran = connection.BeginTransaction();
return tran;
}
/// <summary>
/// Builds a value parameter name for the current database.
/// </summary>
/// <param name="name">The name of the parameter.</param>
/// <returns>A correctly formated parameter name.</returns>
public virtual string BuildParameterName(string name)
{
return name;
}
/// <summary>
/// Clears the parameter cache. Since there is only one parameter cache that is shared by all instances
/// of this class, this clears all parameters cached for all databases.
/// </summary>
public static void ClearParameterCache()
{
parameterCache.Clear();
}
static void CommitTransaction(IDbTransaction tran)
{
tran.Commit();
}
/// <summary>
/// Configures a given <see cref="DbParameter"/>.
/// </summary>
/// <param name="param">The <see cref="DbParameter"/> to configure.</param>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <param name="size"><para>The maximum size of the data within the column.</para></param>
/// <param name="direction"><para>One of the <see cref="ParameterDirection"/> values.</para></param>
/// <param name="nullable"><para>A value indicating whether the parameter accepts <see langword="null"/> (<b>Nothing</b> in Visual Basic) values.</para></param>
/// <param name="precision"><para>The maximum number of digits used to represent the <paramref name="value"/>.</para></param>
/// <param name="scale"><para>The number of decimal places to which <paramref name="value"/> is resolved.</para></param>
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the <paramref name="value"/>.</para></param>
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
/// <param name="value"><para>The value of the parameter.</para></param>
protected virtual void ConfigureParameter(DbParameter param,
string name,
DbType dbType,
int size,
ParameterDirection direction,
bool nullable,
byte precision,
byte scale,
string sourceColumn,
DataRowVersion sourceVersion,
object value)
{
param.DbType = dbType;
param.Size = size;
param.Value = value ?? DBNull.Value;
param.Direction = direction;
param.IsNullable = nullable;
param.SourceColumn = sourceColumn;
param.SourceVersion = sourceVersion;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities", Justification = "The purpose of the block is to execute arbitrary SQL on behalf of the user. It is known that the users must review the use of the Database for security vulnerabilities.")]
DbCommand CreateCommandByCommandType(CommandType commandType,
string commandText)
{
DbCommand command = dbProviderFactory.CreateCommand();
command.CommandType = commandType;
command.CommandText = commandText;
return command;
}
/// <summary>
/// <para>Creates a connection for this database.</para>
/// </summary>
/// <returns>
/// <para>The <see cref="DbConnection"/> for this database.</para>
/// </returns>
/// <seealso cref="DbConnection"/>
public virtual DbConnection CreateConnection()
{
DbConnection newConnection = dbProviderFactory.CreateConnection();
newConnection.ConnectionString = ConnectionString;
return newConnection;
}
/// <summary>
/// <para>Adds a new instance of a <see cref="DbParameter"/> object.</para>
/// </summary>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <param name="size"><para>The maximum size of the data within the column.</para></param>
/// <param name="direction"><para>One of the <see cref="ParameterDirection"/> values.</para></param>
/// <param name="nullable"><para>A value indicating whether the parameter accepts <see langword="null"/> (<b>Nothing</b> in Visual Basic) values.</para></param>
/// <param name="precision"><para>The maximum number of digits used to represent the <paramref name="value"/>.</para></param>
/// <param name="scale"><para>The number of decimal places to which <paramref name="value"/> is resolved.</para></param>
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the <paramref name="value"/>.</para></param>
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
/// <param name="value"><para>The value of the parameter.</para></param>
/// <returns>A newly created <see cref="DbParameter"/> fully initialized with given parameters.</returns>
protected DbParameter CreateParameter(string name,
DbType dbType,
int size,
ParameterDirection direction,
bool nullable,
byte precision,
byte scale,
string sourceColumn,
DataRowVersion sourceVersion,
object value)
{
DbParameter param = CreateParameter(name);
ConfigureParameter(param, name, dbType, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, value);
return param;
}
/// <summary>
/// <para>Adds a new instance of a <see cref="DbParameter"/> object.</para>
/// </summary>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <returns><para>An unconfigured parameter.</para></returns>
protected DbParameter CreateParameter(string name)
{
DbParameter param = dbProviderFactory.CreateParameter();
param.ParameterName = BuildParameterName(name);
return param;
}
/// <summary>
/// Does this <see cref='Database'/> object support parameter discovery?
/// </summary>
/// <value>Base class always returns false.</value>
public virtual bool SupportsParemeterDiscovery
{
get { return false; }
}
/// <summary>
/// Retrieves parameter information from the stored procedure specified in the <see cref="DbCommand"/> and populates the Parameters collection of the specified <see cref="DbCommand"/> object.
/// </summary>
/// <param name="discoveryCommand">The <see cref="DbCommand"/> to do the discovery.</param>
protected abstract void DeriveParameters(DbCommand discoveryCommand);
/// <summary>
/// Discovers the parameters for a <see cref="DbCommand"/>.
/// </summary>
/// <param name="command">The <see cref="DbCommand"/> to discover the parameters.</param>
public void DiscoverParameters(DbCommand command)
{
if (command == null) throw new ArgumentNullException("command");
using (var wrapper = GetOpenConnection())
{
using (DbCommand discoveryCommand = CreateCommandByCommandType(command.CommandType, command.CommandText))
{
discoveryCommand.Connection = wrapper.Connection;
DeriveParameters(discoveryCommand);
foreach (IDataParameter parameter in discoveryCommand.Parameters)
{
IDataParameter cloneParameter = (IDataParameter)((ICloneable)parameter).Clone();
command.Parameters.Add(cloneParameter);
}
}
}
}
/// <summary>
/// Executes the query for <paramref name="command"/>.
/// </summary>
/// <param name="command">The <see cref="DbCommand"/> representing the query to execute.</param>
/// <returns>The quantity of rows affected.</returns>
protected int DoExecuteNonQuery(DbCommand command)
{
if (command == null) throw new ArgumentNullException("command");
int rowsAffected = command.ExecuteNonQuery();
return rowsAffected;
}
IDataReader DoExecuteReader(DbCommand command,
CommandBehavior cmdBehavior)
{
IDataReader reader = command.ExecuteReader(cmdBehavior);
return reader;
}
object DoExecuteScalar(IDbCommand command)
{
object returnValue = command.ExecuteScalar();
return returnValue;
}
void DoLoadDataSet(IDbCommand command,
DataSet dataSet,
string[] tableNames)
{
if (tableNames == null)
{
throw new ArgumentNullException(nameof(tableNames));
}
if (tableNames.Length == 0)
{
throw new ArgumentException(Resources.ExceptionTableNameArrayEmpty, nameof(tableNames));
}
for (int i = 0; i < tableNames.Length; i++)
{
if (string.IsNullOrEmpty(tableNames[i]))
{
throw new ArgumentException(Resources.ExceptionNullOrEmptyString, $"{nameof(tableNames)}[{i}]");
}
}
using (DbDataAdapter adapter = GetDataAdapter(UpdateBehavior.Standard))
{
((IDbDataAdapter)adapter).SelectCommand = command;
string systemCreatedTableNameRoot = "Table";
for (int i = 0; i < tableNames.Length; i++)
{
string systemCreatedTableName = (i == 0)
? systemCreatedTableNameRoot
: systemCreatedTableNameRoot + i;
adapter.TableMappings.Add(systemCreatedTableName, tableNames[i]);
}
adapter.Fill(dataSet);
}
}
int DoUpdateDataSet(UpdateBehavior behavior,
DataSet dataSet,
string tableName,
IDbCommand insertCommand,
IDbCommand updateCommand,
IDbCommand deleteCommand,
int? updateBatchSize)
{
if (string.IsNullOrEmpty(tableName)) throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "tableName");
if (dataSet == null) throw new ArgumentNullException("dataSet");
if (insertCommand == null && updateCommand == null && deleteCommand == null)
{
throw new ArgumentException(Resources.ExceptionMessageUpdateDataSetArgumentFailure);
}
using (DbDataAdapter adapter = GetDataAdapter(behavior))
{
IDbDataAdapter explicitAdapter = adapter;
if (insertCommand != null)
{
explicitAdapter.InsertCommand = insertCommand;
}
if (updateCommand != null)
{
explicitAdapter.UpdateCommand = updateCommand;
}
if (deleteCommand != null)
{
explicitAdapter.DeleteCommand = deleteCommand;
}
if (updateBatchSize != null)
{
adapter.UpdateBatchSize = (int)updateBatchSize;
if (insertCommand != null)
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
if (updateCommand != null)
adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
if (deleteCommand != null)
adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
}
int rows = adapter.Update(dataSet.Tables[tableName]);
return rows;
}
}
/// <summary>
/// <para>Executes the <paramref name="command"/> and returns the results in a new <see cref="DataSet"/>.</para>
/// </summary>
/// <param name="command"><para>The <see cref="DbCommand"/> to execute.</para></param>
/// <returns>A <see cref="DataSet"/> with the results of the <paramref name="command"/>.</returns>
public virtual DataSet ExecuteDataSet(DbCommand command)
{
DataSet dataSet = new DataSet();
dataSet.Locale = CultureInfo.InvariantCulture;
LoadDataSet(command, dataSet, "Table");
return dataSet;
}
/// <summary>
/// <para>Executes the <paramref name="command"/> as part of the <paramref name="transaction" /> and returns the results in a new <see cref="DataSet"/>.</para>
/// </summary>
/// <param name="command"><para>The <see cref="DbCommand"/> to execute.</para></param>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <returns>A <see cref="DataSet"/> with the results of the <paramref name="command"/>.</returns>
public virtual DataSet ExecuteDataSet(DbCommand command,
DbTransaction transaction)
{
var dataSet = new DataSet();
dataSet.Locale = CultureInfo.InvariantCulture;
LoadDataSet(command, dataSet, "Table", transaction);
return dataSet;
}
/// <summary>
/// <para>Executes the <paramref name="storedProcedureName"/> with <paramref name="parameterValues" /> and returns the results in a new <see cref="DataSet"/>.</para>
/// </summary>
/// <param name="storedProcedureName">
/// <para>The stored procedure to execute.</para>
/// </param>
/// <param name="parameterValues">
/// <para>An array of parameters to pass to the stored procedure. The parameter values must be in call order as they appear in the stored procedure.</para>
/// </param>
/// <returns>
/// <para>A <see cref="DataSet"/> with the results of the <paramref name="storedProcedureName"/>.</para>
/// </returns>
public virtual DataSet ExecuteDataSet(string storedProcedureName,
params object[] parameterValues)
{
using (DbCommand command = GetStoredProcCommand(storedProcedureName, parameterValues))
{
return ExecuteDataSet(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="storedProcedureName"/> with <paramref name="parameterValues" /> as part of the <paramref name="transaction" /> and returns the results in a new <see cref="DataSet"/> within a transaction.</para>
/// </summary>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <param name="storedProcedureName">
/// <para>The stored procedure to execute.</para>
/// </param>
/// <param name="parameterValues">
/// <para>An array of parameters to pass to the stored procedure. The parameter values must be in call order as they appear in the stored procedure.</para>
/// </param>
/// <returns>
/// <para>A <see cref="DataSet"/> with the results of the <paramref name="storedProcedureName"/>.</para>
/// </returns>
public virtual DataSet ExecuteDataSet(DbTransaction transaction,
string storedProcedureName,
params object[] parameterValues)
{
using (DbCommand command = GetStoredProcCommand(storedProcedureName, parameterValues))
{
return ExecuteDataSet(command, transaction);
}
}
/// <summary>
/// <para>Executes the <paramref name="commandText"/> interpreted as specified by the <paramref name="commandType" /> and returns the results in a new <see cref="DataSet"/>.</para>
/// </summary>
/// <param name="commandType">
/// <para>One of the <see cref="CommandType"/> values.</para>
/// </param>
/// <param name="commandText">
/// <para>The command text to execute.</para>
/// </param>
/// <returns>
/// <para>A <see cref="DataSet"/> with the results of the <paramref name="commandText"/>.</para>
/// </returns>
public virtual DataSet ExecuteDataSet(CommandType commandType,
string commandText)
{
using (DbCommand command = CreateCommandByCommandType(commandType, commandText))
{
return ExecuteDataSet(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="commandText"/> as part of the given <paramref name="transaction" /> and returns the results in a new <see cref="DataSet"/>.</para>
/// </summary>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <param name="commandType">
/// <para>One of the <see cref="CommandType"/> values.</para>
/// </param>
/// <param name="commandText">
/// <para>The command text to execute.</para>
/// </param>
/// <returns>
/// <para>A <see cref="DataSet"/> with the results of the <paramref name="commandText"/>.</para>
/// </returns>
public virtual DataSet ExecuteDataSet(DbTransaction transaction,
CommandType commandType,
string commandText)
{
using (DbCommand command = CreateCommandByCommandType(commandType, commandText))
{
return ExecuteDataSet(command, transaction);
}
}
/// <summary>
/// <para>Executes the <paramref name="command"/> and returns the number of rows affected.</para>
/// </summary>
/// <param name="command">
/// <para>The command that contains the query to execute.</para>
/// </param>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual int ExecuteNonQuery(DbCommand command)
{
using (var wrapper = GetOpenConnection())
{
PrepareCommand(command, wrapper.Connection);
return DoExecuteNonQuery(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="command"/> within the given <paramref name="transaction" />, and returns the number of rows affected.</para>
/// </summary>
/// <param name="command">
/// <para>The command that contains the query to execute.</para>
/// </param>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual int ExecuteNonQuery(DbCommand command,
DbTransaction transaction)
{
PrepareCommand(command, transaction);
return DoExecuteNonQuery(command);
}
/// <summary>
/// <para>Executes the <paramref name="storedProcedureName"/> using the given <paramref name="parameterValues" /> and returns the number of rows affected.</para>
/// </summary>
/// <param name="storedProcedureName">
/// <para>The name of the stored procedure to execute.</para>
/// </param>
/// <param name="parameterValues">
/// <para>An array of parameters to pass to the stored procedure. The parameter values must be in call order as they appear in the stored procedure.</para>
/// </param>
/// <returns>
/// <para>The number of rows affected</para>
/// </returns>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual int ExecuteNonQuery(string storedProcedureName,
params object[] parameterValues)
{
using (DbCommand command = GetStoredProcCommand(storedProcedureName, parameterValues))
{
return ExecuteNonQuery(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="storedProcedureName"/> using the given <paramref name="parameterValues" /> within a transaction and returns the number of rows affected.</para>
/// </summary>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <param name="storedProcedureName">
/// <para>The name of the stored procedure to execute.</para>
/// </param>
/// <param name="parameterValues">
/// <para>An array of parameters to pass to the stored procedure. The parameter values must be in call order as they appear in the stored procedure.</para>
/// </param>
/// <returns>
/// <para>The number of rows affected.</para>
/// </returns>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual int ExecuteNonQuery(DbTransaction transaction,
string storedProcedureName,
params object[] parameterValues)
{
using (DbCommand command = GetStoredProcCommand(storedProcedureName, parameterValues))
{
return ExecuteNonQuery(command, transaction);
}
}
/// <summary>
/// <para>Executes the <paramref name="commandText"/> interpreted as specified by the <paramref name="commandType" /> and returns the number of rows affected.</para>
/// </summary>
/// <param name="commandType">
/// <para>One of the <see cref="CommandType"/> values.</para>
/// </param>
/// <param name="commandText">
/// <para>The command text to execute.</para>
/// </param>
/// <returns>
/// <para>The number of rows affected.</para>
/// </returns>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual int ExecuteNonQuery(CommandType commandType,
string commandText)
{
using (DbCommand command = CreateCommandByCommandType(commandType, commandText))
{
return ExecuteNonQuery(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="commandText"/> interpreted as specified by the <paramref name="commandType" /> as part of the given <paramref name="transaction" /> and returns the number of rows affected.</para>
/// </summary>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <param name="commandType">
/// <para>One of the <see cref="CommandType"/> values.</para>
/// </param>
/// <param name="commandText">
/// <para>The command text to execute.</para>
/// </param>
/// <returns>
/// <para>The number of rows affected</para>
/// </returns>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual int ExecuteNonQuery(DbTransaction transaction,
CommandType commandType,
string commandText)
{
using (DbCommand command = CreateCommandByCommandType(commandType, commandText))
{
return ExecuteNonQuery(command, transaction);
}
}
/// <summary>
/// <para>Executes the <paramref name="command"/> and returns an <see cref="IDataReader"></see> through which the result can be read.
/// It is the responsibility of the caller to close the reader when finished.</para>
/// </summary>
/// <param name="command">
/// <para>The command that contains the query to execute.</para>
/// </param>
/// <returns>
/// <para>An <see cref="IDataReader"/> object.</para>
/// </returns>
public virtual IDataReader ExecuteReader(DbCommand command)
{
using (DatabaseConnectionWrapper wrapper = GetOpenConnection())
{
PrepareCommand(command, wrapper.Connection);
IDataReader realReader = DoExecuteReader(command, CommandBehavior.Default);
return CreateWrappedReader(wrapper, realReader);
}
}
/// <summary>
/// All data readers get wrapped in objects so that they properly manage connections.
/// Some derived Database classes will need to create a different wrapper, so this
/// method is provided so that they can do this.
/// </summary>
/// <param name="connection">Connection + refcount.</param>
/// <param name="innerReader">The reader to wrap.</param>
/// <returns>The new reader.</returns>
protected virtual IDataReader CreateWrappedReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
{
return new RefCountingDataReader(connection, innerReader);
}
/// <summary>
/// <para>Executes the <paramref name="command"/> within a transaction and returns an <see cref="IDataReader"></see> through which the result can be read.
/// It is the responsibility of the caller to close the connection and reader when finished.</para>
/// </summary>
/// <param name="command">
/// <para>The command that contains the query to execute.</para>
/// </param>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <returns>
/// <para>An <see cref="IDataReader"/> object.</para>
/// </returns>
public virtual IDataReader ExecuteReader(DbCommand command,
DbTransaction transaction)
{
PrepareCommand(command, transaction);
return DoExecuteReader(command, CommandBehavior.Default);
}
/// <summary>
/// <para>Executes the <paramref name="storedProcedureName"/> with the given <paramref name="parameterValues" /> and returns an <see cref="IDataReader"></see> through which the result can be read.
/// It is the responsibility of the caller to close the connection and reader when finished.</para>
/// </summary>
/// <param name="storedProcedureName">
/// <para>The command that contains the query to execute.</para>
/// </param>
/// <param name="parameterValues">
/// <para>An array of parameters to pass to the stored procedure. The parameter values must be in call order as they appear in the stored procedure.</para>
/// </param>
/// <returns>
/// <para>An <see cref="IDataReader"/> object.</para>
/// </returns>
public IDataReader ExecuteReader(string storedProcedureName,
params object[] parameterValues)
{
using (DbCommand command = GetStoredProcCommand(storedProcedureName, parameterValues))
{
return ExecuteReader(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="storedProcedureName"/> with the given <paramref name="parameterValues" /> within the given <paramref name="transaction" /> and returns an <see cref="IDataReader"></see> through which the result can be read.
/// It is the responsibility of the caller to close the connection and reader when finished.</para>
/// </summary>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <param name="storedProcedureName">
/// <para>The command that contains the query to execute.</para>
/// </param>
/// <param name="parameterValues">
/// <para>An array of parameters to pass to the stored procedure. The parameter values must be in call order as they appear in the stored procedure.</para>
/// </param>
/// <returns>
/// <para>An <see cref="IDataReader"/> object.</para>
/// </returns>
public IDataReader ExecuteReader(DbTransaction transaction,
string storedProcedureName,
params object[] parameterValues)
{
using (DbCommand command = GetStoredProcCommand(storedProcedureName, parameterValues))
{
return ExecuteReader(command, transaction);
}
}
/// <summary>
/// <para>Executes the <paramref name="commandText"/> interpreted as specified by the <paramref name="commandType" /> and returns an <see cref="IDataReader"></see> through which the result can be read.
/// It is the responsibility of the caller to close the connection and reader when finished.</para>
/// </summary>
/// <param name="commandType">
/// <para>One of the <see cref="CommandType"/> values.</para>
/// </param>
/// <param name="commandText">
/// <para>The command text to execute.</para>
/// </param>
/// <returns>
/// <para>An <see cref="IDataReader"/> object.</para>
/// </returns>
public IDataReader ExecuteReader(CommandType commandType,
string commandText)
{
using (DbCommand command = CreateCommandByCommandType(commandType, commandText))
{
return ExecuteReader(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="commandText"/> interpreted as specified by the <paramref name="commandType" /> within the given
/// <paramref name="transaction" /> and returns an <see cref="IDataReader"></see> through which the result can be read.
/// It is the responsibility of the caller to close the connection and reader when finished.</para>
/// </summary>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <param name="commandType">
/// <para>One of the <see cref="CommandType"/> values.</para>
/// </param>
/// <param name="commandText">
/// <para>The command text to execute.</para>
/// </param>
/// <returns>
/// <para>An <see cref="IDataReader"/> object.</para>
/// </returns>
public IDataReader ExecuteReader(DbTransaction transaction,
CommandType commandType,
string commandText)
{
using (DbCommand command = CreateCommandByCommandType(commandType, commandText))
{
return ExecuteReader(command, transaction);
}
}
/// <summary>
/// <para>Executes the <paramref name="command"/> and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.</para>
/// </summary>
/// <param name="command">
/// <para>The command that contains the query to execute.</para>
/// </param>
/// <returns>
/// <para>The first column of the first row in the result set.</para>
/// </returns>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual object ExecuteScalar(DbCommand command)
{
if (command == null) throw new ArgumentNullException("command");
using (var wrapper = GetOpenConnection())
{
PrepareCommand(command, wrapper.Connection);
return DoExecuteScalar(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="command"/> within a <paramref name="transaction" />, and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.</para>
/// </summary>
/// <param name="command">
/// <para>The command that contains the query to execute.</para>
/// </param>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <returns>
/// <para>The first column of the first row in the result set.</para>
/// </returns>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual object ExecuteScalar(DbCommand command,
DbTransaction transaction)
{
PrepareCommand(command, transaction);
return DoExecuteScalar(command);
}
/// <summary>
/// <para>Executes the <paramref name="storedProcedureName"/> with the given <paramref name="parameterValues" /> and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.</para>
/// </summary>
/// <param name="storedProcedureName">
/// <para>The stored procedure to execute.</para>
/// </param>
/// <param name="parameterValues">
/// <para>An array of parameters to pass to the stored procedure. The parameter values must be in call order as they appear in the stored procedure.</para>
/// </param>
/// <returns>
/// <para>The first column of the first row in the result set.</para>
/// </returns>
/// <seealso cref="IDbCommand.ExecuteScalar"/>
public virtual object ExecuteScalar(string storedProcedureName,
params object[] parameterValues)
{
using (DbCommand command = GetStoredProcCommand(storedProcedureName, parameterValues))
{
return ExecuteScalar(command);
}
}
/// <summary>
/// <para>Executes the <paramref name="storedProcedureName"/> with the given <paramref name="parameterValues" /> within a
/// <paramref name="transaction" /> and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.</para>
/// </summary>
/// <param name="transaction">
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
/// </param>
/// <param name="storedProcedureName">
/// <para>The stored procedure to execute.</para>
/// </param>
/// <param name="parameterValues">
/// <para>An array of parameters to pass to the stored procedure. The parameter values must be in call order as they appear in the stored procedure.</para>
/// </param>
/// <returns>