Database Reference
In-Depth Information
Figure 12-3. Displaying connection information
How It Works
The ConnectionString property can be both read and written. Here you just display it.
Console.WriteLine("\tConnection String: {0}", conn.ConnectionString);
You can see the value you assign to it, including the whitespace, in the verbatim string.
What's the point? Well, it's handy when debugging connections to verify that the connection string
really contains the values you thought you assigned. For example, if you're trying different connection
options, you may have different connection string parameters in the program. You may have
commented out one, intending to use it later, but forgot about it. Displaying the ConnectionString
property helps to see whether a parameter is missing.
The next statement displays the Database property. Since each SQL Server instance has several
databases, this property shows which one you're initially using when you connect.
Console.WriteLine("\tDatabase: {0}",conn.Database);
In this program, it displays
Database: master
since you didn't specify a database in the connection string, so you were connected to the SQL Server's
default database master. If you wanted to connect to the AdventureWorks or a database of your choice,
you'd need to specify the Database parameter, for example:
// connection string
string connString = new SqlConnection(@"server = .\sqlexpress; database = northwind ";
integrated security = true;)
You can also change the default database from the master database to some other database, say,
AdventureWorks, by executing the following statement:
exec sp_defaultdb 'sa'/adventureworks'
 
 
Search WWH ::




Custom Search