Skip to content

Commit c95c93f

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

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

src/DotNetToolkit.Repository.AdoNet/DbHelper.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,64 @@ public DataTable ExecuteDataTable(string cmdText, Dictionary<string, object> par
692692
return ExecuteDataTable(cmdText, CommandType.Text, parameters, -1, -1);
693693
}
694694

695+
/// <summary>
696+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataSet" />.
697+
/// </summary>
698+
/// <param name="command">The command.</param>
699+
/// <returns>A <see cref="System.Data.DataSet" /> object.</returns>
700+
public DataSet ExecuteDataSet(DbCommand command)
701+
{
702+
Guard.NotNull(command, nameof(command));
703+
704+
var connection = command.Connection;
705+
var ownsConnection = _ownsConnection && command.Transaction == null;
706+
var canCloseConnection = false;
707+
708+
if (connection.State == ConnectionState.Closed)
709+
{
710+
connection.Open();
711+
canCloseConnection = true;
712+
}
713+
714+
LogExecutingCommandQuery(command);
715+
716+
var adapter = _factory.CreateDataAdapter();
717+
718+
adapter.SelectCommand = command;
719+
720+
var ds = new DataSet();
721+
722+
adapter.Fill(ds);
723+
724+
if (canCloseConnection && ownsConnection)
725+
connection.Dispose();
726+
727+
return ds;
728+
}
729+
730+
/// <summary>
731+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataSet" />.
732+
/// </summary>
733+
/// <param name="cmdText">The command text.</param>
734+
/// <param name="cmdType">The command type.</param>
735+
/// <param name="parameters">The command parameters.</param>
736+
/// <returns>A <see cref="System.Data.DataSet" /> object.</returns>
737+
public DataSet ExecuteDataSet(string cmdText, CommandType cmdType, Dictionary<string, object> parameters = null)
738+
{
739+
return ExecuteDataSet(CreateCommand(cmdText, cmdType, parameters));
740+
}
741+
742+
/// <summary>
743+
/// Sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataSet" />.
744+
/// </summary>
745+
/// <param name="cmdText">The command text.</param>
746+
/// <param name="parameters">The command parameters.</param>
747+
/// <returns>A <see cref="System.Data.DataSet" /> object.</returns>
748+
public DataSet ExecuteDataSet(string cmdText, Dictionary<string, object> parameters = null)
749+
{
750+
return ExecuteDataSet(cmdText, CommandType.Text, parameters);
751+
}
752+
695753
/// <summary>
696754
/// Asynchronously executes a SQL statement against a connection.
697755
/// </summary>
@@ -1137,6 +1195,67 @@ public DataTable ExecuteDataTable(string cmdText, Dictionary<string, object> par
11371195
return ExecuteDataTableAsync(cmdText, CommandType.Text, parameters, -1, -1, cancellationToken);
11381196
}
11391197

1198+
/// <summary>
1199+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataSet" />.
1200+
/// </summary>
1201+
/// <param name="command">The command.</param>
1202+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1203+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataSet" /> object.</returns>
1204+
public async Task<DataSet> ExecuteDataSetAsync(DbCommand command, CancellationToken cancellationToken = new CancellationToken())
1205+
{
1206+
Guard.NotNull(command, nameof(command));
1207+
1208+
var connection = command.Connection;
1209+
var ownsConnection = _ownsConnection && command.Transaction == null;
1210+
var canCloseConnection = false;
1211+
1212+
if (connection.State == ConnectionState.Closed)
1213+
{
1214+
await connection.OpenAsync(cancellationToken);
1215+
canCloseConnection = true;
1216+
}
1217+
1218+
LogExecutingCommandQuery(command);
1219+
1220+
var adapter = _factory.CreateDataAdapter();
1221+
1222+
adapter.SelectCommand = command;
1223+
1224+
var ds = new DataSet();
1225+
1226+
adapter.Fill(ds);
1227+
1228+
if (canCloseConnection && ownsConnection)
1229+
connection.Dispose();
1230+
1231+
return ds;
1232+
}
1233+
1234+
/// <summary>
1235+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataSet" />.
1236+
/// </summary>
1237+
/// <param name="cmdText">The command text.</param>
1238+
/// <param name="cmdType">The command type.</param>
1239+
/// <param name="parameters">The command parameters.</param>
1240+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1241+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataSet" /> object.</returns>
1242+
public Task<DataSet> ExecuteDataSetAsync(string cmdText, CommandType cmdType, Dictionary<string, object> parameters = null, CancellationToken cancellationToken = new CancellationToken())
1243+
{
1244+
return ExecuteDataSetAsync(CreateCommand(cmdText, cmdType, parameters), cancellationToken);
1245+
}
1246+
1247+
/// <summary>
1248+
/// Asynchronously sends the query string to the <see cref="DbConnection" /> and builds a <see cref="DataSet" />.
1249+
/// </summary>
1250+
/// <param name="cmdText">The command text.</param>
1251+
/// <param name="parameters">The command parameters.</param>
1252+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
1253+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a <see cref="System.Data.DataSet" /> object.</returns>
1254+
public Task<DataSet> ExecuteDataSetAsync(string cmdText, Dictionary<string, object> parameters = null, CancellationToken cancellationToken = new CancellationToken())
1255+
{
1256+
return ExecuteDataSetAsync(cmdText, CommandType.Text, parameters, cancellationToken);
1257+
}
1258+
11401259
/// <summary>
11411260
/// Maps the specified string sql data type value to <see cref="System.Type" />.
11421261
/// </summary>

0 commit comments

Comments
 (0)