Microsoft Visual C#.NET 2003 Kick Start
Data readers let you read data from a data source field by field in a low-level way, and you use them when speed is an issue. You can create data readers only in codethere are no data reader objects in the toolbox. We'll take a look at data readers here in the ch10_04 example, which you can see at work in Figure 10.5. When the user clicks the Read Data button in this example, a data reader reads the data in the authors table field by field and we display that data in the text boxes you see in the figure. Figure 10.5. The ch10_04 example.
There are four types of data readers OleDbDataReader , SqlDataReader , OdbcDataReader , and OracleDataReader and we'll start this topic by taking a look at them in overview. Working with the OleDbDataReader Class
The OleDbDataReader class creates a data reader for use with an OLE DB data provider. You can find the significant public properties of OleDbDataReader objects in Table 10.25, and their significant methods in Table 10.26. Table 10.25. Significant Public Properties of OleDbDataReader Objects
Table 10.26. Significant Public Methods of OleDbDataReader Objects
Working with the SqlDataReader Class
The SqlDataReader class creates a data reader for use with the SQL Server; the SqlDataReader class has the same significant public properties and methods as the OleDbDataReader class, with the additional significant methods you see in Table 10.27. Table 10.27. Additional Significant Public Methods of SqlDataReader Objects
Working with the OdbcDataReader Class
The OdbcDataReader class lets you create a data reader for use with an ODBC data provider. The OdbcDataReader class has the same significant public properties and methods as the OleDbDataReader class. Working with the OracleDataReader Class
The OracleDataReader class lets you create a data reader for use with the Oracle data provider. The OracleDataReader class has the same significant public properties and methods as the OleDbDataReader class, with some additional significant methods, as you see in Table 10.28. Table 10.28. Additional Significant Public Methods of OracleDataReader Objects
Creating Data Readers in Code
You create data readers with the ExecuteReader method of a command object. In this example, we'll connect to the authors table using the OLE DB protocol. Here's how you create a new OLE DB data reader:
private void button1_Click(object sender, System.EventArgs e) { string connection1String = "Provider=SQLOLEDB;" + "Data Source=;User ID=sa;Initial Catalog=pubs;"; OleDbConnection connection1 = new OleDbConnection(connection1String); OleDbCommand command1 = new OleDbCommand("select * from authors", connection1); connection1.Open(); OleDbDataReader reader1 = command1.ExecuteReader(CommandBehavior.CloseConnection); . . . This data reader gives us access to the authors table. If we want to reproduce the kind of field-by-field display we saw in the previous example, ch10_04, we'll need to start by getting the name of each column in the authors table. Unfortunately, data readers are very simple data objects, designed to return data from the fields in a table, one after the next. To get the names of the columns in a table takes a little more work, but it can be done. You do that by using the data reader's GetSchemaTable method to get the XML schema for the table, which lets you retrieve the name of each column this way:
private void button1_Click(object sender, System.EventArgs e) { string connection1String = "Provider=SQLOLEDB;" + "Data Source=;User ID=sa;Initial Catalog=pubs;"; OleDbConnection connection1 = new OleDbConnection(connection1String); . . . DataTable schemaTable = reader1.GetSchemaTable(); textBox1.Text += schemaTable.Rows[0][0].ToString() + "\r\n"; textBox2.Text += schemaTable.Rows[1][0].ToString() + "\r\n"; textBox3.Text += schemaTable.Rows[2][0].ToString() + "\r\n"; textBox4.Text += schemaTable.Rows[3][0].ToString() + "\r\n"; textBox1.Text += "--------------" + "\r\n"; textBox2.Text += "--------------" + "\r\n"; textBox3.Text += "--------------" + "\r\n"; textBox4.Text += "--------------" + "\r\n"; . . . Data readers return the data from field after field in your data source. You use methods like GetBoolean , GetString , and GetDouble to read the actual data from a data reader, which means you must know the data type of the field you're fetching data from. You can determine that type using the XML schema for the table so you know the data-reading method to use. Here's what that looks like in the ch10_04 example, where we're checking for both string and boolean values:
private void button1_Click(object sender, System.EventArgs e) { string connection1String = "Provider=SQLOLEDB;" + "Data Source=;User ID=sa;Initial Catalog=pubs;"; OleDbConnection connection1 = new OleDbConnection(connection1String); . . . while (reader1.Read()) { if (schemaTable.Rows[0][5].ToString() == "System.String") { textBox1.Text += reader1.GetString(0) + "\r\n"; } if (schemaTable.Rows[0][5].ToString() == "System.Boolean") { textBox1.Text += reader1.GetBoolean(0).ToString() + "\r\n"; } if (schemaTable.Rows[1][5].ToString() == "System.String") { textBox2.Text += reader1.GetString(1) + "\r\n"; } if (schemaTable.Rows[1][5].ToString() == "System.Boolean") { textBox2.Text += reader1.GetBoolean(1).ToString() + "\r\n"; } if (schemaTable.Rows[2][5].ToString() == "System.String") { textBox3.Text += reader1.GetString(2) + "\r\n"; } if (schemaTable.Rows[2][5].ToString() == "System.Boolean") { textBox3.Text += reader1.GetBoolean(2).ToString() + "\r\n"; } if (schemaTable.Rows[3][5].ToString() == "System.String") { textBox4.Text += reader1.GetString(3) + "\r\n"; } if (schemaTable.Rows[3][5].ToString() == "System.Boolean") { textBox4.Text += reader1.GetBoolean(3).ToString() + "\r\n"; } } reader1.Close(); connection1.Close(); } You can see the results of this code in Figure 10.5, where you see the first four fields of the records in the authors table. |