C# 尝试附加自动命名的数据库错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9509441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:41:42  来源:igfitidea点击:

An Attempt to attach an auto-named database Error

c#.netdatabaselinq-to-sqlpath

提问by user962206

"An attempt to attach an auto-named database for file C:\Users\John\documents\visual studio 2010\Projects\PAS\PAS\bin\Debug//PatAddSys.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

“尝试为文件 C:\Users\John\documents\visual studio 2010\Projects\PAS\PAS\bin\Debug//PatAddSys.mdf 附加自动命名的数据库失败。存在同名的数据库,或无法打开指定的文件,或者它位于 UNC 共享上。”

What's wrong here? I have the correct code for my path ( I think) but still this error occurs here is my database path

这里有什么问题?我的路径有正确的代码(我认为)但这里仍然发生此错误是我的数据库路径

private string dbPath = Application.StartupPath + "//PatAddSys.mdf";

Here's the location of My Database

这是我的数据库的位置

enter image description here

在此处输入图片说明

采纳答案by ScorpiAS

Try setting the User Instance property in your connection string to true. You need to add this to your connection string:

尝试将连接字符串中的 User Instance 属性设置为 true。您需要将此添加到您的连接字符串:

User Instance=True

Also just to be sure check again your database server as it might already contain a database with the same name.

也只是为了确保再次检查您的数据库服务器,因为它可能已经包含一个同名的数据库。

Hope that helps.

希望有帮助。

回答by Luf

Use this:

用这个:

Path.GetFullPath(yourpath_string)

it will work

它会工作

回答by Muhammad Waqas Aziz

i think it might be very very late BUT

我想可能会很晚但是

this string give me the above error

这个字符串给了我上面的错误

<add name="MAB_ERP_2_0.Properties.Settings.MyConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=Database\MabErp2.mdf;Integrated Security=true;"
        providerName="System.Data.SqlClient" />

But if add |Data Directory| before database name then it work fine

但是如果添加|数据目录| 在数据库名称之前然后它工作正常

<add name="MAB_ERP_2_0.Properties.Settings.MyConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database\MabErp2.mdf;Integrated Security=true;"
        providerName="System.Data.SqlClient" />

回答by Leone

FOR FUTURE HELP !!

为了将来的帮助!

you've corrected it:

你已经更正了:

private string dbPath = Application.StartupPath + "//PatAddSys.mdf";

BUT just have to do one step is to use the Backslash " \ " instead of SLASH " / " so it should to be like this :

但是只需要做一个步骤就是使用反斜杠“\”而不是斜杠“/”,所以它应该是这样的:

private string dbPath = Application.StartupPath + "\PatAddSys.mdf";

&thanks this line saved me alot of work :)

&感谢这条线为我节省了很多工作:)

回答by Don Dilanga

Change both the current working directory and the connection string to the correct ones to solve it.

将当前工作目录和连接字符串都更改为正确的目录以解决它。

Select the database which is located in "Server explorer" then copy the connection string as exactly seeing in its properties, then use it in the codes.

选择位于“服务器资源管理器”中的数据库,然后复制在其属性中完全看到的连接字符串,然后在代码中使用它。

enter image description here

在此处输入图片说明

then for the current working directory use the same path without the database's name. Solution -> properties -> debug is where the current working directory path is located at. This works for visual studio 2015.

然后对于当前工作目录使用相同的路径而不使用数据库的名称。解决方案-> 属性-> 调试是当前工作目录路径所在的位置。这适用于 Visual Studio 2015。

I use my connection string as this

我使用我的连接字符串作为这个

Private constr As String = "Data Source = (LocalDB)\MSSQLLocalDB;AttachDbFilename=" +
        Directory.GetCurrentDirectory() + "\DBNAME.mdf;" +
        "Integrated Security=True;Connect Timeout=30;User Instance=False"

回答by AlirezaKamyab

Actually I've crashed to this problem but I handled it easily. if your connection string is

实际上我遇到了这个问题,但我很容易处理。如果您的连接字符串是

 connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"

you need to make your own localDB. first go to command prompt than write

您需要制作自己的localDB。首先转到命令提示符而不是写入

sqllocaldb create MyDatabase

than start your database

比启动你的数据库

sqllocaldb start MyDatabase

than return to VS and change your connection string to

而不是返回到 VS 并将您的连接字符串更改为

connectionString="Data Source=(LocalDB)\MyDatabase;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"