By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

problem whille restoring the database

P: 8
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
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10.  
  11. namespace Email_Client
  12. {
  13.     public partial class Backup : Form
  14.     {
  15.  
  16.         private SqlCommand cmd;
  17.         string sql = "";
  18.         public Backup()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void button2_Click(object sender, EventArgs e)
  24.         {
  25.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
  26.             con.Open();
  27.             try
  28.             {
  29.                 SqlCommand cmd = new SqlCommand("backup database email_client to disk ='" + textBox1.Text + "\\" + textBox2.Text + ".bak'", con);
  30.                 cmd.ExecuteNonQuery();
  31.                 MessageBox.Show("done");
  32.  
  33.  
  34.             }
  35.             catch (Exception ex)
  36.             {
  37.                 MessageBox.Show(ex.Message);
  38.             }
  39.         }
  40.  
  41.         private void button1_Click(object sender, EventArgs e)
  42.         {
  43.             FolderBrowserDialog dlg = new FolderBrowserDialog();
  44.             if (dlg.ShowDialog() == DialogResult.OK)
  45.             {
  46.                 textBox1.Text = dlg.SelectedPath;
  47.             }
  48.         }
  49.  
  50.         private void button4_Click(object sender, EventArgs e)
  51.         {
  52.             OpenFileDialog dlg = new OpenFileDialog();
  53.             dlg.Filter = "Backup files(*.bak)|*.bak|All Files(*.*)|*.*";
  54.             dlg.FilterIndex = 0;
  55.             if (dlg.ShowDialog() == DialogResult.OK)
  56.             {
  57.                 textBox3.Text = dlg.FileName;
  58.             }
  59.  
  60.         }
  61.  
  62.         private void button3_Click(object sender, EventArgs e)
  63.         {
  64.  
  65.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
  66.             con.Open();
  67.             try
  68.             {
  69.  
  70.                 sql = "alter database email_client set single_user with rollback immediate ;";
  71.                 sql += "RESTORE database email_client from disk='"+textBox3.Text+"'with replace;";
  72.                 cmd = new SqlCommand(sql, con);
  73.                 cmd.ExecuteNonQuery();
  74.                 con.Close();
  75.                 con.Dispose();
  76.                 MessageBox.Show("done");
  77.  
  78.             }
  79.             catch (Exception ex)
  80.             {
  81.                 MessageBox.Show(ex.Message);
  82.             }
  83.  
  84.         }
  85.  
  86.         private void Backup_Load(object sender, EventArgs e)
  87.         {
  88.  
  89.         }
  90.     }
  91. }
  92.  
Attached Images
File Type: jpg Screenshot (16).jpg (35.2 KB, 75 views)
Feb 22 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.