/// <summary>
/// Queries the FMStocks7 database
/// <param name='statement'>Sql statement to execute</param>
/// <returns>DataSet filled with the results of running the query</returns>
/// </summary>
SqlCommand command = new SqlCommand( statement, connection );
DataSet dataSet = new DataSet();
command.Connection.Open();
SqlDataReader reader = null;
try {
// We use a reader so we can handle queries that do not
// return record sets. If the query returns records, we add
// them to a table. If it doesn't, then we add a table to
// display the number of affected records.
reader = command.ExecuteReader();
do {
// Create new data table
DataTable schemaTable = reader.GetSchemaTable();
DataTable dataTable = new DataTable();
if ( schemaTable != null )
{
// A query returning records was executed
for ( int i = 0; i < schemaTable.Rows.Count; i++ )
{
DataRow dataRow = schemaTable.Rows[ i ];
// Create a column name that is unique in the data table
string columnName = ( string )dataRow[ "ColumnName" ] + "<C" + i + "/>";
// Add the column definition to the data table
DataColumn column = new DataColumn( columnName, ( Type )dataRow[ "DataType" ] );
dataTable.Columns.Add( column );
}
dataSet.Tables.Add( dataTable );
// Fill the data table we just created
while ( reader.Read() )
{
DataRow dataRow = dataTable.NewRow();
for ( int i = 0; i < reader.FieldCount; i++ )
dataRow[ i ] = reader.GetValue( i );
dataTable.Rows.Add( dataRow );
}
}
else
{
// No records were returned
DataColumn column = new DataColumn( RowsAffected );
dataTable.Columns.Add(column);
dataSet.Tables.Add( dataTable );
DataRow dataRow = dataTable.NewRow();
dataRow[0] = reader.RecordsAffected;
dataTable.Rows.Add( dataRow );
}
}
while ( reader.NextResult() );
}
catch ( SqlException e )
{
// There was an error executing the query, display it to the user