简体   繁体   English

如何通过C#以编程方式在不使用.mdf或.ldf文件的情况下将.bak文件还原到新服务器?

[英]How can I restore a .bak file to a new server without using the .mdf or .ldf files, programmatically with C#?

Some info to get started: 一些入门信息:

  1. I am using SQL Server Express 2014 我正在使用SQL Server Express 2014
  2. I am using C# 我正在使用C#
  3. I can't use SMO functions to accomplish the following task 我无法使用SMO功能来完成以下任务

I am trying to restore a .bak file that I got from computer A, onto computer B. I want to do this programmatically using C#, but not using SMO functions. 我正在尝试将从计算机A获得的.bak文件还原到计算机B。我想以编程方式使用C#,但不使用SMO功能。

In SSMS (SQL Server Management Studio), I can manually restore a database with nothing more than a .bak file. 在SSMS(SQL Server Management Studio)中,我可以仅使用.bak文件来手动还原数据库。 I would like to be able to do the same running a program with C#. 我希望能够使用C#运行程序进行相同的操作。

I am able to restore a .bak file as long as I have the .mdf and .ldf files, but not without them. 只要拥有.mdf和.ldf文件,我就可以还原.bak文件,但如果没有它们,则无法恢复。 Manually doing this in SSMS seems to work so I assume that there must be a way to do this with C# as well. 在SSMS中手动执行此操作似乎有效,因此我认为也必须使用C#进行此操作。 Here is what I have: 这是我所拥有的:

DBConnection connect1 = new DBConnection();
connect1.connectionString = "Data Source=" + textBox1.Text + "; Integrated Security=SSPI"; // textbox1 contains a user entered server name.

// Create a new database.
using (SqlConnection myConnection = new SqlConnection(connect1.connectionString))// set the connection to sql.
{
    SqlCommand createDB = new SqlCommand("Create Database [" + dbName + "]", myConnection);
    createDB.Connection.Open();
    createDB.ExecuteNonQuery();
}

// Restore the data from the backup into the new database.
connect1 = new DBConnection();
connect1.connectionString = "Data Source=" + textBox1.Text + "; Initial Catalog=Master; Integrated Security=SSPI";
using (SqlConnection myConnection = new SqlConnection(connect1.connectionString))// set the connection to sql.
{
    //SqlCommand createDB = new SqlCommand("Restore Database [" + dbName + "] FROM DISK = " + bakDirctory + " "
    //    + "WITH MOVE '" + oldDBName + "' TO '" + mdfDirectory + "', "
    //    + "MOVE '" + oldDBName + "_log' TO '" + ldfDirectory + "', "
    //    + "REPLACE", myConnection);
    // oldDBName contains what the name of the database was when the .bak file was created.

    SqlCommand createDB = new SqlCommand("Restore Database [" + dbName + "] FROM DISK = " + bakDirectory, myConnection);
    createDB.Connection.Open();
    createDB.ExecuteNonQuery();
}

The commented part of the code snippet works, but requires the use of the .mdf and .ldf files. 该代码段的注释部分有效,但是需要使用.mdf和.ldf文件。 The part just below only uses the .bak file but I get an unhandled exception when attempting to run that code: 下方的部分仅使用.bak文件,但是在尝试运行该代码时出现未处理的异常:

The backup set holds a backup of a database other than the existing 'dbname' database 备份集保存除现有“ dbname”数据库以外的数据库的备份

I believe this is because the database structure of the newly created database doesn't match the structure of the database in the .bak file. 我相信这是因为新创建的数据库的数据库结构与.bak文件中的数据库结构不匹配。 (please correct me if I have that wrong). (如果我有错,请纠正我)。 Is there a way for me to restore the backup using only the .bak file? 有没有办法让我仅使用.bak文件来还原备份? Thank you in advance for taking the time to look at this. 预先感谢您抽出宝贵的时间对此进行研究。

As @DanGuzman stated, there was no need to create a new database beforehand. 如@DanGuzman所述,无需事先创建新数据库。

Following (@ScottChamberlain)'s instructions in the comments, I was able to find the script needed to restore the database without using the .mdf or .ldf files. 遵循注释中(@ScottChamberlain)的说明,我能够找到还原数据库所需的脚本,而无需使用.mdf或.ldf文件。 The exact script was: 确切的脚本是:

SqlCommand createDB = new SqlCommand("RESTORE DATABASE [" + dbName + "] FROM  DISK = N'" + bakDirectory + "' " +
"WITH  FILE = 2,  MOVE N'" + oldDBName + "' TO N'C:\\Program Files\\Microsoft SQL Server\\MSSQL12.ANTSQLSERVER\\MSSQL\\DATA\\" + dbName + ".mdf',  " +
"MOVE N'" + oldDBName + "_log' TO N'C:\\Program Files\\Microsoft SQL Server\\MSSQL12.ANTSQLSERVER\\MSSQL\\DATA\\" + dbName + "_log.ldf',  NOUNLOAD,  STATS = 5", myConnection);

The (C:\\Program Files\\Microsoft SQL Server\\MSSQL12.ANTSQLSERVER\\MSSQL\\DATA\\) path was the default location where .mdf and .ldf files are created. (C:\\ Program Files \\ Microsoft SQL Server \\ MSSQL12.ANTSQLSERVER \\ MSSQL \\ DATA \\)路径是创建.mdf和.ldf文件的默认位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用c#从文件系统和SQL Server中删除动态生成的数据库文件(.MDF和.LDF) - How to delete a dynamically generated database file (.MDF and .LDF) from the filesystem and from SQL server using c# 如何从.bak文件还原C#Windows应用程序中的.mdf数据库 - how to restore .mdf database in C# windows application from .bak file 如何在C#中还原SQL Server 2012数据库.bak文件? - How to restore a SQL Server 2012 database .bak file in C#? 如何从C#以编程方式创建SQL Server数据库文件(.mdf)? - How can I create a SQL Server database file (.mdf) programmatically from C#? 如何使用C#使用新的数据库名称还原Sqlserver .bak文件 - How to Restore Sqlserver .bak file with New database name using c# 如何还原对其MDF和LDF文件使用相同命名方案的多个SQL Server数据库 - How to restore multiple SQL Server databases that use the same naming scheme for their MDF and LDF files 如何以编程方式将.MDF文件附加到Azure SQL Server C# - How to attach .MDF file to Azure SQL Server programmatically c# 我可以在没有 sql 服务器的情况下运行带有 mdf 文件的 C# windows 程序吗? - Can i run C# windows program with mdf file without sql server? 如何使用PowerShell和C#检索每个.bak文件? - How can I retrieve every .bak file with PowerShell and C#? C#:将SQL Server数据库备份到新的.bak文件 - C#: Backup SQL Server Database to a NEW .bak file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM