'How can I find out the location of my (localdb) SQL Server 2012 database and back it up?
I am using VS2012 and I have a database created:
(localdb)\v11.0 (SQL Server 11.0.2100 - T61\Alan)
How can I find out the physical location of this database. How can I back this up? Can I just make a copy of the files, move these to another location and start the database again.
Here is my connection string:
<add name="DB1Context" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=DB1;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
Solution 1:[1]
By default, LocalDB database creates “*.mdf” files in the C:/Users/"username" directory.
Link ref: https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html
Solution 2:[2]
It is quite confusing for people who touch with Entity Framework the first time.
If you use Code First, an mdf file is generated at %USERPROFILE% (e.g. C:\Users\<username>).
If you use Database First, you create a database under SQL Server Object Explorer (not Server Explorer!), an mdf file will be generated at %LOCALAPPDATA%\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB.
Solution 3:[3]
Are you saying you can see it listed in SQL Server Management Studio? Right click on DataBase -> Properties -> Files will tell you where on your hard disk it lives. If you backup the mdf, be sure to back up the ldf too.

Alternatively, you can right click on the DB, and choose Tasks -> Backup. This will make a a single .bak file for you, and you don't need to worry about the mdf/ldf.
Solution 4:[4]
http://technet.microsoft.com/en-us/library/hh510202.aspx
The system database files for the database are stored in the users' local AppData path which is normally hidden. For example C:\Users\--user--\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\LocalDBApp1. User database files are stored where the user designates, typically somewhere in the C:\Users\\Documents\ folder.
Solution 5:[5]
Open Windows registry editor and navigate to key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions. Look beneath the version key of the SQL Server instance being targeted e.g. 11.0 for SQL 2012, and see its InstanceAPIPath value for file system location of the localdb's.
Note that at full list of SQL server versions mapped to release name and year can be found here
Solution 6:[6]
This PowerShell script, will give you the default location for localdb .mdf files:
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
(New-Object Microsoft.SqlServer.Management.Smo.Server("(localdb)\$instancename")).DefaultFile
where $instancename is the name of the localdb instance you want to check for. You can get a list of localdb instances by running
sqllocaldb i
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dave Schweisguth |
| Solution 2 | Ray |
| Solution 3 | Andrey Morozov |
| Solution 4 | Jonathan |
| Solution 5 | John K |
| Solution 6 | Jon List |
