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

标签 c# .net sql-server

我有一段用于恢复 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 列出逻辑文件名。

问题是我直接从 Databasename 属性中提取它。 任何帮助将不胜感激。

最佳答案

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/

相关文章:

c# - Visual Studio 改变了 Ctrl-K-D 的工作方式

c# - 服务器响应为 : 5. 7.1 无法中继/邮件守护程序

sql-server - 在 SQL Server 中,如何从 MS Access 数据库读取数据以更新一个或多个表列中的数据?

c# - UWP vs WPF : System. 全局化 CultureInfo 不同?

c# - If/Else OR "? :"(条件)不工作

c# - C#Dictionary.ContainsKey()始终返回false

c# - wcf 服务器向一个客户端发送数据

sql - 从多个表中进行多次选择还是从所有表中对所有数据进行 1 次选择更好?

sql - 在 SQL Server 和 Postgresql 中将 CASE 用于类似枢轴的函数

c# - 如何限制特定ip的wcf服务方法