gpt4 book ai didi

c# - 如何还原多个对其 MDF 和 LDF 文件使用相同命名方案的 SQL Server 数据库

转载 作者:行者123 更新时间:2023-11-30 21:30:42 25 4
gpt4 key购买 nike

我有一段恢复 SQL Server 数据库的 C# 代码。这很好用。问题是在另一个 .bak 文件上再次运行它。尽管第二个备份的名称不同,但它需要写入与第一个备份相同的目录,并且 .mdf.ldf 的命名方案也相同文件。

我很好奇是否有办法修改 .mdf.ldf 文件的命名方案,或者是否有其他方法可以创建这些文件要还原到的初始 SQL Server 目录下的子目录。

我目前收到的错误消息:

Additional information: The file XXXXXX.MDF cannot be overwritten. It is being used by database XAXAXAXAX

我想我可以使用一个移动语句,但我试图避免将所有目录值硬编码或记录在某处的配置中。

string sql = "SELECT database_id FROM sys.databases WHERE Name = '"+yuy+"'";

SqlConnection con = new SqlConnection(@"" + singleconn.Replace(@"\\", @"\"));
SqlCommand command = new SqlCommand(sql, con);
con.Open();

object resultObj = command.ExecuteScalar();
con.Close();

if (resultObj == null)
{
string sql2 = "Restore Database " + yuy + " FROM DISK = '" + @"\" + localdir.Replace(@"\\", @"\") + @"\" + FileName + "'";

SqlCommand command2 = new SqlCommand(sql2, con);
con.Open();
command2.ExecuteNonQuery();
con.Close();
con.Dispose();

File.Delete(@"\" + localdir.Replace(@"\\", @"\") + @"\" + FileName);
MessageBox.Show("Database recovered successfully!");
}
else
{
Random rnd = new Random();
int card = rnd.Next(52);
MessageBox.Show("There is already a database under this name; renaming the DB to " + yuy + card.ToString());

string sql2 = "Restore Database " + yuy + card.ToString() + " FROM DISK = '" + @"\" + localdir.Replace(@"\\", @"\") + @"\" + FileName + "'";

SqlCommand command2 = new SqlCommand(sql2, con);
con.Open();
command2.ExecuteNonQuery();
con.Close();
con.Dispose();

File.Delete(@"\" + localdir.Replace(@"\\", @"\") + @"\" + FileName);
MessageBox.Show("Database Recovered Successfully!");
}

多亏了 scsimon 解决了大部分问题,现在我得到的关于错误的最后一件事是这个。

附加信息:逻辑文件“XXXXXX.mdf”不是数据库“Databasename”的一部分。使用 RESTORE FILELISTONLY 列出逻辑文件名。

问题是我直接从数据库名称属性中提取它。任何帮助将不胜感激。

最佳答案

I'm just curious if there is a way to modify the naming scheme of the .mdf and .ldf files, or if there is some other method to create subdirs under the initial SQL Server directory for these files to be restored to.

您可以使用 MOVE子句,如您在 RESTORE 命令中所建议的那样重命名 和/或移动 您的数据文件。它看起来像这样:

RESTORE DATABASE myDatabase FROM DISK = '\\somedir\someSubDir\mybackup.bak'
WITH
MOVE 'datafile1' TO 'E:\somedir\new_datafile2.mdf',
MOVE 'logfile' TO 'E\somedir\new_log.ldf'

这是为了将文件从备份中的默认位置移动到另一个目录(您可以这样做),但也可以重命名它们。当然,您也会对所有 .ndf 执行此操作。

I am trying to keep from needing all of the directory values hardcoded or logged in a config somewhere.

不用担心,只要restore带有 HEADERONLY 的数据库查看备份的内容,如名称、日期和其他有用信息。具体对于文件路径,您将使用 FILELISTONLY .这将阻止您对它们进行硬编码。更多关于 here .

CREATE TABLE #DataFiles (LogicalName nvarchar(128)
,PhysicalName nvarchar(260)
,[Type] char(1)
,FileGroupName nvarchar(128) null
,Size numeric(20,0)
,MaxSize numeric(20,0)
,FileID bigint
,CreateLSN numeric(25,0)
,DropLSN numeric(25,0)
,UniqueID uniqueidentifier
,ReadOnlyLSN numeric(25,0) null
,ReadWriteLSN numeric(25,0) null
,BackupSizeInBytes bigint
,SourceBlockSize int
,FileGroupID int
,LogGroupGUID uniqueidentifier null
,DifferentialBaseLSN numeric(25,0) null
,DifferentialBaseGUID uniqueidentifier null
,IsReadOnly bit
,IsPresent bit
,TDEThumbprint varbinary(32) null
,SnapshotURL nvarchar(360) null
)
INSERT INTO #DataFiles
EXEC('RESTORE FILELISTONLY FROM DISK = ''E:\DB Backups\YourBackup.bak''')

SELECT LogicalName, PhysicalName FROM #DataFiles

DROP TABLE #DataFiles

关于c# - 如何还原多个对其 MDF 和 LDF 文件使用相同命名方案的 SQL Server 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54030313/

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