1、Connection对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Connection
{
class Program
{
static void Main(string[] args)
{
//构造连接字符串
SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder();
connStr.DataSource = @".SQLEXPRESS";
connStr.InitialCatalog = "master";
connStr.IntegratedSecurity = true;
SqlConnection conn = new SqlConnection();//创建连接对象
conn.ConnectionString = connStr.ConnectionString;//设置连接字符串
conn.Open();//打开连接
if(conn.State == ConnectionState.Open)
{
Console.WriteLine("Database is linked.");
Console.WriteLine("
DataSource:{0}",conn.DataSource);
Console.WriteLine("Database:{0}",conn.Database);
Console.WriteLine("ConnectionTimeOut:{0}",conn.ConnectionTimeout);
}
conn.Close();//关闭连接
conn.Dispose();//释放资源
if(conn.State == ConnectionState.Closed)
{
Console.WriteLine("
Database is closed.");
}
Console.Read();
}
}
}