0

I have as ASP.net project with localDB in it. The database file name is ProjectDB.sdf and I placed him in the App_Data folder.

My connection string is:

<add name="ProjectConnection" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ProjectDB.sdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

I try to use the database in my cs file like this:

conn.ConnectionString = ConfigurationManager.ConnectionStrings["ProjectConnection"].ConnectionString;
using(SqlCommand cmd = new SqlCommand())
{
    cmd.CommandText = "select JobTitleId, JobTitleText from LuJobTitle where JobTitleText like @SearchText + '%'";
    cmd.Parameters.AddWithValue("@SearchText", prefix);
    cmd.Connection = conn;
    conn.Open();

The application falls in the conn.Open(); command.

The error message I get says:

An attempt to attach an auto-named database for file d:\user\documents\visual studio 2012\Projects\RealMatchSite\RealMatchSite\App_Data\ProjectDB.sdf failed.
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

What am I doing wrong?

Thank you in advance!

itzick binder
  • 524
  • 10
  • 31

2 Answers2

0

Your db server already has database attached with that name so you are getting that error. If this is not the case then try adding this to your connections string:

User Instance=True
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • When I added "User Instance=True" I got this: "The user instance login flag is not allowed when connecting to a user instance of SQL Server. The connection will be closed.". My db server is the local host. How do I remove all the databases from it and keep the only one I need? – itzick binder Jan 06 '17 at 16:03
  • Do you have sql mgmt studio? Login and remove it. – CodingYoshi Jan 06 '17 at 16:05
0

Try using Initial Catalog to call your database. I hope this helps.

connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=ProjectDB;Integrated Security=True"

jmag
  • 796
  • 1
  • 8
  • 17
  • I tried it and got this error message: "Cannot open database "ProjectDB" requested by the login. The login failed. Login failed for user 'User-PC\User'." – itzick binder Jan 06 '17 at 16:35
  • Is your server password protected?? If so, include ConnString = "Data Source=(LocalDB)\v11.0; Initial Catalog = ProjectDB; Persist Security Info = True; User ID = user; Password =password"; – jmag Jan 06 '17 at 16:39
  • It is currently using your machine credentials to access your database. The good news is it found the database and trying to access it. – jmag Jan 06 '17 at 16:42