I am using the following code to restore a backup sql database using 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");
BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);
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;
resDB.SqlRestore(sv);
}
}
but in the last line I got less exception !!!
{"Restore failed for Server '\\\\.\\pipe\\3F103E6E-3FD4-47\\tsql\\query'. "}
What is wrong with him?
Could you guide me? thank
source
share