具有vb.net的备份Sql服务器数据库

问题描述 投票:0回答:1

我已经在vb.net中编写了一些代码来备份本地计算机中的数据库。在许多计算机中,它都能正常工作。但是在特定的计算机上,我遇到此错误:*“超时已到期。在操作完成之前超时或服务器没有响应。备份或还原被中止。”使用SQL Server Management,我可以毫无问题地进行备份,但是使用我的代码,我有问题。

Private Sub BackupDataBase()
    Dim cnn_str As String = "Data Source=mycomputer\rb;Database=Master;integrated security=SSPI;"

    Dim _backupFileName As String = "d:\backup.bak"

    Dim query As String = "BACKUP DATABASE rb_db TO DISK='" & _backupFileName & "' WITH INIT"
    Dim cnn As New SqlConnection(cnn_str)
    Dim cmd As New SqlCommand(query, cnn)

    Try
        If cnn.State = ConnectionState.Closed Then cnn.Open()
        cmd.ExecuteNonQuery()
        MsgBox("Backup successful!")
    Catch ex As Exception
        MessageBox.Show("Backup failed!" & vbNewLine & Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
sql-server vb.net backup
1个回答
0
投票

A SqlCommand的默认CommandTimeout值为30秒。如果指定的操作在该时间段内未完成,则将引发异常。

如果您的操作需要30秒钟以上才能完成,请将CommandTimeout设置为更大的值。执行时间将取决于系统硬件和当前负载。

我不确定,但是我怀疑即使您的应用程序抛出了该异常,备份仍然会执行。

© www.soinside.com 2019 - 2024. All rights reserved.