10

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 ?

3 Answers 3

15

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>
Sign up to request clarification or add additional context in comments.

4 Comments

what will come in data source if i put code in server? i get error on server.
@Hiral: what are you trying to accomplish and what is the error that you are getting? I do not understand what you are asking
Application is working properly in local machine.i put publish folder on Server, i get error in mdf file connection string. what i have to give on datasource on server?
@Hiral: what is the error text? It might have to do with the sql engine instance name, you might need to change this part of the connection string to the correct name: .\SQLEXPRESS
12

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!

Comments

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:\Users\YourUserName Folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.