简体   繁体   中英

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.?

试试这样:

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

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#?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM