using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ConnectionDisplay { class Program { static void Main(string[] args) { SqlConnection conn = new SqlConnection(@" server=.; user id=administrator; integrated security =true; "); try { conn.Open(); Console.WriteLine("Connection opened."); 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) { Console.WriteLine("Error: " + e); } finally { conn.Close(); Console.WriteLine("Connection close."); } Console.ReadKey(); } } }
这里未在连接字符串中指定数据库,所以它连接到SQL Server的默认数据库master。如果想连接到Northwind,则需要指定Database参数。