2016-12-04 66 views
1

我正在C#/ ASP.NET中处理一个项目,并且遇到了一个我无法解决的错误。我有我创造了一些表的数据库,并且每当应用程序试图打开数据库时,我收到以下错误:打开数据库时无法打开物理文件

"Unable to open the physical file "path\App_Data\Cars.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)". An attempt to attach an auto-named database for file path\App_Data\Cars.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share." 

我的是SQL Express 2014安装,命名实例“SQLEXPRESS”,写了这Visual - >选项 - >工具 - >数据连接中的实例ID。在web.config文件中,我有:

< connectionStrings>   
< add name="ConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='path\App_Data\Cars.mdf';Integrated Security=True;User Instance=True"/>  
< add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='path\App_Data\AspnetDB.mdf';Integrated Security=True;User Instance=True"/> 
< /connectionStrings> 

,我试图打开数据库的方式是:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Cars.mdf;Integrated Security=True"); 
con.Open(); 

你能告诉我该怎么办?谢谢!

更新:管理解决它,SqlConnection中的参数应该与web.config中的ConnectionString相同。现在还有另一个:当我尝试打开Visual Studio提供的默认数据库(使用用户,角色,ecc)时,我收到以下错误:数据库“path \ App_Data \ AspnetDB.mdf”无法打开,因为它是版本852.此服务器支持版本782和更早版本。 downrade路径不受支持。 无法打开新数据库“path \ App_Data \ AspnetDB.mdf”。 CREATE DATABASE被中止。 尝试为file'path \ App_Data \ AspnetDB.mdf附加自动命名的数据库失败。具有相同名称的数据库不存在,或者指定的文件无法打开,或它位于UNC共享“任何想法

+0

不要连接到mdf文件你应该总是使用服务器中的数据库名称和连接字符串中的服务器IP(名称)。 www.connectionstrings.com。 – jdweng

+0

你能告诉我我应该修改的位置吗?SqlConnection实例中的AttachDbFilename?谢谢! – flaviumanica

+0

如果还有其他问题,请发一篇文章。 –

回答

0

从OP的编辑带到了一个问题:?

设法解决它,争论在SqlConnection中应该与在web.config中的ConnectionString相同

相关问题