473,468 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem whille restoring the database

8 New Member
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, 137 views)
Feb 22 '14 #1
0 1145

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

Similar topics

0
by: JW | last post by:
I have 2 backup files (using mysqldump.) The first one is for the whole database, backed up from Januaray to July 25. The second one is just for 2 tables of that database, backed up from June 29 to...
1
by: Tom Loach | last post by:
I have the need to restore a database to a user's laptop on a daily basis. This has been no problem using Enterprise Manager. I found a stored procedure to run the restore and tried to call it...
1
by: Yass | last post by:
Dear Newsgroup, I am using sql server 2000 over win 2000 server with service pack 4. I have been given a back up of a database (I have tried both from T-SQL and Enterprise Manager) T-SQL
6
by: Rajesh Kumar Mallah | last post by:
Hi, I face the following problem in transferring data from pgsql 7.3.4 to pgsql 7.4 RC1 . I am piping the output of pg_dumpall from 7.3 to 7.4 running on different port. The problem is there...
0
by: Wind Wood | last post by:
Hello £¡ I can dump data from old dababase(Postgresql 6.5.3), but new problem came out when I restore the data to postgresql 7.4. It went out during the COPY command, if I do it in such command...
3
by: mike | last post by:
I have just upgraded froom Fedora test1 to test2 which has upgraded postgres version from 7.4.2 to 7.4.5 (frrom rpm -q) I have used pg_dumpall to dump my database, the restore appeared to go OK,...
0
by: mahesh123 | last post by:
Hi, I am new to use the crystal reports8.0. I am facing the problem regarding the database connection through the ADO Connection. Suppose my database name is "sample.pra"(due to some security...
0
by: DMT012 | last post by:
Hello. Occasionally on my production server when I run an automated script to restore a database from a backup it hangs and the database is put in restoring mode and just stays there, requiring me...
1
by: babu | last post by:
Hi all I am trying to take exchange server 2003 backup and restore programmatically. My backup function works fine. But i am in problem in restoring database. My HrESERestoreAddDatabase is not...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.