1、连接字符串
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
"Server=(Local);Database=EduBaseDemo;Integrated Security=sspi"; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
sqlConnection.Open();
sqlConnection.Close();
2、连接字符串构造器
using System.Data.SqlClient; 声明对象
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder() //声明并实例化SQL连接字符串构造器;
{ / /在初始化器中,分别将各控件的内容赋予SQL连接字符串构造器的相应属性;
DataSource = this.txb_Server.Text //数据源(即服务器);
, InitialCatalog = this.txb_Database.Text //初始化条目(即数据库);
, IntegratedSecurity = this.ckb_IsWindowsAuthentication.Checked //集成安全性(即是否Windows验证);
};
sqlConnection.ConnectionString = sqlConnectionStringBuilder.ConnectionString; //SQL连接字符串构造器的连接字符串属性包含了SQL连接所需的连接字符串;
sqlConnection.Open();
sqlConnection.Close();