我的连接方法:
public static SqlConnection con()
{
SqlConnectionStringBuilder cnnSb = new SqlConnectionStringBuilder();
cnnSb.DataSource = @"(LocalDb)\MSSQLLocalDb";
cnnSb.AttachDBFilename = @"|DataDirectory|\Database2.mdf";
cnnSb.IntegratedSecurity = true;
SqlConnection kon = new SqlConnection(cnnSb.ToString());
return kon;
}
和Database2.mdf在 App_Data
中创建答案 0 :(得分:0)
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=.\SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=|DataDirectory|Database2.mdf;"
conn.Open();