I'm using the following code to restore a backup sql database with C# and SMO.
void RestoreDatabaseWithSMO(string sConnect, string dbName, string backUpPath)
{
using (SqlConnection cnn = new SqlConnection(sConnect))
{
cnn.Open();
cnn.ChangeDatabase("master");
ServerConnection sc = new ServerConnection(cnn);
Server sv = new Server(sc);
if (!sv.Databases.Contains(dbName))
throw new Exception("this DataBase does not exist");
// Create backup device item for the backup
BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);
// Create the restore object
Restore resDB = new Restore();
resDB.PercentComplete += new PercentCompleteEventHandler(percentComplete);
resDB.PercentCompleteNotification = 10;
resDB.Devices.Add(bdi);
resDB.NoRecovery = false;
resDB.ReplaceDatabase = true;
resDB.Database = dbName;
resDB.Action = RestoreActionType.Database;
// Restore the database
resDB.SqlRestore(sv);//Exception
}
}
but in the last line I got below exception !!!
{"Restore failed for Server '\\\\.\\pipe\\3F103E6E-3FD4-47\\tsql\\query'. "}
What's wrong with it ?
Could you please guide me? thanks