Let’s write a program to display connection information:
1. Add a C# Console Application project named ConnectionDisplayto the Chapter05 solution.
2. Rename Program.csto ConnectionDisplay.cs. Replace the code with that in Listing 5-2.
Listing 5-2.ConnectionDisplay.cs using System;
using System.Data;
using System.Data.SqlClient;
namespace Display {
class Display {
static void Main() {
// Connection string string connString = @"
server = (local)\netsdk;
integrated security = sspi;
";
// Create connection
SqlConnection conn = new SqlConnection(connString);
C H A P T E R 5 ■ I N T R O D U C I N G C O N N E C T I O N S 106
try {
// Open connection conn.Open();
Console.WriteLine("Connection opened.");
// Display connection properties
Console.WriteLine("Connection Properties:");
Console.WriteLine(
"\tConnection String: {0}", conn.ConnectionString);
Console.WriteLine(
"\tDatabase: {0}", conn.Database);
Console.WriteLine(
"\tDataSource: {0}", conn.DataSource);
Console.WriteLine(
"\tServerVersion: {0}", conn.ServerVersion);
Console.WriteLine(
"\tState: {0}", conn.State);
Console.WriteLine(
"\tWorkstationId: {0}", conn.WorkstationId);
}
catch (SqlException e) { // Display error
Console.WriteLine("Error: " + e);
}
finally {
// Close connection conn.Close();
Console.WriteLine("Connection closed.");
} } } }
3. Make it the startup project, and run it with Ctrl+F5. If the connection is successful, you’ll see output like that shown in Figure 5-3.
C H A P T E R 5 ■ I N T R O D U C I N G C O N N E C T I O N S 107
How It Works
The ConnectionStringproperty can be both read and written. Here you just display it:
Console.WriteLine(
"\tConnection String: {0}", conn.ConnectionString);
You’ll see the value you assigned 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 out 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 ConnectionStringproperty helps you to see that a parameter is missing.
The next statement displays the Databaseproperty. 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, you were connected to the default database, which for this SSE instance is master. If you want to connect to the Northwind database, then you’ll need to specify the Databaseparameter, for example:
C H A P T E R 5 ■ I N T R O D U C I N G C O N N E C T I O N S 108
Figure 5-3.Displaying connection information
// Connection string string connString = @"
server = (local)\netsdk;
integrated security = sspi;
database = northwind
";
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 to 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 this error: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.
The next statement displays theDataSourceproperty, which gives the server instance name for SQL Server database connections:
Console.WriteLine(
"\tDataSource: {0}", conn.DataSource);
In this program, this statement displays the same SQL Server instance name you’ve used in all the examples so far.
DataSource: .\sqlexpress
The utility of this, again, is mainly for debugging purposes.
The ServerVersionproperty displays the server version information:
Console.WriteLine(
"\tServerVersion: {0}", conn.ServerVersion);
It shows the version of SSE you installed in Chapter 1. (Your version may differ.)
C H A P T E R 5 ■ I N T R O D U C I N G C O N N E C T I O N S 109
ServerVersion: 09.00.1399
The version number is useful for debugging. This information actually comes from the server, so it indicates the connection is working.
■ Note SQL Server 2005 (and SSE) is version 9. SQL Server 2000 is version 8.
The Stateproperty indicates whether the connection is open or closed:
Console.WriteLine(
"\tState: {0}", conn.State);
Since you display this property after the Open()call, it shows that the connection is open:
State: Open
You’ve been displaying your own message that the connection is open, but this property contains the current state. If the connection is closed, then the Stateproperty would be Closed.
You then display the workstation ID, a string identifying the client computer. The WorkstationIdproperty is specific to SQL Server and can be handy for debugging:
Console.WriteLine(
"\tWorkstationId: {0}", conn.WorkstationId);
It defaults to the computer name. Our computer is named JQT, but yours, of course, will be different:
WorkstationId: JQT
What makes this useful for debugging is that the SQL Server tools on the server can display which workstation ID issued a particular command. If you don’t know which machine is causing a problem, you can modify your programs to display the WorkstationIdproperty and compare it to the workstation IDs displayed on the server.
C H A P T E R 5 ■ I N T R O D U C I N G C O N N E C T I O N S 110
You can also set this property with the Workstation IDconnection string parameter as follows, so if you want all the workstations in, say, Building B to show that information on the server, you can indicate that in the program:
// Connection string string connString = @"
server = .\sqlexpress;
workstation id = Building B;
integrated security = true;
";
That completes the discussion of the fundamentals of connecting to SQL Server with SqlClient. Now let’s look at connecting with another data provider.