如何在VB.net中备份sql server 2005的数据库使用代码

问题描述:

我想知道如何在SQL Server 2005的VB.net中以编程方式进行数据库的备份。如果您有关于它的想法,那么请给我这个代码。如何在VB.net中备份sql server 2005的数据库使用代码

+0

我已经提到我的答案的文章必须帮助你,但那么你必须拿出代码,按您的要求。 – 2010-04-30 11:08:34

您可以通过使用Sql管理对象来实现此目的。 SQL Server管理对象(SMO)是为Microsoft SQL Server的程序化管理而设计的对象。

参考How to: Back Up Databases and Transaction Logs in Visual Basic .NET

Imports System.IO 
Imports System.Data.SqlClient 

Public Class Form2 
    Dim con As SqlConnection 
    Dim cmd As SqlCommand 
    Dim dread As SqlDataReader 
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     server(".") 
     server(".\sqlexpress") 
    End Sub 

    Sub server(ByVal str As String) 
     con = New SqlConnection("Data Source=" & str & ";Database=Master;integrated security=SSPI;") 
     con.Open() 
     cmd = New SqlCommand("select * from sysservers where srvproduct='SQL Server'", con) 
     dread = cmd.ExecuteReader 
     While dread.Read 
      ComboBox1.Items.Add(dread(2)) 
     End While 
     dread.Close() 
     con.Close() 
    End Sub 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
     If ComboBox1.Text = "" Then 
      MsgBox("select the server name") 
      ComboBox1.Focus() 
      Exit Sub 
     End If 
     Try 
      ComboBox2.Text = "" 
      con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;") 
      con.Open() 
      If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 
       ComboBox2.Text = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName) 
       cmd = New SqlCommand("drop database " & ComboBox2.Text & " ", con) 
       cmd.ExecuteNonQuery() 
       cmd = New SqlCommand("RESTORE DATABASE " & ComboBox2.Text & " FROM disk='" & OpenFileDialog1.FileName & "'", con) 
       cmd.ExecuteNonQuery() 
       MsgBox("Restore Successfully Completed") 
       ComboBox1.Text = "" 
       ComboBox2.Text = "" 
      Else 
       MsgBox("Restore Cancelled") 
      End If 
      con.Close() 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     If ComboBox1.Text = "" Then 
      MsgBox("select the server name") 
      ComboBox1.Focus() 
      Exit Sub 
     ElseIf ComboBox2.Text = "" Then 
      MsgBox("select the database name") 
      ComboBox2.Focus() 
      Exit Sub 
     End If 
     Try 
      con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;") 
      con.Open() 
      SaveFileDialog1.FileName = ComboBox2.Text + ".bak" 
      If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 
       Dim s As String 
       s = SaveFileDialog1.FileName 
       cmd = New SqlCommand("backup database " & ComboBox2.Text & " to disk='" & s & "'", con) 
       cmd.ExecuteNonQuery() 
       MsgBox("Back Up Successfully Created") 
       ComboBox1.Text = "" 
       ComboBox2.Text = "" 
       con.Close() 
      Else 
       MsgBox("Back Up Cancelled") 
      End If 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
    End Sub 

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged 
     con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;") 
     con.Open() 
     ComboBox2.Items.Clear() 
     cmd = New SqlCommand("select * from sysdatabases", con) 
     dread = cmd.ExecuteReader 
     While dread.Read 
      ComboBox2.Items.Add(dread(0)) 
     End While 
     dread.Close() 
     con.Close() 
    End Sub 
End Class