开发者

How to take a backup of database of sql server 2005 in VB.net Using Code

I want to know how to take a backup of database using programatically in VB.net of SQL Server 2005. If you have an idea about it then please send me开发者_运维问答 the code for that.


SQL Server Database Backup Utility using VB.NET and SQL-DMO (New version)


You can achieve this by using Sql Management Object. SQL Server Management Objects (SMO) are objects designed for programmatic management of Microsoft SQL Server.

Refer 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜