Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all i am restoring my database through coding, but i am facing an error. The error is "Restore cannot process database 'Frogy' because it is in use by this session. It is recommended that the master database be used when performing this operation. RESTORE DATABASE is terminating abnormally." How do I rectify this error?
Posted
Updated 3-Dec-10 2:37am
v2

Try out following code.If u have any problem I can send u the full form I am already using to restore my db in a software-app.

bool blYesRestoredb = false;
            string strDirectorypath = txtFilePath.Text;
            FileInfo fileRestoreDatabaseFrom = new FileInfo(strDirectorypath);
            string strFileName = fileRestoreDatabaseFrom.Name.ToString();
            string strCurrentDir = Directory.GetCurrentDirectory();
            FileInfo fileTempDB = new FileInfo(strCurrentDir + "\\TempDataBase.sdf");
            
if (fileRestoreDatabaseFrom.Exists == true)
                {
                    if (strFileName.Contains(".sdf") && (strFileName.Split('.').Length == 2) && (fileRestoreDatabaseFrom.Extension==".sdf"))
                    {
                        FileStream FSSource = new FileStream(fileRestoreDatabaseFrom.FullName, FileMode.Open, FileAccess.Read);                        
                        FileStream FSDestination = new FileStream(fileTempDB.FullName,  FileMode.OpenOrCreate, FileAccess.Write);
                        
                        if(fileTempDB.Exists == false)     FSSource.CopyTo(FSDestination);
                        
                        if(FSSource.SafeFileHandle.IsClosed == false) FSSource.SafeFileHandle.Close();
                        if(FSDestination.SafeFileHandle.IsClosed == false) FSDestination.SafeFileHandle.Close();

                        blYesRestoredb = true;
                    }
                    else
                    {
                        MessageBox.Show("Please provide A Valid Database File Having '.sdf' extension",
                            "Invalid Extension",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                    }
                }
                else
                {
                    MessageBox.Show("Please Provide a Valid Database File to Restore.", "File Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);                       
                }
#endregion
}
catch (Exception err)
{
    MessageBox.Show(err.Message, "Error Encountered", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (blYesRestoredb == true && fileTempDB.Length > 0 && fileTempDB.Exists == true )
{
    fileTempDB.Refresh();
    CheckConditionsAndRestoreDatabase();
}
 
Share this answer
 
v2
Comments
Hiren solanki 3-Dec-10 9:04am    
Added 'pre' tags for code visibility.
sameertm 3-Dec-10 9:41am    
hey can u send me the full form,
I am a novoice. Please tell me how to attach file - zip or images to answer


RestoreDB.Designer.cs

namespace eBayWatch
{
partial class RestoreDB
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnCancel = new System.Windows.Forms.Button();
this.btnRestore = new System.Windows.Forms.Button();
this.btnBrowse = new System.Windows.Forms.Button();
this.txtFilePath = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.openFileDialogRestoreDB = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnCancel.Location = new System.Drawing.Point(213, 93);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 29);
this.btnCancel.TabIndex = 14;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// btnRestore
//
this.btnRestore.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnRestore.Location = new System.Drawing.Point(119, 93);
this.btnRestore.Name = "btnRestore";
this.btnRestore.Size = new System.Drawing.Size(75, 29);
this.btnRestore.TabIndex = 13;
this.btnRestore.Text = "Restore";
this.btnRestore.UseVisualStyleBackColor = true;
this.btnRestore.Click += new System.EventHandler(this.btnRestore_Click);
//
// btnBrowse
//
this.btnBrowse.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnBrowse.Location = new System.Drawing.Point(348, 38);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.Size = new System.Drawing.Size(75, 30);
this.btnBrowse.TabIndex = 12;
this.btnBrowse.Text = "Browse";
this.btnBrowse.UseVisualStyleBackColor = true;
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
//
// txtFilePath
//
this.txtFilePath.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtFilePath.Location = new System.Drawing.Point(15, 42);
this.txtFilePath.Name = "txtFilePath";
this.txtFilePath.Size = new System.Drawing.Size(327, 23);
this.txtFilePath.TabIndex = 11;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(12, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(332, 17);
this.label2.TabIndex = 10;
this.label2.Text = "Please select the path to Restore OLD Database : ";
//
// openFileDialogRestoreDB
//
this.openFileDialogRestoreDB.DefaultExt = "sdf";
this.openFileDialogRestoreDB.FileName = "*.sdf";
this.openFileDialogRestoreDB.Title = "Select the database file to be Restored";
this.openFileDialogRestoreDB.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialogRestoreDB_FileOk);
//
// RestoreDB
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(466, 172);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnRestore);
this.Controls.Add(this.btnBrowse);
this.Controls.Add(this.txtFilePath);
this.Controls.Add(this.label2);
this.KeyPreview = true;
this.Name = "RestoreDB";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Restore Previous Database";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.RestoreDB_FormClosing);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnRestore;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.TextBox txtFilePath;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.OpenFileDialog openFileDialogRestoreDB;

}
}


RestoreDB.cs

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.IO;
namespace eBayWatch
{
public partial class RestoreDB : Form
{
public RestoreDB()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnRestore_Click(object sender, EventArgs e)
{
/////above answer code copy here.
// In that CheckConditionsAndRestoreDatabase() method will check db table //contents.
//e.g. If I have 5 rows in current table & 10 rows in the source db //(which is to be restored) than check these things and insert values //accordingly.
//INSPITE OF SIMPLY COPY AND PASTE THE SOURCE DATABASE TO DES. DIRECTLY
 
Share this answer
 
Comments
fjdiewornncalwe 3-Dec-10 11:28am    
Please use the "Improve Answer" link on your original answer instead of adding a brand new one. Cheers.
sandipapatel 3-Dec-10 11:56am    
As previously said...... I am novice.
Check out that text size is limited for providing answer. Otherwise I am not willing for doing such disgusting & frustrating task like reading u r comments.
fjdiewornncalwe 3-Dec-10 12:15pm    
Follow the best practices of the site and you won't have to. (Or at least start learning from the comments you get. I'm not the first to provide information to you)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900