DB Connection string in Web.config to use attached .mdf database won’t work

The file neodb.mdf is in my App_Data folder and I can browse the database in the server explorer in visual studio, using built in SQLEXPRESS:

Currently trying to no avail:

 <connectionStrings>
    <add name="EFDbContext" connectionString=".SQLExpress;AttachDbFilename=|DataDirectory|neodb.mdf; Database=neodb;Trusted_Connection=Yes;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

and

 <connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=.SQLEXPRESS;Database=neodb.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Also as I understand the *.mdf is an SQL server database file type and .dbo is owner of file when it’s included in the initial catalog ? What’s the initial catalog anywhere ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you have the *.mdf placed in App_Data folder, using this format works:

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

Method 2

There is a simple way to retrieve the connection string to each database.
Double click on “DataBase.mdf” file in Solution Explorer > right click on the “DataBase.mdf” file in Server Explorer > click “Properties” > Now you can see the connection string (under the “Connection” header) in the properties menu!

Method 3

If you wanted the database to be created in your App_Data folder. You can use the following ConnectionString:

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

Alternatively you can use:

<add name="ConnectionName" connectionString="Data Source=(LocalDb)v11.0;Initial Catalog=MyDatabase;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

Also you will see MyDatabase.mdf and .ldf database files are created in the C:UsersYourUserName Folder.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x