简体   繁体   English

恢复数据库备份时出错

[英]Error in restoring backup of database

I know it may seem a duplicate question but previous questions could not solve my problem. 我知道这似乎是一个重复的问题,但以前的问题无法解决我的问题。
I used this code to restore the backup that I have created. 我使用此代码还原了我创建的备份。

public string conrestore = "Data Source=.;Integrated Security=True";

private void btn_restore_Click(object sender, EventArgs e)
    {

        SqlConnection.ClearAllPools();

        using (SqlConnection con = new SqlConnection(conrestore))
        {
            ServerConnection srvconn = new ServerConnection(con);


            Server srvr = new Server(srvconn);

            if (srvr != null)
            {
                try
                {
                    Restore restoredb = new Restore();

                    restoredb.Action = RestoreActionType.Database;

                    restoredb.Database = "3LStoreDB";

                    OpenFileDialog openfd = new OpenFileDialog();

                    openfd.Filter = "Backup File (*.Bak)|*.Bak";

                    Nullable<bool> result = Convert.ToBoolean(openfd.ShowDialog());

                    if (result == true)
                    {
                        BackupDeviceItem bkDevice = new BackupDeviceItem(openfd.FileName, DeviceType.File);

                        restoredb.Devices.Add(bkDevice);

                        restoredb.ReplaceDatabase = true;

                        restoredb.SqlRestore(srvr);

                        MessageBox.Show("Restored successfully.");

                    }
                }
                catch(Exception ex) 
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

It works properly till the line 直到线路正常工作

restoredb.SqlRestore(srvr);

And it shows error Restore failed for server 'Server Name' 并且它显示错误Restore failed for server 'Server Name'

I don't know what's wrong with this code and I would really appreciate any help. 我不知道这段代码有什么问题,我将非常感谢您的帮助。

Well it's amazing and I don't know why this happened. 好吧,这太神奇了,我不知道为什么会这样。 But I Logged Off windows and again Logged in and now it works properly. 但是我注销了Windows并再次登录,现在它可以正常工作了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM