Skip to content

Commit d7b632f

Browse files
author
jaguzman
committed
Added a new method to the ado.net db helper to execute datatables
1 parent c25ead2 commit d7b632f

File tree

1 file changed

+258
-0
lines changed

1 file changed

+258
-0
lines changed

src/DotNetToolkit.Repository.AdoNet/DbHelper.cs

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,131 @@ public Dictionary<TDictionaryKey, TElement> ExecuteDictionary<TDictionaryKey, TE
567567
return ExecuteDictionary<TDictionaryKey, TElement>(cmdText, CommandType.Text, parameters);
568568
}
569569

570+
/// <summary>
571+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
572+
/// </summary>
573+
/// <param name="command">The command.</param>
574+
/// <param name="startRecord">The zero-based record number to start with.</param>
575+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
576+
/// <returns>A <see cref="System.Data.DataTable" /> object.</returns>
577+
public DataTable ExecuteDataTable(DbCommand command, int startRecord, int maxRecords)
578+
{
579+
Guard.NotNull(command, nameof(command));
580+
581+
var connection = command.Connection;
582+
var ownsConnection = _ownsConnection && command.Transaction == null;
583+
var canCloseConnection = false;
584+
585+
if (connection.State == ConnectionState.Closed)
586+
{
587+
connection.Open();
588+
canCloseConnection = true;
589+
}
590+
591+
LogExecutingCommandQuery(command);
592+
593+
var adapter = _factory.CreateDataAdapter();
594+
595+
adapter.SelectCommand = command;
596+
597+
var dt = new DataTable();
598+
599+
if (startRecord >= 0 || maxRecords >= 0)
600+
adapter.Fill(startRecord, maxRecords, dt);
601+
else
602+
adapter.Fill(dt);
603+
604+
if (canCloseConnection && ownsConnection)
605+
connection.Dispose();
606+
607+
return dt;
608+
}
609+
610+
/// <summary>
611+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
612+
/// </summary>
613+
/// <param name="command">The command.</param>
614+
/// <returns>A <see cref="System.Data.DataTable" /> object.</returns>
615+
public DataTable ExecuteDataTable(DbCommand command)
616+
{
617+
return ExecuteDataTable(command, -1, -1);
618+
}
619+
620+
/// <summary>
621+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
622+
/// </summary>
623+
/// <param name="cmdText">The command text.</param>
624+
/// <param name="cmdType">The command type.</param>
625+
/// <param name="parameters">The command parameters.</param>
626+
/// <param name="startRecord">The zero-based record number to start with.</param>
627+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
628+
/// <returns>A <see cref="System.Data.DataTable" /> object.</returns>
629+
public DataTable ExecuteDataTable(string cmdText, CommandType cmdType, Dictionary<string, object> parameters, int startRecord, int maxRecords)
630+
{
631+
return ExecuteDataTable(CreateCommand(cmdText, cmdType, parameters), startRecord, maxRecords);
632+
}
633+
634+
/// <summary>
635+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
636+
/// </summary>
637+
/// <param name="cmdText">The command text.</param>
638+
/// <param name="cmdType">The command type.</param>
639+
/// <param name="startRecord">The zero-based record number to start with.</param>
640+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
641+
/// <returns>A <see cref="System.Data.DataTable" /> object.</returns>
642+
public DataTable ExecuteDataTable(string cmdText, CommandType cmdType, int startRecord, int maxRecords)
643+
{
644+
return ExecuteDataTable(cmdText, cmdType, null, startRecord, maxRecords);
645+
}
646+
647+
/// <summary>
648+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
649+
/// </summary>
650+
/// <param name="cmdText">The command text.</param>
651+
/// <param name="cmdType">The command type.</param>
652+
/// <param name="parameters">The command parameters.</param>
653+
/// <returns>A <see cref="System.Data.DataTable" /> object.</returns>
654+
public DataTable ExecuteDataTable(string cmdText, CommandType cmdType, Dictionary<string, object> parameters = null)
655+
{
656+
return ExecuteDataTable(cmdText, cmdType, parameters, -1, -1);
657+
}
658+
659+
/// <summary>
660+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
661+
/// </summary>
662+
/// <param name="cmdText">The command text.</param>
663+
/// <param name="parameters">The command parameters.</param>
664+
/// <param name="startRecord">The zero-based record number to start with.</param>
665+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
666+
/// <returns>A <see cref="System.Data.DataTable" /> object.</returns>
667+
public DataTable ExecuteDataTable(string cmdText, Dictionary<string, object> parameters, int startRecord, int maxRecords)
668+
{
669+
return ExecuteDataTable(cmdText, CommandType.Text, parameters, startRecord, maxRecords);
670+
}
671+
672+
/// <summary>
673+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
674+
/// </summary>
675+
/// <param name="cmdText">The command text.</param>
676+
/// <param name="startRecord">The zero-based record number to start with.</param>
677+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
678+
/// <returns>A <see cref="System.Data.DataTable" /> object.</returns>
679+
public DataTable ExecuteDataTable(string cmdText, int startRecord, int maxRecords)
680+
{
681+
return ExecuteDataTable(cmdText, null, startRecord, maxRecords);
682+
}
683+
684+
/// <summary>
685+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
686+
/// </summary>
687+
/// <param name="cmdText">The command text.</param>
688+
/// <param name="parameters">The command parameters.</param>
689+
/// <returns>A <see cref="System.Data.DataTable" /> object.</returns>
690+
public DataTable ExecuteDataTable(string cmdText, Dictionary<string, object> parameters = null)
691+
{
692+
return ExecuteDataTable(cmdText, CommandType.Text, parameters, -1, -1);
693+
}
694+
570695
/// <summary>
571696
/// Asynchronously executes a SQL statement against a connection.
572697
/// </summary>
@@ -879,6 +1004,139 @@ public Dictionary<TDictionaryKey, TElement> ExecuteDictionary<TDictionaryKey, TE
8791004
}
8801005
}
8811006

1007+
/// <summary>
1008+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
1009+
/// </summary>
1010+
/// <param name="command">The command.</param>
1011+
/// <param name="startRecord">The zero-based record number to start with.</param>
1012+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
1013+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1014+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataTable" /> object.</returns>
1015+
public async Task<DataTable> ExecuteDataTableAsync(DbCommand command, int startRecord, int maxRecords, CancellationToken cancellationToken = new CancellationToken())
1016+
{
1017+
Guard.NotNull(command, nameof(command));
1018+
1019+
var connection = command.Connection;
1020+
var ownsConnection = _ownsConnection && command.Transaction == null;
1021+
var canCloseConnection = false;
1022+
1023+
if (connection.State == ConnectionState.Closed)
1024+
{
1025+
await connection.OpenAsync(cancellationToken);
1026+
canCloseConnection = true;
1027+
}
1028+
1029+
LogExecutingCommandQuery(command);
1030+
1031+
var adapter = _factory.CreateDataAdapter();
1032+
1033+
adapter.SelectCommand = command;
1034+
1035+
var dt = new DataTable();
1036+
1037+
if (startRecord >= 0 || maxRecords >= 0)
1038+
adapter.Fill(startRecord, maxRecords, dt);
1039+
else
1040+
adapter.Fill(dt);
1041+
1042+
if (canCloseConnection && ownsConnection)
1043+
connection.Dispose();
1044+
1045+
return dt;
1046+
}
1047+
1048+
/// <summary>
1049+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
1050+
/// </summary>
1051+
/// <param name="command">The command.</param>
1052+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1053+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataTable" /> object.</returns>
1054+
public Task<DataTable> ExecuteDataTableAsync(DbCommand command, CancellationToken cancellationToken = new CancellationToken())
1055+
{
1056+
return ExecuteDataTableAsync(command, -1, -1, cancellationToken);
1057+
}
1058+
1059+
/// <summary>
1060+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
1061+
/// </summary>
1062+
/// <param name="cmdText">The command text.</param>
1063+
/// <param name="cmdType">The command type.</param>
1064+
/// <param name="parameters">The command parameters.</param>
1065+
/// <param name="startRecord">The zero-based record number to start with.</param>
1066+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
1067+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1068+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataTable" /> object.</returns>
1069+
public Task<DataTable> ExecuteDataTableAsync(string cmdText, CommandType cmdType, Dictionary<string, object> parameters, int startRecord, int maxRecords, CancellationToken cancellationToken = new CancellationToken())
1070+
{
1071+
return ExecuteDataTableAsync(CreateCommand(cmdText, cmdType, parameters), startRecord, maxRecords, cancellationToken);
1072+
}
1073+
1074+
/// <summary>
1075+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
1076+
/// </summary>
1077+
/// <param name="cmdText">The command text.</param>
1078+
/// <param name="cmdType">The command type.</param>
1079+
/// <param name="startRecord">The zero-based record number to start with.</param>
1080+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
1081+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1082+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataTable" /> object.</returns>
1083+
public Task<DataTable> ExecuteDataTableAsync(string cmdText, CommandType cmdType, int startRecord, int maxRecords, CancellationToken cancellationToken = new CancellationToken())
1084+
{
1085+
return ExecuteDataTableAsync(cmdText, cmdType, null, startRecord, maxRecords, cancellationToken);
1086+
}
1087+
1088+
/// <summary>
1089+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
1090+
/// </summary>
1091+
/// <param name="cmdText">The command text.</param>
1092+
/// <param name="cmdType">The command type.</param>
1093+
/// <param name="parameters">The command parameters.</param>
1094+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1095+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataTable" /> object.</returns>
1096+
public Task<DataTable> ExecuteDataTableAsync(string cmdText, CommandType cmdType, Dictionary<string, object> parameters = null, CancellationToken cancellationToken = new CancellationToken())
1097+
{
1098+
return ExecuteDataTableAsync(cmdText, cmdType, parameters, -1, -1, cancellationToken);
1099+
}
1100+
1101+
/// <summary>
1102+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
1103+
/// </summary>
1104+
/// <param name="cmdText">The command text.</param>
1105+
/// <param name="parameters">The command parameters.</param>
1106+
/// <param name="startRecord">The zero-based record number to start with.</param>
1107+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
1108+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1109+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataTable" /> object.</returns>
1110+
public Task<DataTable> ExecuteDataTableAsync(string cmdText, Dictionary<string, object> parameters, int startRecord, int maxRecords, CancellationToken cancellationToken = new CancellationToken())
1111+
{
1112+
return ExecuteDataTableAsync(cmdText, CommandType.Text, parameters, startRecord, maxRecords, cancellationToken);
1113+
}
1114+
1115+
/// <summary>
1116+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
1117+
/// </summary>
1118+
/// <param name="cmdText">The command text.</param>
1119+
/// <param name="startRecord">The zero-based record number to start with.</param>
1120+
/// <param name="maxRecords">The maximum number of records to retrieve.</param>
1121+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1122+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataTable" /> object.</returns>
1123+
public Task<DataTable> ExecuteDataTableAsync(string cmdText, int startRecord, int maxRecords, CancellationToken cancellationToken = new CancellationToken())
1124+
{
1125+
return ExecuteDataTableAsync(cmdText, null, startRecord, maxRecords, cancellationToken);
1126+
}
1127+
1128+
/// <summary>
1129+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataTable" />.
1130+
/// </summary>
1131+
/// <param name="cmdText">The command text.</param>
1132+
/// <param name="parameters">The command parameters.</param>
1133+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1134+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataTable" /> object.</returns>
1135+
public Task<DataTable> ExecuteDataTableAsync(string cmdText, Dictionary<string, object> parameters = null, CancellationToken cancellationToken = new CancellationToken())
1136+
{
1137+
return ExecuteDataTableAsync(cmdText, CommandType.Text, parameters, -1, -1, cancellationToken);
1138+
}
1139+
8821140
/// <summary>
8831141
/// Maps the specified string sql data type value to <see cref="System.Type" />.
8841142
/// </summary>

0 commit comments

Comments
 (0)