Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello developers,
Actually my problem is, I made a project in c# with service-based Database. Everything is going good. But I am stuck with backup and Restore process.
I have done Backup and I got databasename.bak file But I don't know how to Restore that BAK file in service-based Database.
If possible please tell me that is it possible and if it is then please tell me how to do that. Is there any third-party software that I have to use or is it done by Code.
Posted
Updated 25-Feb-15 21:59pm
v2
Comments
[no name] 26-Feb-15 4:42am    
Have you checked MSDN Documentation?
https://msdn.microsoft.com/en-us/library/ms186858%28v=sql.105%29.aspx
Member 11302460 26-Feb-15 7:58am    
No I did not try that option...
but I tried to do restore with some of SQL Query and it giving me error "Database already in use."
[no name] 26-Feb-15 8:06am    
Check this:
http://stackoverflow.com/questions/3639530/unable-to-restore-database-in-sql-server-single-user

If it doesn't help you should post here what exactly you have tried (your SQL script and other actions taken, e.g. closing connections). Your question as it stands doesn't help much in identifying what the problem could be.
Member 11302460 27-Feb-15 2:34am    
I tried your both link but it is not working showing error "database already in use"
Let me explain you more about my project, Actually I made an Windows based application with service-based database which create two files databasename.mdf and databasename_log.ldf in project folder. Which I want to restore with .bak file.
I am using Query


Query = "RESTORE DATABASE DBName FROM DISK = 'C:\path\DBNameBackup.bak'";
sqlcmd = new SqlCommand(Query, sqlcon);
sqlcmd.ExecuteNonQuery();


And this is my connectionstring:-
public static string constring = "Data Source=.\\SQLExpress; Integrated Security=True; AttachDbFilename=|DataDirectory|\\Database1.mdf; User Instance=True; Integrated security=true;";

1 solution

I'm no expert in this field but I'm fairly certain that the problem is that you're using SQL-Express together with the Connectionstring-Option "AttachDBFileName".

Please have a look at these sites for further information:
attachdbfilename-option-in-connection-string[^]
sql-server-backuprestore[^]
how-to-backup-and-restore-sql-express-2005-attachdbfilename-mode[^]
connecting-to-sql-express-user-instances-in-management-studio.aspx[^]

From the third linked site I "extracted" this as a possible solution:

1) For the restore, remove the "AttachDBFileName"-Part from the Connectionstring.

2) Use this for the restore command:
C#
string destinationDb = "<path>\\Database1.mdf";
string backupDb = "<path>\\DBNameBackup.bak";
string query = "USE [master]; RESTORE DATABASE [" + destinationDb + "] FROM DISK = N'" + backupDb + "' WITH REPLACE;";


If this is the solution for your problem, please mark this as accepted :-)
If not, please tell and I will try to dig deeper into it.
 
Share this answer
 
v2
Comments
Member 11302460 27-Feb-15 5:12am    
if (sqlcon.State != ConnectionState.Open)
sqlcon.Open();


string destinationdb = Application.StartupPath + "\\Database1.mdf";


string query = "USE [master]; RESTORE DATABASE [" + destinationdb + "] FROM DISK = N'" + "d:\\svBackUp1.bak" + " ' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10";

SqlCommand cmd = new SqlCommand(query, sqlcon);



int b = cmd.ExecuteNonQuery();
if (b > 0)
{
}

this is the code which I am using for restore but I got error..

"Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.
Changed database context to 'master'."

Not Resolve... Please Give me another solution.....
Thanks
[no name] 27-Feb-15 5:16am    
You removed the "AttachDBFileName"-Part from the Connectionstring?
Member 11302460 27-Feb-15 5:19am    
SqlConnection sqlcon = new SqlConnection("Data Source=.\\SQLExpress; User Instance=True; Integrated security=true;");

is this true or I need to change it
[no name] 27-Feb-15 5:20am    
Well, it's how I thought it should work. I'll try to replicate the whole thing on my system. Will take a while but I'll get back 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