Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式还原多个数据库_C#_Sql Server_Sql Server 2012_Database Backups_Database Restore - Fatal编程技术网

C# 以编程方式还原多个数据库

C# 以编程方式还原多个数据库,c#,sql-server,sql-server-2012,database-backups,database-restore,C#,Sql Server,Sql Server 2012,Database Backups,Database Restore,我正在做一个从Microsoft SQL Server恢复/备份数据库的应用程序 当目标数据库的文本框用作新数据库的新名称并将其恢复到sql server中时,如何实现捕获源文件夹中所有.bak文件的效果 我的验证是,如果目标数据库groupbox中的名称不正确,它将提示错误,而不是还原错误 这是接口 这是我的密码 CheckDBExist public List<string> CheckIfDatabaseExists(string SQLServer, string back

我正在做一个从Microsoft SQL Server恢复/备份数据库的应用程序

当目标数据库的文本框用作新数据库的新名称并将其恢复到sql server中时,如何实现捕获源文件夹中所有.bak文件的效果

我的验证是,如果目标数据库groupbox中的名称不正确,它将提示错误,而不是还原错误

这是接口

这是我的密码

CheckDBExist

public List<string> CheckIfDatabaseExists(string SQLServer, string backupRestore)
{
    bool result = false;
    List<string> DBList = new List<string>();
    string sqlConnectionString = this.rbWindow.Checked ?
                "Server=" + this.cboSQLServer.Text.Trim() + ";Database=master;Trusted_Connection=Yes" :
                "Server=" + this.cboSQLServer.Text.Trim() + ";Database=master;uid=" + this.txtUsername.Text.Trim() + ";pwd=" + this.txtPassword.Text.Trim();
    foreach (Control c in groupBox1.Controls)
    {
        if (c.GetType() == typeof(TextBox))
        {
            SqlConnection tmpConn = new SqlConnection(sqlConnectionString);

            string sqlCreateDBQuery = string.Format("SELECT database_id FROM sys.databases WHERE Name = '{0}'", c.Text);

            using (tmpConn)
            {
                using (SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn))
                {
                    tmpConn.Open();

                    object resultObj = sqlCmd.ExecuteScalar();

                    int databaseID = 0;

                    if (resultObj != null)
                    {
                        int.TryParse(resultObj.ToString(), out databaseID);
                    }

                    tmpConn.Close();

                    result = (databaseID > 0);
                    if ((!result) && (backupRestore == "backup"))
                    {
                        DBList.Add("[" + c.Text + "]");
                    }
                    else if ((result) && (backupRestore == "restore"))
                    {
                        DBList.Add("[" + c.Text + "]");
                    }

                }
            }
        }
    }
    return DBList;
}
这是触发异常的代码

错误显示:

无法打开备份设备“D:\TestBackup\VSM642SP2QC\uu VfsWorkflow.bak.”?>操作系统错误2(系统找不到文件>指定文件)。\r\n存储文件列表异常终止


我该怎么办?

将这行代码添加到您使用的
ReadFileList

destination.Devices.Add(source);

在调用
ReadFileList
方法之前,还原实例必须声明一个
DeviceType
。否则,将引发异常。您正在声明一个
设备类型
,但在失败时从未将其连接到
还原

;在第一个数据库上还是之后?这很奇怪。我从没上过那些课。但是使用Sql Server命令来代替它怎么样?只需执行命令“restore[database name]from disk=[backup file location]。您可以声明一个设备类型(source),但决不能将其连接到还原对象。从MSDN中,还原实例必须在调用此方法之前声明一个设备类型。否则,将引发异常。“我明天会试试。我把我的工作笔记本电脑忘在办公室了。谢谢你的建议:)嗨@Richard Hansell。我确实有另一个问题,请你重新检查并提问,然后给我提建议。看起来在备份的路径上有两个“\\”,当然应该是一个“\”?
        ServerConnection con = new ServerConnection(SQLServer);
        Server server = new Server(con);            

            foreach (Control c in groupBox3.Controls)
            {

                //try
                //{
                    if (c.GetType() == typeof(TextBox))
                    {
                        Restore destination = new Restore();
                        destination.Action = RestoreActionType.Database;
                        destination.Database = c.Text;
                        string backUpFile = outputFolder + "\\" + destination.Database + ".bak";
                        BackupDeviceItem source = new BackupDeviceItem(backUpFile, DeviceType.File);

                        string logFile = Path.GetDirectoryName(backUpFile);
                        logFile = Path.Combine(logFile, destination.Database + "_Log.ldf");

                        string dataFile = Path.GetDirectoryName(backUpFile);
                        dataFile = Path.Combine(dataFile, destination.Database + ".mdf");


                        destination.Devices.Add(source);
                        DataTable logicalRestoreFiles = destination.ReadFileList(server);
                        destination.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[0][0].ToString(), dataFile));
                        destination.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[1][0].ToString(), logFile));
                        destination.ReplaceDatabase = true;
                        destination.SqlRestore(server);
                    }            
                //}
                //catch (Exception ex)
                //{
                //MessageBox.Show(ex.Message);
                //}                                                            
            }      
             }                   
destination.Devices.Add(source);