如何找到我的(localdb)SQL Server 2012数据库的位置并进行备份?

时间:2021-03-09 09:02:42

I am using VS2012 and I have a database created:

我正在使用VS2012,我创建了一个数据库:

(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" />

7 个解决方案

#1


8  

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'

#2


17  

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.

您是说您可以在SQL Server Management Studio中看到它?右键单击DataBase - > Properties - > Files将告诉您硬盘上的位置。如果备份mdf,请务必备份ldf。

如何找到我的(localdb)SQL Server 2012数据库的位置并进行备份?

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.

或者,您可以右键单击数据库,然后选择“任务” - >“备份”。这将为您创建一个.bak文件,您不必担心mdf / ldf。

#3


16  

By default, LocalDB database creates “*.mdf” files in the C:/Users/"username" directory.

默认情况下,LocalDB数据库在C:/ Users /“username”目录中创建“* .mdf”文件。

Link ref: https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html

链接参考:https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html

#4


10  

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

对于第一次接触Entity Framework的人来说,这是相当混乱的。

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

如果您使用Code First,则会生成一个mdf文件,它位于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

如果使用Database First,则在SQL Server对象资源管理器(非服务器资源管理器!!)下创建数据库,将在C:\ Users \“username”\ AppData \ Local \ Microsoft \ Microsoft SQL Server Local下生成mdf文件DB \实例\ MSSQLLocalDB

#5


8  

http://technet.microsoft.com/en-us/library/hh510202.aspx

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.

数据库的系统数据库文件存储在用户的本地AppData路径中,该路径通常是隐藏的。例如C:\ Users \ - user - \ AppData \ Local \ Microsoft \ Microsoft SQL Server Local DB \ Instances \ LocalDBApp1。用户数据库文件存储在用户指定的位置,通常位于C:\ Users \\ Documents \文件夹中的某个位置。

#6


2  

This PowerShell script, will give you the default location for localdb .mdf files:

此PowerShell脚本将为您提供localdb .mdf文件的默认位置:

[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

其中$ instancename是要检查的localdb实例的名称。您可以通过运行获取localdb实例列表

sqllocaldb i

#7


0  

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.

打开Windows注册表编辑器并导航到密钥HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Microsoft SQL Server本地DB \ Installed Versions。查看被定位的SQL Server实例的版本密钥,例如11.0 for SQL 2012,并查看localdb的文件系统位置的InstanceAPIPath值。

Note that at full list of SQL server versions mapped to release name and year can be found here

请注意,可以在此处找到映射到版本名称和年份的SQL服务器版本的完整列表

#1


8  

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'

#2


17  

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.

您是说您可以在SQL Server Management Studio中看到它?右键单击DataBase - > Properties - > Files将告诉您硬盘上的位置。如果备份mdf,请务必备份ldf。

如何找到我的(localdb)SQL Server 2012数据库的位置并进行备份?

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.

或者,您可以右键单击数据库,然后选择“任务” - >“备份”。这将为您创建一个.bak文件,您不必担心mdf / ldf。

#3


16  

By default, LocalDB database creates “*.mdf” files in the C:/Users/"username" directory.

默认情况下,LocalDB数据库在C:/ Users /“username”目录中创建“* .mdf”文件。

Link ref: https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html

链接参考:https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html

#4


10  

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

对于第一次接触Entity Framework的人来说,这是相当混乱的。

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

如果您使用Code First,则会生成一个mdf文件,它位于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

如果使用Database First,则在SQL Server对象资源管理器(非服务器资源管理器!!)下创建数据库,将在C:\ Users \“username”\ AppData \ Local \ Microsoft \ Microsoft SQL Server Local下生成mdf文件DB \实例\ MSSQLLocalDB

#5


8  

http://technet.microsoft.com/en-us/library/hh510202.aspx

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.

数据库的系统数据库文件存储在用户的本地AppData路径中,该路径通常是隐藏的。例如C:\ Users \ - user - \ AppData \ Local \ Microsoft \ Microsoft SQL Server Local DB \ Instances \ LocalDBApp1。用户数据库文件存储在用户指定的位置,通常位于C:\ Users \\ Documents \文件夹中的某个位置。

#6


2  

This PowerShell script, will give you the default location for localdb .mdf files:

此PowerShell脚本将为您提供localdb .mdf文件的默认位置:

[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

其中$ instancename是要检查的localdb实例的名称。您可以通过运行获取localdb实例列表

sqllocaldb i

#7


0  

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.

打开Windows注册表编辑器并导航到密钥HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Microsoft SQL Server本地DB \ Installed Versions。查看被定位的SQL Server实例的版本密钥,例如11.0 for SQL 2012,并查看localdb的文件系统位置的InstanceAPIPath值。

Note that at full list of SQL server versions mapped to release name and year can be found here

请注意,可以在此处找到映射到版本名称和年份的SQL服务器版本的完整列表