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

时间:2011-11-30 19:57:17

标签: c# sql-server sql-server-2005 smo

我有一个用于管理我们软件演示的数据库的应用程序,它所做的一件事是从中央服务器获取数据库的副本并将其还原到本地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)

关于SMO类内部工作的MSDN is fairly light。我找不到任何方法来更改执行还原的超时时间。我能做什么才能解除异常呢?


以下是执行恢复的代码

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);
}

2 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:0)

U可以使用此功能,特别是当您无法修改连接字符串或连接字符串中的超时修改不起作用时 它对我来说适用于超过2 GB的镜像数据库

使用(SqlConnection1)

Dim sqlStmt As String = String.Format(“BACKUP DATABASE map TO DISK ='{0}'”,backup_directory + backupfile)

            Using bu2 As New SqlCommand(sqlStmt, SqlConnection1)
                SqlConnection1.Open()
                bu2.CommandTimeout = 180   //this line is the key
                bu2.ExecuteNonQuery()
                SqlConnection1.Close()
            End Using
        End Using