I try to copy a database from a networked SQLServer to a local SQLExpress:
private void DoRestore()
{
if (!File.Exists(fileNach))
{
MessageBox.Show("Restore gescheitert " + fileNach);
return;
}
Restore res = new Restore();
Server srv = new Server(Environment.MachineName.ToString()+@"\SQLEXPRESS");
//MessageBox.Show("Ziel Server :"+srv.Name);
try
{
string fileName = @fileNach;
string databaseName = "Outdoor";
srv.KillAllProcesses(databaseName);
res.Database = databaseName;
res.Action = RestoreActionType.Database;
res.Devices.AddDevice(fileName, DeviceType.File);
res.ReplaceDatabase = true;
res.NoRecovery = true;
string odb;
string odblog;
odb = @"C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Outdoor.mdf";
odblog = @"C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Outdoor.ldf";
res.RelocateFiles.Add(new RelocateFile("Outdoor", odb));
res.RelocateFiles.Add(new RelocateFile("Outdoor_Log", odblog));
// restore starten
res.SqlRestore(srv);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
This code starts a restore process on the SQLExpress but never ends. The mdf and log files are created in the destination directory but the restore does not finalize.
I also tried with srv.KillAllProcesses(databaseName);
but no result.
No error is raised at the C# level.
You say it never ends, is what your seeing that your code finishes but the database is stuck in "restoring" mode? If so, I think you want to restore with recovery, otherwise you're leaving the database non-operational. Try setting NoRecovery to false instead of true:
res.NoRecovery = false;
From here
Leave the database ready for use by rolling back the uncommitted transactions. Additional transaction logs cannot be restored. (RESTORE WITH RECOVERY)
Recovers the database. This option is equivalent to the RECOVERY option in a Transact-SQL RESTORE statement.
Choose this option only if you have no log files you want to restore.
Leave the database non-operational, and do not roll back the uncommitted transactions. Additional transaction logs can be restored. (RESTORE WITH NORECOVERY)
Leaves the database in an unrecovered state. This option is equivalent to using the NORECOVERY option in a Transact-SQL RESTORE statement.
When you choose this option, the Preserve replication settings option is unavailable.