Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;无法打开物理文件";打开数据库时_C#_Asp.net_Sql Server - Fatal编程技术网

C# &引用;无法打开物理文件";打开数据库时

C# &引用;无法打开物理文件";打开数据库时,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我在用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.)".

我在用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->Options->Tools->Data Connections中编写了此实例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中的参数应该与web.config中的ConnectionString相同。现在还有另一个问题:当我尝试打开Visual Studio提供的默认数据库(用户、角色、ecc)时,我收到以下错误:数据库“path\App\u Data\AspnetDB.mdf”无法打开,因为它是852版。此服务器支持782及更早版本。不支持下行路径。 无法打开新数据库“path\App\u Data\AspnetDB.mdf”。创建数据库已中止。
尝试为文件“path\App\u Data\AspnetDB.mdf”附加自动命名数据库失败。存在同名数据库,或无法打开指定的文件,或该文件位于UNC共享上。“有什么想法吗?

摘自OP的编辑:


为了解决这个问题,SqlConnection中的参数应该与web.config中的ConnectionString相同。

取自OP对问题的编辑:


要解决此问题,SqlConnection中的参数应该与web.config中的ConnectionString相同。

不要连接到mdf文件。您应该始终在服务器中使用数据库名称和服务器IP(名称)在连接字符串中。请参见www.connectionstrings.com。您能告诉我应该在哪里修改吗?SqlConnection实例中的AttachDbFilename?谢谢!如果您有其他问题,请发表另一篇文章。不要连接到mdf文件。您应该始终在服务器中使用数据库名称和服务器IP(名称)在连接字符串中。请访问www.connectionstrings.com。您能告诉我应该在哪里修改吗?SqlConnection实例中的AttachDbFilename?谢谢!如果您还有其他问题,请发表另一篇文章。
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Cars.mdf;Integrated Security=True");
con.Open();