ExecuteSelectCommandToDataTableAsync
Part of the QSAppContext object. Executes a SELECT command against the database and returns the result as a standard DataTable. This is the lower‑level method that other data‑retrieval helpers build upon.
The command is automatically configured to check for safety (injection prevention) and restricted to Select operations.
Signature
public async Task<DataTable> ExecuteSelectCommandToDataTableAsync(SqlCommandDto command)Parameters
| Name | Type | Description |
|---|---|---|
command | SqlCommandDto | A data‑transfer object describing the SQL command to execute. It should contain the command text, parameters (if any), and connection information. |
Returns
A Task<DataTable> representing the asynchronous operation. The resulting DataTable contains all rows and columns returned by the query. If no rows match, an empty DataTable is returned (never null).
Example Usage
// Build a SqlCommandDto with a parameterised query
SqlCommandDto cmd = new SqlCommandDto
{
CommandText = "SELECT Id, Name, TotalSales FROM QS.Customers WHERE TotalSales > @MinSales"
};
// Use the Parameters collection's convenience methods
cmd.Parameters.AddDecimal("MinSales", 10000M, SqlDbType.Decimal);
// Or the more generic Add method:
// cmd.Parameters.Add("MinSales", 10000M, SqlDbType.Decimal);
// Execute via QSAppContext
DataTable dt = await QSAppContext.ExecuteSelectCommandToDataTableAsync(cmd);
foreach (DataRow row in dt.Rows)
{
Console.WriteLine($"{row["Name"]}: {row["TotalSales"]}");
}