0

I am getting the following Error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Details: An attempt to attach an auto-named database for file Path\pharmacyDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Here is my connection string in app.config file:

<connectionStrings>
    <add name="PharmacyManagementSystem.Properties.Settings.pharmacyDBConnectionString"
        connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\pharmacyDB.mdf;Integrated Security=True;Connect Timeout=30"
        providerName="System.Data.SqlClient" />
</connectionStrings>

I already did search for similar issues and found solution but non of them work for me until now, here is what I did:

following this article: A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. in msdn forum and this one: An attempt to attach an auto-named database for file …database1.mdf failed also this one: An Attempt to attach an auto-named database Error in the Stack.

then I tried out to change my connection string couple times as follow:

connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Database = pharmacyDB.mdf;Integrated Security=True;Connect Timeout=30"

but it give me this error this time:

Cannot open database \"pharmacyDB.mdf\" requested by the login. The login failed.\r\nLogin failed for user 'computername\username'.

hint: the folder access is full control for the current user in Security>>Permissions for username

then I modified my connection string for 2nd time as follow:

connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\pharmacyDB.mdf;Integrated Security=True;User Instance = True;Connect Timeout=30"

but it still gave me this error :

The user instance login flag is not allowed when connecting to a user instance of SQL Server. The connection will be closed.

also I can't give a fixed path to connection string because I can't be sure where my application data will be in the end user computers. End user can modify the default installation folder Program Files as per my requirements.

EDIT: tried the fixed path in connection string just to see if it works while debugging phase but it gave me the same first error. Also my Database file is copied always so I tried out to delete the original file after debugging so I have only one database with that name in my computer and run the App from the debug folder but it crash on the same point that it fire the exception from within VS.

Can you please help me or direct me into the right way to follow Please!

Community
  • 1
  • 1
JihadiOS
  • 381
  • 2
  • 13
  • It seems you just manually deleted the database. Try to delete in Server Management Studio. If deleted manually, the name of db is still registered in Server and this is why it's throwing such exception. I also recommend to use hardcorded path for the localDB. – Kay Lee Mar 18 '17 at 23:33
  • In Server Management Studio only 1 Database is registered and it is the debug folder Database. about the hard corded path it seems you didn't look to my **EDIT** fixed path = hard cored path – JihadiOS Mar 19 '17 at 07:30

0 Answers0