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!