September 2003
Intermediate to advanced
624 pages
14h 27m
English
You want to connect to a named instance of a SQL Server or Microsoft Data Engine (MSDE).
You need to understand what a SQL Server or MSDE named instance is and how to connect to one. The sample code contains a single event handler:
Button.Click Creates and opens a connection to a named instance of a SQL Server.
Information about the SQL Server is displayed from the properties of
the SqlConnection object.
The C# code is shown in Example 1-6.
Example 1-6. File: ConnectNamedInstanceForm.cs
// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Windows.Forms;
using System.Text;
using System.Data.SqlClient;
// . . .
private void connectButton_Click(object sender, System.EventArgs e)
{
StringBuilder result = new StringBuilder( );
SqlConnection conn = new SqlConnection(
ConfigurationSettings.AppSettings["Sql_Msde_ConnectString"]);
try
{
conn.Open( );
// Return some information about the server.
result.Append(
"ConnectionState = " + conn.State + Environment.NewLine +
"DataSource = " + conn.DataSource + Environment.NewLine +
"ConnectionState = " + conn.State + Environment.NewLine +
"ServerVersion=" + conn.ServerVersion +
Environment.NewLine);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close( );
}
result.Append("ConnectionState = " + conn.State);
resultTextBox.Text = result.ToString( );
}SQL Server 2000 introduced ...
Read now
Unlock full access