If you only want to read data without updating the data, you do not have to store it in the dataset. On the contrary, data can be read directly from the database and the application can be read.
In this case, you can use a data reader to simplify the code.
If the program needs to perform continuous read-only transfer through the query results, you can use the data reader object instead of filling the dataset. The data reader object obtains data from the data source and passes the data directly to the application. Generally, the data reader object is used to read-only and only forward access to data when data is not cached in the dataset. (The data adapter uses the data reader object to fill the dataset .) The Web form page that displays database information is an example. Because you need to re-create a Web form page every round-trip, it is often useless to store data in a dataset.
Visual Studio provides four data reader objects, , , And .
OleDbDataReader reads data from an object, which is read-only and directly queried forward. The following code reads all fields in all rows in the table.
P rivate void button2_Click (object sender, EventArgs e)
{
OleDbConnection cn = new OleDbConnection ("Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ Application. startupPath + "\ flList. mdb "+"; Persist Security Info = True ");
Cn. Open ();
Oledbcommand cmd = new oledbcommand ();
Cmd. Connection = cn;
Cmd. commandtext = "select * from List ";
// Oledbdatareader the data reader is read-only
Oledbdatareader reader = cmd. executereader ();
While (reader. Read ())
{
// Debug. writeline (Reader [1]. tostring ());
RichTextBox1.Text + = reader [0]. ToString ();
RichTextBox1.Text + = "\ n ";
RichTextBox1.Text + = reader [1]. ToString ();
RichTextBox1.Text + = "\ n ";
RichTextBox1.Text + = reader [2]. ToString ();
RichTextBox1.Text + = "\ n ";
RichTextBox1.Text + = reader [3]. ToString ();
RichTextBox1.Text + = "\ n ";
}
Reader. Close ();
}