Database Reference
In-Depth Information
//Combining queries to produce multiple result set
string sql = sql1 + sql2;
The only other change is that you loop through result sets. You nest the loop that retrieves rows
inside one that loops through result sets.
// Loop through result sets
do
{
txtResult.AppendText(rdr.GetName(0));
txtResult.AppendText("\t\t");
txtResult.AppendText(rdr.GetName(1));
txtResult.AppendText("\n");
txtResult.AppendText("".PadLeft(30, '='));
txtResult.AppendText("\n");
while (rdr.Read())
{
// Print one row at a time
txtResult.AppendText(rdr[0].ToString());
txtResult.AppendText("\t\t\t");
txtResult.AppendText(rdr[1].ToString());
txtResult.AppendText("\n");
}
}
while (rdr.NextResult());
We have you choose only two character-string columns per query to simplify things. Extending this
to handle result tables with different numbers of columns and column data types is straightforward.
Summary
In this chapter, you used data readers to perform a variety of common tasks, from simply looping
through single result sets to handling multiple result sets. You learned how to retrieve values for
columns by column name and index and learned about methods available for handling values of
different data types. You also learned how to get information about result sets and get schema
information.
In the next chapter, we'll cover the really interesting aspects of ADO.NET: handling database data
while disconnected from the database using DataSets and DataAdapters.
 
Search WWH ::




Custom Search