从联网 SQLServer 的 SMO 恢复永远不会完成(从 C# 调用)
我尝试将数据库从联网的 SQLServer 复制到本地 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());
}
}
此代码在 SQLExpress 上启动恢复过程,但永远不会结束。 mdf 和日志文件会在目标目录中创建,但恢复不会最终完成。
我也尝试过 srv.KillAllProcesses(databaseName); ,但没有结果。
C# 级别不会引发任何错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你说它永远不会结束,你看到你的代码完成但数据库陷入“恢复”模式吗?如果是这样,我认为您希望通过恢复进行恢复,否则您将导致数据库无法运行。尝试将 NoRecovery 设置为 false 而不是 true:
从此处
离开通过回滚未提交的数据库准备使用
交易。无法恢复其他事务日志。
(恢复并恢复)
恢复数据库。该选项相当于RECOVERY
Transact-SQL RESTORE 语句中的选项。
仅当您没有想要的日志文件时才选择此选项
恢复。
让数据库处于非操作状态,并且不要回滚
未提交的交易。额外的事务日志可以是
恢复了。 (恢复但不恢复)
使数据库处于未恢复状态。这个选项是
相当于在 Transact-SQL RESTORE 中使用 NORECOVERY 选项
声明。
当您选择此选项时,保留复制设置
选项不可用。
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:
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.