问题whille恢复数据库 [英] problem whille restoring the database

查看:109
本文介绍了问题whille恢复数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为备份和恢复机制设计了一个应用程序。当我按下备份按钮时,它将在所选路径上成功创建备份文件。但是当我想要恢复相同的数据库时,那时它会向我显示错误 RESTORE无法处理数据库'email_client',因为它正在使用此会话。建议在执行此操作时使用master数据库.RESTORE DATABASE异常终止



所以请提供编码



I design one application for backup and restore mechanism . When I press on the backup button it will successfully creates the backup file on the selected path. But when I want to restore the same database then that time it showing me an error "RESTORE cannot process database 'email_client' because it is in use bye this session. It is recommended that the master database be used when performing this operation.RESTORE DATABASE is terminating abnormally"

so please provide coding for it

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Email_Client
{
    public partial class Backup : Form
    {
        
        private SqlCommand cmd;
        string sql = "";
        public Backup()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
            con.Open();
            try
            {
                SqlCommand cmd = new SqlCommand("backup database email_client to disk ='" + textBox1.Text + "\\" + textBox2.Text + ".bak'", con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("done");


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

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = dlg.SelectedPath;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Backup files(*.bak)|*.bak|All Files(*.*)|*.*";
            dlg.FilterIndex = 0;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = dlg.FileName;
            }
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
            con.Open();
            try
            {

                sql = "alter database email_client set single_user with rollback immediate ;";
                sql += "RESTORE database email_client from disk='"+textBox3.Text+"'with replace;";
                cmd = new SqlCommand(sql, con);
                cmd.ExecuteNonQuery();
                con.Close();
                con.Dispose();
                MessageBox.Show("done");

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

        }

        private void Backup_Load(object sender, EventArgs e)
        {

        }
    }
}

推荐答案

我是这样做的:用C#备份SQL数据库 [ ^ ]
Here is how I do it: Backing up an SQL Database in C#[^]


这篇关于问题whille恢复数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆