C#连接SQL Server 2005 Express的字符串

 

using System.Data.SqlClient;
public class sqlServerConnection {
//
public static void Main() {

//下面就是标准的连接,注意SERVER是我的计算机名。SQLEXPRESS是数据库实例。SSPI表示使用Windows登陆的帐户。master是要连接的数据库。
//                                      //初始目录//         //综合的,完整的:集成(信息)//
string cstr = "server=SERVER//SQLEXPRESS;Initial Catalog=master;Integrated Security=SSPI";
//
try {
//
using (SqlConnection con = new SqlConnection(cstr)) {
con.Open();
System.Console.WriteLine("ServerVersion:{0}",con.ServerVersion);
System.Console.WriteLine("State:{0}",con.State);
}
//
}
catch (System.Exception sqle) {
System.Console.WriteLine(sqle.Message);
}
//

}
//
}
/*
程序的输出:
ServerVersion:09.00.3042
State:Open
*/

//不使用远程连接的时候应该把连接字符串改为:
"Server=localhost//SQLExpress;Integrated Security=SSPI"
//也可以指定默认的数据库,例如,指定默认数据库是"master"就应该写为:

"Server=localhost//SQLExpress;Initial Catalog=master;Integrated Security=SSPI"

              //此处也可以改成你自己的计算机名,查自己的计算机名称,在我的电脑属性里面//

你可能感兴趣的:(sql,数据库,server,server,C#,Security,express)