David Cruwys
David Cruwys

Reputation: 6872

Cannot find Database for SQL Server Express 2014

Two Questions

  1. How do I connect to "SQL Server Express" Database from SQL Management Studio?
  2. How do I get my MVC Entity Framework application to connect to my original "SQL Server Express LocalDB" database.

My Scenario

I have setup my MVC application to use IIS and when I first ran the application it asked me to switch from SQL Server Express LocalDb to SQL Server Express.

SQL Server Express LocalDb to SQL Server Express

The result, I now have no data in the Database as I am now connecting to a new Database Server.

Before / After

Question 1: How do I connect to SQL Server Express Database from SQL Management Studio?

How to Connect to SQL Server Express?

Question 2: How do I get my MVC Entity Framework application to connect to my original SQL Server Express LocalDB database?

I can connect to the old "SQL Server Express LocalDB" database from a unit test, but when I use the exact same connection string in MVC, I get the new server "SQL Server Express"

Connection String in UnitTest

Unit Test is connecting to LocalDB

<connectionStrings>
    <add name="RqDbContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Rq;Integrated Security=True;Pooling=false" providerName="System.Data.SqlClient" />
</connectionStrings>

Connection String in Web.config

Web Site is connecting to another database that I can't find

<connectionStrings>
    <add name="RqDbContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Rq;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

The image below shows that the Unit Test is writing data from the old LocalDB but the Website which is connected to a Database Server, but it's database is empty.

They are using the same connection strings.

I have used debugger to check the entity framework models and it's clearly a different Database but with the same structure being used.

I'm not sure what is going on

Website vs Nuit

Upvotes: 4

Views: 1157

Answers (1)

Amit Mittal
Amit Mittal

Reputation: 1127

Four thing you need to do:

1) Check if SQL Server Express is already installed in your PC or not. Follow these steps:

  • Go to Run, start services.msc
  • Look for "SQL Server (....)" entries with running state. The text inside the bracket is the instance name, if you dont find any such entry then you need to install SQL Server.

2) To connect to Management Studio:

  • Start Management Studio.
  • In Server Name, type in: .\(instancename)
  • Instance name is what you see in brackets in step 1.

3) For moving your database from LocalDb to Sql Server:

  • Locate your LocalDb database file. Typically LocalDb databases are created inside the "C:\Users(Your Username)" folder.
  • Copy the MDF and LDF files from above step to some other location, this step is optional.
  • Using Management studio, Attach the MDF file from above step to your SQL Server Instance.

4) To use SQL Server Instance in your project:

  • Change connection to point to your SQL Server Instance instead of LocalDb.
  • For Eg:

     <add name="RqDbContext" connectionString="Data Source=.\sqlexpress;Initial Catalog=Rq;Integrated Security=True;" providerName="System.Data.SqlClient" />

Hope this helps. Cheers!!

Upvotes: 1

Related Questions