Database Reference
In-Depth Information
Again, this is a handy property to display for debugging purposes. If you get an error saying that a
particular table doesn't exist, often the problem isn't that the table doesn't exist but that it isn't in the
database to which you're connected. Displaying the Database property helps you find that kind of error
quickly.
Tip If you specify a database in the connection string that doesn't exist on the server, you may see the
following error message: “System.Data.SqlClient.SqlException: Cannot open database 'database' requested by the
login. The login failed.”
You can change the database currently used on a connection with the ChangeDatabase method,
like so:
Conn.ChangeDatabase("AdventureWorks");
The next statement displays the DataSource property, which gives the server instance name for SQL
Server database connections.
Console.WriteLine( "\tDataSource: {0}", conn.DataSource);
This displays the same SQL Server instance name as per your SQL instance name; for example, in
my case, it will show the following:
DataSource: .\sql2012
The utility of this, again, is mainly for debugging purposes.
The ServerVersion property displays the server version information.
Console.WriteLine("\tServerVersion: {0}",conn.ServerVersion);
It shows the version of SQL Server Express you installed in Chapter 1. (Your version may differ.)
ServerVersion: 11.00.1750
The version number is useful for debugging. This information actually comes from the server, so it
indicates the connection is working.
Note SQL Server 2008 is version 10, and SQL Server 2005 (and SSE) is version 9. SQL Server 2000 is
version 8.
The State property indicates whether the connection is open or closed.
Search WWH ::




Custom Search