Free mag vol1 | Page 881

CHAPTER 21  ADO.NET PART I: THE CONNECTED LAYER cn.Open(); // Make command object. DbCommand cmd = df.CreateCommand(); Console.WriteLine("Your command object is a: {0}", cmd.GetType().Name); cmd.Connection = cn; cmd.CommandText = "Select * From Inventory"; // Print out data with data reader. using (DbDataReader dr = cmd.ExecuteReader()) { Console.WriteLine("Your data reader object is a: {0}", dr.GetType().Name); Console.WriteLine("\n***** Current Inventory *****"); while (dr.Read()) Console.WriteLine("-> Car #{0} is a {1}.", dr["CarID"], dr["Make"].ToString()); } } } Console.ReadLine(); Notice that, for diagnostic purposes, you use reflection services to print the name of the underlying connection, command, and data reader. If you run this application, you will find the following current data in the Inventory table of the AutoLot database printed to the console: ***** Fun with Data Provider Factories ***** Your connection object is a: SqlConnection Your command object is a: SqlCommand Your data reader object is a: SqlDataReader ***** Current Inventory ***** -> Car #32 is a VW. -> Car #83 is a Ford. -> Car #872 is a Saab. -> Car #888 is a Yugo. -> Car #1000 is a BMW. -> Car #1011 is a BMW. -> Car #2911 is a BMW. Now change the *.config file to specify System.Data.OleDb as the data provider (and update your connection string with a Provider segment), like so: