简体   繁体   English

使用 vb.net 备份 Sql 服务器数据库

[英]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.默认情况下, SqlCommandCommandTimeout值为 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.

相关问题 备份 SQL 服务器 localdb 在 vb.net - Backup SQL Server localdb in vb.net 用于备份和保存 SQL Server 数据库的 VB.Net 代码不起作用 - VB.Net code to Backup and Save SQL Server database won't work vb.net中的本地SQL Server备份异常 - Local SQL Server Backup Exception in vb.net VB.NET中的MSSQLLocalDb数据库备份 - MSSQLLocalDb Database Backup in VB.NET 如何使用 C# 或 VB.net 以编程方式生成 SQL Server 数据库备份,仅包含选定的表数据 - How can I generate a SQL Server database backup programmatically using C# or VB.net with selected table data only 如何从数据库SQL Server备份特定表并使用vb.net代码将.bak文件保存在计算机中 - how to backup of specific tables from database sql server and save .bak file in computer using vb.net code 来自VB.NET的SQL Server备份不生成备份文件,文件未显示在驱动器中 - SQL Server backup from VB.NET not generating backup file, file not shown in drive 无法从DataGridView保存到SQL Server数据库VB.Net - Unable to save from DataGridView to SQL Server Database VB.Net VB.NET未在SQL Server 2008 Express中创建数据库 - VB.NET not creating database in sql server 2008 express 使用VB.Net将数据插入SQL Server数据库 - Inserting data into SQL Server database using VB.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM