Get the App
SLTechnology News&Howtos  ›  Development  › 

How to retrieve data using ADO.NET DataReader

Shulou Source: shulou.com Published: 2022-06-02 18:35:19 09月15日 Update

This article mainly introduces how to use ADO.NET DataReader to retrieve data, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Retrieving data using ADO.NET DataReader includes creating an instance of the Command object, and then creating a DataReader by calling Command.ExecuteReader to retrieve rows from the data source. The following example shows how to retrieve data using ADO.NET DataReader, where reader represents a valid DataReader and command represents a valid Command object.

Reader=command.ExecuteReader ()

Use the Read method of the DataReader object to get rows from the query results. You can access each column of the returned row by passing the name or ordinal reference of the column to DataReader. However, for * * performance, DataReader provides a series of methods that will give you access to the column values of its native data types (GetDateTime, GetDouble, GetGuid, GetInt32, and so on). For a list of typed accessor methods for a data provider-specific DataReaders, see OleDbDataReader and SqlDataReader. Assuming that the underlying data type is known, using the typed accessor method reduces the amount of type conversion required to retrieve column values.

The WindowsServer2003 version of the .NET Framework contains an additional property of DataReader, HasRows, which enables you to determine whether DataReader returns any results before it is read. The following code example iterates through a DataReader object and returns two columns from each row.

PrivateSubHasRows (ByValconnectionAsSqlConnection) Usingconnection DimcommandAsSqlCommand=NewSqlCommand (_ "SELECTCategoryID,CategoryNameFROMCategories;", _ connection) connection.Open () DimreaderAsSqlDataReader=command.ExecuteReader () Ifreader.HasRowsThen DoWhilereader.Read () Console.WriteLine (reader.GetInt32 (0) _ & vbTab&reader.GetString (1) Loop Else Console.WriteLine ("Norowsfound.") EndIf reader.Close () EndUsing EndSub

DataReader provides an unbuffered data stream that enables process logic to effectively process the results returned from the data source sequentially. Because the data is not cached in memory, DataReader is a suitable choice when ADO.NET DataReader retrieves too much data. Close DataReader. The Close method should be called every time you finish using the DataReader object. If Command contains output parameters or return values, these output parameters or return values will not be accessible until DataReader is turned off.

Note that when DataReader is turned on, the DataReader will use Connection exclusively. No commands can be executed on Connection (including creating another DataReader) until the original DataReader is shut down. Do not call Close or Dispose on Connection, DataReader, or any other managed object in the Finalize method of the class. In the finalizer, only unmanaged resources that are directly owned by the class are released. If the class does not own any unmanaged resources, do not include the Finalize method in the class definition.

Thank you for reading this article carefully. I hope the article "how to use ADO.NET DataReader to retrieve data" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Data retrieval method object type article valid result managed parameter property data flow data source example resource output original two code value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Microsoft NVidia Linux