使用 C# 恢复 SQL Server 2005 数据库。回滚问题
大家好,我刚刚开始学习 C#。我正在尝试恢复 .bak 文件。但是我收到了错误。由于数据库正在使用中,无法获得独占访问权限。
我在这里进行了研究< /a> 和 这里都说我必须执行回滚。我不知道如何在我的恢复代码中应用回滚。
public void RestoreDatabase(String RestorePath)
{
try
{
SqlConnection sqlCon = new SqlConnection("Data Source=RITZEL-PC\\SQLEXPRESS;User ID=NNIT-Admin;Password=password;Initial Catalog=master;");
ServerConnection connection = new ServerConnection(sqlCon);
Server sqlServer = new Server(connection);
Restore restoreDB = new Restore();
restoreDB.Database = "NNIT DB";
restoreDB.Action = RestoreActionType.Database;
restoreDB.Devices.AddDevice(RestorePath, DeviceType.File);
restoreDB.ReplaceDatabase = true; // will overwrite any existing DB
restoreDB.NoRecovery = false; // NoRecovery = true;
restoreDB.SqlRestore(sqlServer);
MessageBox.Show("Restored");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " " + ex.InnerException);
}
}
Hi I'm just starting to learn C#. I am trying to restore a .bak file. However I am getting the error. Exclusive access cannot be obtained because the database is in use.
I did my research here and here both says I have to perform a rollback. I do not know how to apply rollback in my restore code.
public void RestoreDatabase(String RestorePath)
{
try
{
SqlConnection sqlCon = new SqlConnection("Data Source=RITZEL-PC\\SQLEXPRESS;User ID=NNIT-Admin;Password=password;Initial Catalog=master;");
ServerConnection connection = new ServerConnection(sqlCon);
Server sqlServer = new Server(connection);
Restore restoreDB = new Restore();
restoreDB.Database = "NNIT DB";
restoreDB.Action = RestoreActionType.Database;
restoreDB.Devices.AddDevice(RestorePath, DeviceType.File);
restoreDB.ReplaceDatabase = true; // will overwrite any existing DB
restoreDB.NoRecovery = false; // NoRecovery = true;
restoreDB.SqlRestore(sqlServer);
MessageBox.Show("Restored");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " " + ex.InnerException);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 SMO,您可以像这样设置用户访问和回滚:
Using SMO you can set user access and rollback like this:
这有效吗?
Does this work?