March 2008
Intermediate to advanced
982 pages
22h 43m
English
You want to connect to a named instance of a SQL Server or SQL Server Express.
You need to understand what a SQL Server or SQL Server Express named instance is and how to connect to one.
The solution creates and opens a connection to a named instance of a SQL Server Express. Information about the SQL Server is displayed from the properties of the SqlConnection object.
The C# code in Program.cs in the project ConnectSqlServerNamedInstance is shown in Example 1-5.
Example 1-5. File: Program.cs for ConnectSqlServerNamedInstance solution
using System;
using System.Data.SqlClient;
namespace ConnectSqlServerNamedInstance
{
class Program
{
static void Main(string[] args)
{
string sqlConnectString = @"Data Source=(local)\SQLExpress;" +
"Integrated security=SSPI;Initial Catalog=master;";
using (SqlConnection connection = new SqlConnection(sqlConnectString))
{
connection.Open( );
// Return some information about the server.
Console.WriteLine("ConnectionString = {0}\n", sqlConnectString);
Console.WriteLine("State = {0}", connection.State);
Console.WriteLine("DataSource = {0}", connection.DataSource);
Console.WriteLine("ServerVersion = {0}", connection.ServerVersion);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey( );
}
}
}The output is shown in Figure 1-4.

Figure 1-4. Output for ConnectSqlServerNamedInstance solution ...
Read now
Unlock full access