[英]Backup Sql server Database with vb.net
I have written some codes in vb.net to backup a database in local computer.我在 vb.net 中编写了一些代码来备份本地计算机中的数据库。 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.
使用 SQL 服务器管理,我可以毫无问题地备份,但我的代码有问题。
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
A SqlCommand
has a CommandTimeout
value of 30 seconds by default.默认情况下,
SqlCommand
的CommandTimeout
值为 30 秒。 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.如果您的操作需要超过 30 秒才能完成,请将
CommandTimeout
设置为更大的值。 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.我不确定,但我怀疑仍然会执行备份,即使您的应用程序抛出了该异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.