SqlDataReader class

xiaoxiao2021-03-06  17

You can use ADO.NET DataReader to retrieve read only, only data streams in the database. Because every time there is always one line in memory, use DataReader to improve the performance of the application and reduce system overhead.

When you create an instance of a Command object, you can call Command.executeReader from the data source to create a DataReader, as shown in the following example. SqlDataReader myReader = mycommand.executeReader (); Using the DataReader object's READ method can get rows from the query results. You can access each column of the return line by delivering the name or serial number of the DataReader. However, in order to achieve the best performance, DataReader provides a range of methods that will enable you to access the column values ​​in our local data type (getDatetime, getDouble, getGuid, getInt32, etc.). For a list of types of accessor methods, see the OLEDBDataReader class and the SqlDataReader class. If you use the Type Accessor Method, you will reduce the amount of type conversion you need to retrieve the column value when an unknown of the underlying data type.

The following code sample loops accesses a DataReader object and returns two columns from each row. While (MyReader.Read ()) console.writeline ("/ t {0} / t {1}", myreader.getint32 (0), myreader.getstring (1)); myreader.close (); DataReader provides uncomfortable buffer Data stream, which process logic can effectively process the result returned from the data source in sequence. Since the data is not cached in memory, DataReader is a suitable choice when retrieving a lot of data.

Close DataReader should call a Close method each time the DataReader object is used.

If the Command contains the output parameter or the return value, these output parameters or return values ​​will not be accessed before the DataReader is off.

Note that when DataReader is turned on, the DataReader will use the Connection in exclusive way. You will not be able to perform any commands for Connection (including create another DataReader) before the initial DataReader is turned off.

Multiple result set If multiple result sets are returned, DataReader provides the nextResult method to loop from these result sets in order, as shown in the following code example. Sqlcommand mycmd = new sqlcommand ("Select CategoryId, CategoryName from Categories;" "Select Employeeid, Lastname from Employees", NWINDCONNNNN.Open ();

Sqldataareader myreader = mycmd.executeReader ();

Do {console.writeline ("/ t {0} / t {1}", myreader.getname (0), MyReader.getname (1));

While (MyReader.Read ()) Console.Writeline ("/ T {0} / t {1}", MyReader.GetInt32 (0), MyReader.getstring (1));

WHILE (MyReader.nextResult ());

MyReader.close (); nwindconn.close (); get schema information from DataReader When the DataReader is turned on, you can search the architecture information about the current result set. GetSchematable will return a DataTable object that populates the rows and columns, which contain the architecture information of the current result set. For each column of the result set, DataTable will contain a line. Each column of the architectural table is mapped to the properties of the columns returned in the result set, where columnName is the name of the property, and the value of the column is the value of the attribute. The following code example writes architecture information for DataReader. DataTable schemaTable = myReader.GetSchemaTable (); foreach (DataRow myRow in schemaTable.Rows) {foreach (DataColumn myCol in schemaTable.Columns) Console.WriteLine (myCol.ColumnName "=" myRow [myCol]); Console.WriteLine ( );} OLE DB chapter hierarchical row set or chapter (OLE DB type DBTYPE_HCHAPTER, ADO Type Adchapter) can use OLEDBDataReader to retrieve. When a query containing a chapter is returned in the form of a DataReader, this section will return in the form of columns in the DataReader and is disclosed as a DataReader object.

ADO.NET DataSet can also be used to represent a hierarchical rowset through a parent-child relationship between the table. For more information, see Creating and using DataSet.

The following code example uses the MSDataShape provider to generate an order column for each customer in the client list. OLEDBCONNECTION NWINDCONN = New OLEDBCONNECTION ("provike = msdatashape; data provike = sqloledb;" "Data Source = localhost; integrated security = sspi; initial catalog = northwind");

OleDbCommand custCMD = new OleDbCommand ( "SHAPE {SELECT CustomerID, CompanyName FROM Customers}" "APPEND ({SELECT CustomerID, OrderID FROM Orders} AS CustomerOrders" "RELATE CustomerID TO CustomerID)", nwindConn); nwindConn.Open ();

OLEDBDATAREADER CUSTREADER = Custcmd.executeReader (); OLEDBDATAREADER ORDERREADER;

While (CustReader.Read ()) {Console.WriteLine ("Orders for" CustReader.getstring (1)); // CustReader.getstring (1) = CompanyName

orderReader = (OleDbDataReader) custReader.GetValue (2); // custReader.GetValue (2) = Orders chapter as DataReaderwhile (orderReader.Read ()) Console.WriteLine ( "/ t" orderReader.GetInt32 (1)); / / OrderReader.getInt32 (1) = OrderId ORDERREADER.CLOSE ();

CustReader.close (); nwindconn.close ();

转载请注明原文地址:https://www.9cbs.com/read-44807.html

New Post(0)