4

I have a database file (FinanceDB.mdf) stored on the local harddrive. I can connect to the file, but when I execute queries to it through C#, I get the following error:

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

I have set the permissions for everyone on the file to full access which I thought may be part of the problem, but it turns out that its not (unless there is more to it than just setting the permissions).

This is the connection string:

<add name="FinanceDBConnectionString1" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\IT7x01Project\App_Data\FinanceDB.mdf;Integrated Security=True;" providerName="System.Data.SqlClient"/>

Any help with this would be appreciated.

John
  • 4,658
  • 2
  • 14
  • 23

2 Answers2

1

try to create connection string in this format

<add name="Connec" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=C:\USERS\dL\DESKTOP\DATABASE\MYSHOP.MDF;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient"/>

(or)

Try setting the User Instance property in your connection string to true.

User Instance=True

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

MMM
  • 3,132
  • 3
  • 20
  • 32
-1

<add name="Connections" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

add in we.config file

Radha
  • 51
  • 1
  • 1