Free mag vol1 | Page 886

CHAPTER 21  ADO.NET PART I: THE CONNECTED LAYER provides a number of members that let you configure additional settings regarding your connection, such as timeout settings and transactional information. Table 21-5 lists some (but not all) members of the DbConnection base class. Table 21-5. Members of the DbConnection Type Member Meaning in Life BeginTransaction() You use this method to begin a database transaction. ChangeDatabase() You use this method to change the database on an open connection. ConnectionTimeout This read-only property returns the amount of time to wait while establishing a connection before terminating and generating an error (the default value is 15 seconds). If you would like to change the default, specify a Connect Timeout segment in the connection string (e.g., Connect Timeout=30). Database This read-only property gets the name of the database maintained by the connection object. DataSource This read-only property gets the location of the database maintained by the connection object. GetSchema() This method returns a DataTable object that contains schema information from the data source. State This read-only property gets the current state of the connection, which is represented by the ConnectionState enumeration. The properties of the DbConnection type are typically read-only in nature and are only useful when you want to obtain the characteristics of a connection at runtime. When you need to override default settings, you must alter the construction string itself. For example, the connection string sets the connection timeout setting from 15 seconds to 30 seconds: static void Main(string[] args) { Console.WriteLine("***** Fun with Data Readers *****\n"); using(SqlConnection cn = new SqlConnection()) { cn.ConnectionString = @"Data Source=(local)\SQLEXPRESS;" + "Integrated Security=SSPI;Initial Catalog=AutoLot;Connect Timeout=30"; cn.Open(); // New helper function (see below). ShowConnectionStatus(cn); ... } 832