The following is the post of Michael_jackson in this 9CBS community (McLeak ★ Jackson) post (deleted C # part), put it here, I want to be more useful to everyone! 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.
[Visual Basic] DIM MyReader As SqldataReader = 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.
[Visual Basic] do while myreader.read () console.writeline (VBTAB & "{0}" & vbtab & "{1}", myreader.getint32 (0), myreader.getstring (1)) loopMyReader.close () DataReader provides uncomfortable data streams that can effectively process the results returned from the data source in sequential process logic. 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.
[Visual Basic] DIM mycmd as sqlcommand = new sqlcommand ("select categoryid, categoryName from categories;" & _ "Select Employees", Nwindconn Nwindconn () NWINDCONN.Open ()
Dim MyReader as SqldataReader = mycmd.executeRead ()
Dim fNextResult As Boolean = TrueDo Until Not fNextResult Console.WriteLine (vbTab & myReader.GetName (0) & vbTab & myReader.GetName (1)) Do While myReader.Read () Console.WriteLine (vbTab & myReader.GetInt32 (0) & vbtab & myReader.getstring (1)) loop
FnextResult = MyReader.nextResult () loop
MyReader.close () nwindconn.close () Gets schema information from DataReader When the DataReader is turned on, you can retrieve 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.
[Visual Basic] DIM Schematable As DataTable = myReader.getschematable ()
Dim Myrow As DataRowdim Mycol As Datacolumn
For Each myRow In schemaTable.Rows For Each myCol In schemaTable.Columns Console.WriteLine (myCol.ColumnName & "=" & myRow (myCol) .ToString ()) Next Console.WriteLine () NextOLE DB section or sections hierarchical rowsets (OLE DB Type DBTYPE_HCHAPTER, ADO Type Adchapter) You can use OLEDBDataReader to retrieve it. 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.
[Visual Basic] Dim nwindConn As OleDbConnection = New OleDbConnection ( "Provider = MSDataShape; Data Provider = SQLOLEDB;" & _ "Data Source = localhost; Integrated Security = SSPI; Initial Catalog = northwind")
Dim custCMD As OleDbCommand = New OleDbCommand ( "SHAPE {SELECT CustomerID, CompanyName FROM Customers}" & _ "APPEND ({SELECT CustomerID, OrderID FROM Orders} AS CustomerOrders" & _ "RELATE CustomerID TO CustomerID)", nwindConn) nwindConn.Open () DIM Custreader AS OledbDataReader = Custcmd.executeReader () Dim ORDERReader AS OledbDataReader
Do While CustReader.Read () Console.writeline ("Orders for" & CustReader.getstring (1)) 'CustReader.getstring (1) = CompanyName
OrderReader = CustReader.getValue (2) 'CustReader.getValue (2) = Orders Chapter AS DataReader
Do while orderreader.read () console.writeLine (vbtab & orderreader.getint32 (1)) 'OrderReader.GetInt32 (1) = Orderid loop OrderReader.close () loop
CustReader.close () nwindconn.close ()