我试图在连接字符串中连接数据库名称,如下所示:
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
string connectionString = "Server=My-PC\\SQLEXPRESS; Database="+name+";Integrated Security=True; App=EntityFramework;";
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
connectionStringField.SetValue(this, connectionString);
}
但它抛出异常:数据库登录失败。 当我硬编码数据库名称时,它可以工作:
string connectionString = "Server=My-PC\\SQLEXPRESS;
Database=mydb;Integrated Security=True; App=EntityFramework;";
任何人都可以指导我做错了什么。
感谢名单
修改
我也试过这个:
`string domaniName = Helpers.RouteManager.GetSubDomain();
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder["Data Source"] = "MY-PC\\SQLEXPRESS";
sqlBuilder["Initial Catalog"] = domaniName+"_db";
sqlBuilder["Integrated Security"] = true;
sqlBuilder["Application Name"] = "EntityFramework";`
但仍然得到相同的结果。当我硬编码dbname时,它工作正常。
请指导我。
感谢名单。