Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Here, I have show my c# code for BackUp and Restore with Sql Database, in that back up is working properly but when i try to restore the database from backup folder, that time one error is occurred, and the error is:
RESTORE cannot process database 'UsersDB' 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.


C#
protected void btnbackup_Click(object sender, EventArgs e)
    {
        sqlcon.ConnectionString = (System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
        string destdir = "C:\\backupdb";
        if (!System.IO.Directory.Exists(destdir))
        {
            System.IO.Directory.CreateDirectory("C:\\backupdb");
        }
        try
        {
            sqlcon.Open();
            sqlcmd = new SqlCommand("backup database UsersDB to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
            sqlcmd.ExecuteNonQuery();
            Response.Write("Backup database successfully");
        }
        catch (Exception ex)
        {
            Response.Write("Error During backup database!");
        }
    }


C#
protected void btnrestore_Click(object sender, EventArgs e)
   {
       //try
       //{


       sqlcon.ConnectionString = (System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
       sqlcon.Open();
       string destdir = "C:\\backupdb\\11082014_121403.Bak";

       sqlcmd = new SqlCommand("Restore database UsersDB from disk='C:11082014_143650.Bak' ", sqlcon);
       sqlcmd.ExecuteNonQuery();
       Response.Write("restore database successfully");
       //}
       //catch (Exception ex)
       //{
       //    Response.Write("Error During backup database!");
       //}


   }
Posted
Comments
VishwaKL 11-Aug-14 5:22am    
try this code
http://stackoverflow.com/questions/21305192/restore-database-using-c-sharp-and-smo

Refer this
error-restore-cannot-process-database-test-db-because-it-is-in-use-by-this-se[^]

The error shows it all.. before taking backup, shift it to master (use master) and then take the backup.

Hope it will help..
 
Share this answer
 
Comments
Pratham4950 11-Aug-14 7:05am    
but, where i will put the master. can you please tell me in brief ?
[no name] 11-Aug-14 8:00am    
use master;Restore database in a single line
Pratham4950 12-Aug-14 5:44am    
thx, it's working...
Sid_Joshi 13-Aug-14 5:18am    
How to set Restore location for Database?
Pratham4950 13-Aug-14 6:49am    
Still, i have mention by default in 'C' Drive.
Hello

By default created database is create for multi-user. You should make database for single user when perform the restore operation.


SQL Server 2005 Database Backup and Restore using C# and .NET 2.0[^]

OR

SQL Server 2008 - Backup and Restore Databases using SMO[^]
 
Share this answer
 

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