无法使用c#在本地数据库上连接

时间:2017-05-23 12:46:55

标签: c# asp.net sql-server database connection

我的连接方法:

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

中创建

1 个答案:

答案 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();