简体   繁体   中英

Connect LocalDB to ASP.net Project

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!

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM