Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use ADODataReader class to read data

Shulou Source: shulou.com Published: 2022-06-02 08:04:24 10月15日 Update

This article introduces the relevant knowledge of "how to read data with ADODataReader class". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

1. We use ADOConnection to open a database.

ADOConnection conn = new ADOConnection(DB_CONN_STRING); conn.Open();

2. We write an SQL statement to define the data to be fetched. The result of this data execution is to return an ADODataReader class object. Note the out keyword in the Execute method. This in C#means passing parameters by reference.

ADODataReader dr; ADOCommand cmd = new ADOCommand( "SELECT * FROM Person", conn ); cmd.Execute( out dr);

3. We loop through each record in the ADODataReader class until we're done. Note: Data is returned directly as a string and data field names are used to indicate the data field being read.

while( dr.Read() ) { System.Console.WriteLine( dr["FirstName"] ); }

4. Let's call it a day.

But, as good programmers, we also need to add a lot of try/catch/finally statements to make sure we handle all the errors.

try { .... Database operations... } catch( Exception ex ) { System.Console.WriteLine( "READING:" ); System.Console.WriteLine( " ERROR:" + ex.Message ); System.Console.WriteLine( " SQL :" + sSqlCmd ); System.Console.WriteLine( " Conn.: " + DB_CONN_STRING ); } finally { //Close the connection if( conn.State == DBObjectState.Open ) conn.Close(); }" How to read data with ADODataReader class "is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Data content database more knowledge statements practical learned next keys keywords parameters at the same time dilemmas domain names characters strings actual objects situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB macOS NVidia OPPO Reno