Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An Attempt to attach an auto-named database Error

"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."

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

like image 554
user962206 Avatar asked Mar 01 '12 01:03

user962206


3 Answers

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

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.

like image 179
Petar Petkov Avatar answered Nov 13 '22 23:11

Petar Petkov


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.

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"
like image 41
Don Dilanga Avatar answered Nov 13 '22 23:11

Don Dilanga


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" />
like image 37
Muhammad Waqas Aziz Avatar answered Nov 13 '22 22:11

Muhammad Waqas Aziz