How to restore databse SQL 2008 in C #?

I want to restore a database in C #, use the .BAK file type

var db = new QLTDEntities();
var con = ((SqlConnection)db.Database.Connection);
con.Open();
string stringquery = "RESTORE DATABASE TEST FROM DISK ='D:\ABC.BAK'";
SqlCommand cmd = new SqlCommand(stringquery, con);
cmd.ExecuteNonQuery();
con.Close();

I get an error in the line cmd.ExecuteNonQuery();

'TEST' is not a recognized RESTORE option.

Can you tell me that I am wrong or wrong.?

+4
source share
2 answers

Try it like this:

string stringquery = @"USE master BACKUP DATABASE [TEST] TO DISK='D:\ABC.BAK'";
+1
source

You need to connect to the main database and then restore the database.

Take a look at How to recover a SQL Server 2012.bak database file in C #?

0
source

Source: https://habr.com/ru/post/1612881/


All Articles