我无法使用以下代码连接到现有的MDF文件:
conn = new SqlConnection("Data Source=(LocalDB)\\v11.0;" +
"Integrated Security=True;" +
"AttachDbFilename=" + Path.GetFullPath("Database.mdf") + ";");
conn.Open();无论我尝试什么,我都会得到以下错误:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.)发布于 2015-12-11 17:25:25
有趣的是,不久前我也有过同样的问题。你说过不管你做什么都不行。不过,你在某个地方有个打字错误。这是我以前的那个:
public const string ssqlconnectionstring = @"Data Source=(LocalDB)\v11.0;" +
@"AttachDbFilename=C:\MyFilePath\MyDBFile.mdf;" +
@"Integrated Security=True;" +
@"MultipleActiveResultSets=True;" +
@"User Instance=False";另外,你可能会用
"(LocalDB)\MSSQLLocalDB";而不是\v11.0我对此有意见。如果您遇到某种版本或兼容性问题,可能就是它。
此外,“环境”在我的代码中发生了一些变化,并将DB用作实体框架的项目项。这样,Visual就会生成连接字符串。在这里,我在一个配置文件中找到了它:
<Value Profile="(Default)">Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\MyFilePath\MyDBFile.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework</Value>https://stackoverflow.com/questions/34228667
复制相似问题