SqlConnectionStringBuilder提供了一个很好的构建SQL连接字串的方式。不多说,见代码:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder
{
DataSource = "(localdb)\\MSSQLLocalDB",
InitialCatalog = "UserManage",
//UserID = "adb",
//Password = "aaa",
IntegratedSecurity = true,//是否使用windows登录相同的用户名和密码
MultipleActiveResultSets = true,//是否允许一次连接多次查询
ApplicationName="EntityFramework"//标识应用程序名称,可以自定义,供在SQL跟踪调试时用
};
string connStr = builder.ConnectionString;
上面代码得到的连接字串为:data source=(localdb)\\MSSQLLocalDB;initial catalog=UserManage;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework
Namespace: System.Data.SqlClient
Assembly: System.Data (in System.Data.dll)