Using the DataReader Object to Retrieve Records To use the DataReader object to retrieve records, follow these steps: 1. Open the Author class code in the code editor. Add a public method to the class definition called GetAuthorList that returns an generic List of strings: public List<string> GetAuthorList() { } Add the following code, which executes a SQL Select statement to retrieve the authors’ last names. A DataReader object then loops through the records and creates a list of names that gets returned to the client. SqlCommand authorsCommand = new SqlCommand(); SqlDataReader authorDataReader; List<string> nameList = new List<string>(); try { authorsCommand.Connection = _pubConnection; authorsCommand.CommandText = "Select au_lname from authors"; _pubConnection.Open(); authorDataReader = authorsCommand.ExecuteReader(); while (authorDataReader.Read() == true) { nameList.Add(authorDataReader.GetString(0)); } return nameList; } catch (SqlException ex) { throw ex; } finally { if (_pubConnection != null) { _pubConnection.Close(); } } Change the code in the Main Method of the Program class to show the list of names in the console window. static void Main(string[] args) { try { Author author = new Author(); foreach (string name in author.GetAuthorList()) { Console.WriteLine(name); } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } } Select Debug > Start to run the project. The Console window should launch with the names of the authors displayed. After viewing the output, stop the debugger.
Using the DataReader Object to Retrieve Records To use the DataReader object to retrieve records, follow these steps: 1. Open the Author class code in the code editor. Add a public method to the class definition called GetAuthorList that returns an generic List of strings: public List<string> GetAuthorList() { } Add the following code, which executes a SQL Select statement to retrieve the authors’ last names. A DataReader object then loops through the records and creates a list of names that gets returned to the client. SqlCommand authorsCommand = new SqlCommand(); SqlDataReader authorDataReader; List<string> nameList = new List<string>(); try { authorsCommand.Connection = _pubConnection; authorsCommand.CommandText = "Select au_lname from authors"; _pubConnection.Open(); authorDataReader = authorsCommand.ExecuteReader(); while (authorDataReader.Read() == true) { nameList.Add(authorDataReader.GetString(0)); } return nameList; } catch (SqlException ex) { throw ex; } finally { if (_pubConnection != null) { _pubConnection.Close(); } } Change the code in the Main Method of the Program class to show the list of names in the console window. static void Main(string[] args) { try { Author author = new Author(); foreach (string name in author.GetAuthorList()) { Console.WriteLine(name); } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } } Select Debug > Start to run the project. The Console window should launch with the names of the authors displayed. After viewing the output, stop the debugger.
68793928390368.docx