gpt4 book ai didi

sql-server - 尝试将 mdf 文件附加到 localDb 会引发错误,至少需要一个文件

转载 作者:行者123 更新时间:2023-12-02 05:04:29 24 4
gpt4 key购买 nike

Here is error

TITLE: Microsoft SQL Server Management Studio

Attach database failed for Server '(localdb)\mssqllocaldb'. (Microsoft.SqlServer.Smo)

ADDITIONAL INFORMATION:

At least one file is needed for Database Attach. (Microsoft.SqlServer.Smo)

我正在尝试将此 .mdf 数据库文件附加到我的 LocalDb 实例。如果我也能将其连接到 SQL Server 就好了。我在同一目录中有 .ldf 文件

最佳答案

为了完成 - Jim's comment解决(一半)问题并让你继续前进。

问题的另一“一半”是 - 如果您最终想要重命名物理数据库文件怎么办?答案就在这个CodeProject post .

<小时/>

步骤:

  1. ALTER DATABASE设置新的物理文件名(数据文件和日志文件)
    只有重新启动 SQL Server 或数据库脱机并恢复联机后才会生效

    • 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');
  2. ALTER DATABASE再次设置新的逻辑文件名(同样是数据和日志文件)
    立即生效

    • ALTER DATABASE [CurrentName] MODIFY FILE (NAME = 'CurrentName', NEWNAME = 'NewDbName');
    • ALTER DATABASE [CurrentName] MODIFY FILE (NAME = 'CurrentName_log', NEWNAME = 'NewDbName_log');
  3. 脱机并恢复联机或重新启动 SQL Server

    • 使用 SQL Server Management Studio:
      1. 右键单击重命名的数据库,然后单击 Take OfflineTasks .
      2. 右键单击(离线)数据库,然后单击 Bring OnlineTasks .
    • 使用 T-SQL:
      1. ALTER DATABASE [CurrentName] SET OFFLINE WITH ROLLBACK IMMEDIATE; (将其设置为离线并断开所有客户端的连接)
      2. ALTER DATABASE [CurrentName] SET ONLINE;
<小时/>

完整代码:

-- Find "CurrentName" (without quotes) and replace with the current database name
-- Find "NewDbName" (without quotes) and replace with the new database name


USE [CurrentName];

-- Change physical file names:
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');

-- Change logical names:
ALTER DATABASE [CurrentName] MODIFY FILE (NAME = 'CurrentName', NEWNAME = 'NewDbName');
ALTER DATABASE [CurrentName] MODIFY FILE (NAME = 'CurrentName_log', NEWNAME = 'NewDbName_log');

-- Take offline and back online
USE [master]
GO
ALTER DATABASE [CurrentName] SET OFFLINE WITH ROLLBACK IMMEDIATE;
-- Then navigate to <Full-Path-Required> and rename the files
ALTER DATABASE [CurrentName] SET ONLINE;

关于sql-server - 尝试将 mdf 文件附加到 localDb 会引发错误,至少需要一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40020271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com