How can I find out the location of my (localdb) SQL Server 2012 database and back it up?

前端 未结 7 1162
旧巷少年郎
旧巷少年郎 2021-02-01 15:09

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 o

相关标签:
7条回答
  • 2021-02-01 15:21

    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

    0 讨论(0)
  • 2021-02-01 15:30

    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.

    enter image description here

    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.

    0 讨论(0)
  • 2021-02-01 15:31

    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

    0 讨论(0)
  • 2021-02-01 15:32

    It is quite confusing for people who touch with Entity Framework the first time.

    If you use Code First, a mdf file is generated, it is under C:/Users/"username"

    If you use Database First, you create a database under SQL Server Object Explorer (Not Server Explorer !!), a mdf file will be generated under the C:\Users\"username"\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB

    0 讨论(0)
  • 2021-02-01 15:33

    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.

    0 讨论(0)
  • 2021-02-01 15:44

    Try this one -

    DECLARE 
          @SQL NVARCHAR(1000)
        , @DB_NAME NVARCHAR(100) = 'AdventureWorks2008R2'
    
    SELECT TOP 1 @SQL = '
        BACKUP DATABASE [' + @DB_NAME + '] 
        TO DISK = ''' + REPLACE(mf.physical_name, '.mdf', '.bak') + ''''
    FROM sys.master_files mf
    WHERE mf.[type] = 0
        AND mf.database_id = DB_ID(@DB_NAME)
    
    PRINT @SQL
    EXEC sys.sp_executesql @SQL
    

    Output -

    BACKUP DATABASE [AdventureWorks2008R2] 
    TO DISK = 'D:\DATABASE\SQL2012\AdventureWorks2008R2.bak'
    
    0 讨论(0)
提交回复
热议问题