Q. Can I use one data reader object to create more than one series?
A. Yes, you can use one data reader object to create more than one series if you use MS SQL Server, because MS SQL Server supports batch queries. Actually, you should use SqlDataReader class' NextResult method which is used to process multiple results, which can be generated by executing batch Transact-SQL statements. By default, the data reader is positioned on the first result.
It looks as follows:
// Initializes a connection string
string myConnectionString = "Integrated Security=SSPI;database=Test;server=ACA";
// Defines the database query
string mySelectQuery="SELECT * FROM Table1; SELECT * FROM Table2; SELECT * FROM Table3";
// Creates a database connection object using the connection string
SqlConnection myConnection = new SqlConnection(myConnectionString);
// Creates a database command on the connection using query
SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
// Opens the connection
myCommand.Connection.Open();
// Creates a database reader
SqlDataReader myReader = myCommand.ExecuteReader();
// Removes all series in the collection
chart1.Series.Clear();
int i = 0;
do
{
// Adds new series
chart1.Series.Add( i.ToString() );
while( myReader.Read() )
{
// Adds new data point to the collection
chart1.Series[i].Points.AddXY( myReader.GetDouble( 1 ), myReader.GetDouble( 2 ) );
}
i++;
}
while( myReader.NextResult() );
// Closes the reader and the connection
myReader.Close();
myConnection.Close();
In this code snippet, we create 3 series from 3 different tables that comprises of 3 columns: Table1 (ID, x_val1, y_val1), Table2 (ID, x_val2, y_val2) and Table3 (ID, x_val3, y_val3).
You cannot use one data reader object to create more than one series if you use MS Access, because MS Access does not support batch queries.
|