the previous ExecuteReader () method, which involves the command object, returns a DataReader object, so we'll describe the Datareade object in detail.
The following example uses the same data table as in the previous article for the Manager data table in the Customermanagement database:
DataReader Object Overview
The DataReader object provides a sequential, read-only way of reading the data result set obtained by the Command object. Because DataReader reads the data sequentially, DataReader opens the database connection in an exclusive manner.
Because DataReader only performs read operations and stores only one piece of the result set in the memory buffer at a time, using DataReader objects is relatively efficient, and if you are querying large amounts of data, colleagues do not need to randomly access and modify the data, DataReader is the preferred choice. The DataReader object has many properties and methods:
Determine if there is data in the query results
To determine whether there is data in the query results, you only need to determine the HasRows property of the DataReader object.
Example one, use the above method to determine whether the Manager data table in the Customermanagement database has the complete code for the data:
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.data.sqlclient;//introduces a namespace namespace consoleapplication1{class program {static void Main (string[] args) {string constr = "server=.; User=sa;pwd=123456;database=customermanagement ";//connection string SqlConnection ConText = new SqlConnection (CONSTR);//Create C Onnection Object try {context.open ();//Open database String sql = "Select * FROM M Anager ";//Create STATISTICAL statement SqlCommand Comtext = new SqlCommand (sql, conText);//create Command object Sqldataread Er dr;//create DataReader object Dr=comtext.executereader ();//Execute query if (Dr. HasRows)//Determine if the data table contains data {Console.WriteLine ("The Manager data sheet contains data"); } else {Console.WriteLine ("No data in the Manager data table"); }} catch (Exception ex)//create check Exception object {Console.WriteLine (ex. Message.tostring ());//Output error message} finally {context.close ();//close connection } console.readline (); }}}</span>
The result of the operation is:
Reading data
To read the data in the DataReader object, it is necessary to use the Read method of the DataReader object, since the DataReader object stores only one piece of data in the result set in the memory buffer at a time, so it is necessary to read multiple data in the DataReader object. The iteration statement is used.
Example two, the complete code for reading the data from the Manager data table in the Customermanagement database through the Read method of the DataReader object is:
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.data.sqlclient;//introduces a namespace namespace consoleapplication1{class program {static void Main (string[] args) {string constr = "server=.; User=sa;pwd=123456;database=customermanagement ";//connection string SqlConnection ConText = new SqlConnection (CONSTR);//Create C Onnection Object try {context.open ();//Open database String sql = "Select * FROM M Anager ";//Create STATISTICAL statement SqlCommand Comtext = new SqlCommand (sql, conText);//create Command object Sqldataread Er dr;//create DataReader object Dr=comtext.executereader ();//Execute Query while (Dr. Read ())//Determine if the data table contains data {Console.Write (dr[0]. ToString () + ",");//output User ID Console.Write (dr["UserName"]. ToString () + ",");//output User name Console.WriteLine (dr[2]. ToString ());//output User password} Dr. Close ()//Closed DataReader object} catch (Exception ex)//create check Exception object {Console . WriteLine (ex. Message.tostring ());//Output error message} finally {context.close ();//close connection } console.readline (); }}}</span>
The result of the operation is:
The results of the read are consistent with the data in the Manage data table in the Customermanagement database.
Read data from C #: DataReader object