gpt4 book ai didi

c# - SMO.Restore.SqlRestore 有时会在部署的计算机上引发超时异常

转载 作者:太空狗 更新时间:2023-10-29 22:53:56 43 4
gpt4 key购买 nike

我有一个应用程序用于管理我们软件演示的数据库,它所做的其中一件事是从中央服务器获取数据库副本并将其恢复到本地 SQL 实例。备份部分一切正常,但在恢复部分有人报告他们在恢复过程中遇到以下异常。

Microsoft.SqlServer.Management.Smo.FailedOperationException: Restore failed for Server 'Computername'.
---> Microsoft.SqlServer.Management.Common.ExecutionFailureException:
An exception occurred while executing a Transact-SQL statement or batch.
---> System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
(snip)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
(snip)
at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
at ContractFlowTool.WebInfinity2.AttachDatabase.RestoreLocal(AttachDatabaseArgs arg)

MSDN is fairly light关于 SMO 类(class)的内部工作。我找不到任何方法来更改执行还原的超时时间。我该怎么做才能避免异常发生?


这是执行恢复的代码

private static bool RestoreLocal(AttachDatabaseArgs arg)
{
if (arg.DestDatabase == null)
throw new ArgumentNullException("DestDatabase");
SqlConnectionInfo serverConnInfo = new SqlConnectionInfo(/*snip*/);
ServerConnection serverConn = null;
serverConn = new ServerConnection(serverConnInfo);
var remoteServer = new Server(serverConn);
var clinicFolder = ClinicFolder(arg);
var restore = new Restore();
restore.PercentCompleteNotification = 5;
restore.NoRecovery = false;
restore.RelocateFiles.Add(/*snip mdf*/);
restore.RelocateFiles.Add(/*snip ldf*/);
restore.Database = arg.LocalDB;
restore.ReplaceDatabase = true;
restore.Action = RestoreActionType.Database;
restore.PercentComplete += arg.ProgressForm.Restore_PercentComplete;

restore.SqlRestore(remoteServer);
}

最佳答案

感谢 Cameron 的建议,这个问题的解决方案是我需要将 ServerConnection.StatementTimeout 设置得更高。显然,当数据库大于 3GB 时,这种情况经常发生。

(...) 
serverConn = new ServerConnection(serverConnInfo);
serverConn.StatementTimeout = 240; //<- set this.
var remoteServer = new Server(serverConn);
var clinicFolder = ClinicFolder(arg);
(...)

关于c# - SMO.Restore.SqlRestore 有时会在部署的计算机上引发超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8332138/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com