Brian Crist
Brian Crist

Reputation: 814

How to restore databse SQL 2008 in C#?

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

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 error at line cmd.ExecuteNonQuery();

'TEST' is not a recognized RESTORE option.

Can you tell me what i mistake or wrong place.?

Upvotes: 4

Views: 434

Answers (2)

Sami
Sami

Reputation: 3800

You need to connect to the master database, and then you can restore your database.

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

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172438

Try like this:

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

Upvotes: 1

Related Questions