Oledbdatareader data reader

Source: Internet
Author: User

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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.