Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to retrieve data using ADO.NET DataReader

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report