TITLE:Microsoft SQL Server Management Studio
为服务器'(localdb)\ mssqllocaldb'附加数据库失败。 (Microsoft.SqlServer.Smo)
其他信息:
数据库附加至少需要一个文件。 (Microsoft.SqlServer.Smo)
我正在尝试将此.mdf
数据库文件附加到我的LocalDb实例。如果我也可以使用SQL Server也没关系。我在同一目录中有.ldf
个文件
答案 0 :(得分:19)
为了完成起见 - Jim's comment解决了(一半)问题并让你前进。
问题的另一半“ - 如果你最终想要重命名物理数据库文件怎么办?答案可在此CodeProject post中找到。
步骤:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int orientation = getContext().getResources().getConfiguration().orientation;
//The below code will set width = height, to achieve square layout
if (orientation == Configuration.ORIENTATION_PORTRAIT || orientation == Configuration.ORIENTATION_UNDEFINED) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
setMeasuredDimension(width, height);
} else {
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
setMeasuredDimension(width, height);
}
设置新的物理文件名(数据文件和日志文件)
在SQL Server重新启动或数据库脱机并重新联机之后才会生效
ALTER DATABASE
ALTER DATABASE [CurrentName] MODIFY FILE (NAME = 'CurrentName', FILENAME = '<Full-Path-Required>\NewDbName.mdf');
ALTER DATABASE [CurrentName] MODIFY FILE (NAME = 'CurrentName_log', FILENAME = '<Full-Path-Required>\NewDbName_log.ldf');
再次设置新的逻辑文件名(同样,数据和日志文件)
立即生效
ALTER DATABASE
ALTER DATABASE [CurrentName] MODIFY FILE (NAME = 'CurrentName', NEWNAME = 'NewDbName');
脱机并重新联机或重新启动SQL Server
ALTER DATABASE [CurrentName] MODIFY FILE (NAME = 'CurrentName_log', NEWNAME = 'NewDbName_log');
下的Take Offline
。Tasks
下的Bring Online
。Tasks
(将其设置为脱机并断开任何客户端连接)ALTER DATABASE [CurrentName] SET OFFLINE WITH ROLLBACK IMMEDIATE;
完整代码:
ALTER DATABASE [CurrentName] SET ONLINE;
答案 1 :(得分:3)
如果您不记得以前的文件名,请在十六进制编辑器中打开.mdf文件,在偏移量0x19D附近,您将看到该文件名的UTF-16(2字节/字符)字符串
答案 2 :(得分:1)
我不得不多次移动/重命名数据库。如果您在同一条船上,这里的脚本使用变量来避免反复输入新/旧名称。
它使用Jesse's answer中的相同逻辑,而不是为您自动启动数据库备份。我假设您需要在移动/重命名物理文件后将其重新打开,因此删除了该语句。如果这个假设不正确,请评论。
但是,要反映SSMS中的逻辑重命名,您仍需要right click -> rename
。如果不使用下面的EXECUTE
/ REPLACE
方法,这似乎是相同的。
---------- CHANGE THESE ----------
-- Keep names identical to only move locations
DECLARE @CurrDbName AS varchar(255) = 'CurrentDbName'
DECLARE @NewDbName AS varchar(255) = 'NewDbName'
DECLARE @PathToFolder AS varchar(255) = '<FullPathMinusFilename>\'
---------- DECLARE TEMPLATES ----------
-- Use DB
DECLARE @USE_DB AS varchar(255) = 'USE [{CurrDbName}]'
-- Change physical file names
DECLARE @SET_PHYS_MDF AS varchar(255) = 'ALTER DATABASE [{CurrDbName}] MODIFY FILE (NAME = ''{CurrDbName}'', FILENAME = ''{PathToFolder}{NewDbName}.mdf'')'
DECLARE @SET_PHYS_LDF AS varchar(255) = 'ALTER DATABASE [{CurrDbName}] MODIFY FILE (NAME = ''{CurrDbName}_log'', FILENAME = ''{PathToFolder}{NewDbName}_log.ldf'')'
-- Change logical names (LOG = "logical", not "log")
If (@CurrDbName != @NewDbName)
BEGIN
DECLARE @SET_LOG_MDF AS varchar(255) = 'ALTER DATABASE [{CurrDbName}] MODIFY FILE (NAME = ''{CurrDbName}'', NEWNAME = ''{NewDbName}'')'
DECLARE @SET_LOG_LDF AS varchar(255) = 'ALTER DATABASE [{CurrDbName}] MODIFY FILE (NAME = ''{CurrDbName}_log'', NEWNAME = ''{NewDbName}_log'')'
END
-- Take offline
DECLARE @SET_OFFLINE AS varchar(255) = 'ALTER DATABASE [{CurrDbName}] SET OFFLINE WITH ROLLBACK IMMEDIATE'
---------- START DOING STUFF ----------
DECLARE @SQL_SCRIPT AS varchar(255)
-- Use DB
SET @SQL_SCRIPT = REPLACE(@USE_DB, '{CurrDbName}', @CurrDbName)
EXECUTE (@SQL_SCRIPT)
-- Change physical file names
SET @SQL_SCRIPT = REPLACE(REPLACE(REPLACE(@SET_PHYS_MDF, '{CurrDbName}', @CurrDbName), '{NewDbName}', @NewDbName), '{PathToFolder}', @PathToFolder)
EXECUTE (@SQL_SCRIPT)
SET @SQL_SCRIPT = REPLACE(REPLACE(REPLACE(@SET_PHYS_LDF, '{CurrDbName}', @CurrDbName), '{NewDbName}', @NewDbName), '{PathToFolder}', @PathToFolder)
EXECUTE (@SQL_SCRIPT)
-- Change logical names (LOG = "logical", not "log")
If (@CurrDbName != @NewDbName)
BEGIN
SET @SQL_SCRIPT = REPLACE(REPLACE(@SET_LOG_MDF, '{CurrDbName}', @CurrDbName), '{NewDbName}', @NewDbName)
EXECUTE (@SQL_SCRIPT)
SET @SQL_SCRIPT = REPLACE(REPLACE(@SET_LOG_LDF, '{CurrDbName}', @CurrDbName), '{NewDbName}', @NewDbName)
EXECUTE (@SQL_SCRIPT)
END
-- Take offline
USE [master]
SET @SQL_SCRIPT = REPLACE(@SET_OFFLINE, '{CurrDbName}', @CurrDbName)
EXECUTE (@SQL_SCRIPT)
-- Now turn off the database, rename/move physical files, and bring the database back online
这是我的第一个回答,如果质量不够,请道歉。
答案 3 :(得分:1)
这些答案都没有快速得到答案,所以我想我只想添加我的答案来指出我的发现(基于每个人的贡献)......
情况:
您有一个数据库文件和一个日志文件,但不是它们的备份。您正在尝试连接数据库(更可能是为了从已关闭的服务器中恢复)。
问题:
您已将MDF和LDF文件的名称更改为与原始文件不同的名称。您需要将它们重命名为原始名称,然后尝试ATTACH。
如何重命名数据库文件(简单方法):
Restore As
文件名,您希望将MDF和LDF文件命名为。答案 4 :(得分:0)
对于重命名的文件,命令行显得更为宽容。请注意,这不是一劳永逸的脚本...单独运行每个部分,请注意需要更改的名称:
--#1 Attach the db
USE [master]
GO
CREATE DATABASE RenamedDB ON
( FILENAME = N'<PathToRenamedFile>\renamedDBFile.mdf' ),
( FILENAME = N'<PathToRenamedFile>\renamedDBFile_log.ldf' )
FOR ATTACH
GO
--#2 Get the old logical file names:
USE RenamedDB
select * from sys.database_files
--#3 Rename the old logical files
ALTER DATABASE RenamedDB MODIFY FILE (NAME=N'OldLogicalDBName', NEWNAME=N'renamedDBFile')
GO
ALTER DATABASE RenamedDB MODIFY FILE (NAME=N'OldLogicalLogName', NEWNAME=N'renamedDBFile_log')
GO
--#4 check for the new names
select * from sys.database_files