Alireza
Alireza

Reputation: 9

Backup Sql server Database with vb.net

I have written some codes in vb.net to backup a database in local computer. In many computers it work without problem. but in a specific computer I encounter with this error :*"Timeout expired. The timeout elapsed prior to completion of the operation or the server is not responding The backup or restore was aborted." With SQL server Management I can backup without problem but with my codes I have problem.

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

Upvotes: 0

Views: 679

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54457

A SqlCommand has a CommandTimeout value of 30 seconds by default. If the operation specified does not complete in that time period, an exception is thrown.

If your operation requires more than 30 seconds to complete, set the CommandTimeout to a greater value. The time to execute will depend on the system hardware and current load.

I don't know for sure but I suspect that the backup will still be performed, even if that exception is thrown by your application.

Upvotes: 2

Related Questions