Wednesday, 22 May 2013

Sql Backup database in wpf application using vb.net

Using Stored Procedure


Step 1
Create a stored procedure for Sql Backup database



create procedure [bosusersp].[uSp_Backup]
(
@pvc_DataBase    varchar(50),
@pvc_Filename varchar(50)
)
as

backup database @pvc_DataBase to disk=@pvc_Filename

here we can pass the two parameters as database name and File Storing Location

Step 2

Design Page Like below


Step 3
Write the below code 

#Region "Private Declarations"
        Private con, con1 As SqlConnection
        Private cmd As SqlCommand
        Private dread As SqlDataReader
        Private objConStr As New ConnectionStrings
        Public msDbconn As String = ""
        Private objReadData As New Masters
#End Region
        Public Sub New()
            InitializeComponent()
            con = New SqlConnection("Data Source="".......\SQLEXPR2"";Database=Master;integrated security=SSPI;")
        End Sub
        Private Sub DBbackUp_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            server("........\SQLEXPR2")
        End Sub
        Private Sub server(ByVal str As String)

            con.Open()
            cmd = New SqlCommand("select *  from sysservers  where srvproduct='SQL Server'", con)
            dread = cmd.ExecuteReader
            While dread.Read
                ddlServername.Items.Add(dread(2))
            End While
            dread.Close()
        End Sub
        Sub connection()
         
            ddlDatabase.Items.Clear()
            cmd = New SqlCommand("select * from sysdatabases", con)
            dread = cmd.ExecuteReader
            While dread.Read
                ddlDatabase.Items.Add(dread(0))
            End While
            dread.Close()
        End Sub

        Private Sub cmbserver_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlServername.SelectionChanged
            connection()
        End Sub

        Private Sub blank(ByVal str As String)
            objReadData.ConnectionString = objConStr.DBConnString
            Dim ds As DataSet
            If ddlServername.Text = "" Or ddlDatabase.Text = "" Then
                MsgBox("Server Name & Database Blank Field")
                Exit Sub
            Else
                If str = "backup" Then
                    Dim dlg As New Microsoft.Win32.SaveFileDialog()
                    dlg.FileName = ddlDatabase.SelectedValue
                    ' Default file name
                    dlg.DefaultExt = ".bak"
                    ' Default file extension
                    dlg.Filter = "Text documents (.bak)|*.bak"
                    ' Filter files by extension
                    ' Show save file dialog box
                    Dim result As Nullable(Of Boolean) = dlg.ShowDialog()
                    Dim msDataBase As String = ddlDatabase.SelectedValue
                    ' Process save file dialog box results
                    If result = True Then
                        ' Save document
                        Dim filename As String = dlg.FileName
                        Process()
                        ds = _
            objReadData.ExecuteSprocDS("bosusersp.uSp_Backup", _
               New SqlParameter("@pvc_DataBase", msDataBase), _
               New SqlParameter("@pvc_Filename", filename))
                        ProgressBar1.Value = ProgressBar1.Maximum
                        MessageBox.Show("Success")
                        ProgressBar1.Visibility = Windows.Visibility.Hidden
                    End If

                    '
                End If
            End If
        End Sub
        Private Sub cmbbackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

            ProgressBar1.Visibility = Windows.Visibility.Visible
            blank("backup")
        End Sub

        'Private Sub cmdrestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdrestore.Click
        '    blank("restore")
        'End Sub
        'Create a Delegate that matches
        'the Signature of the ProgressBar's SetValue method
        Private Delegate Sub UpdateProgressBarDelegate(ByVal dp As  _
                         System.Windows.DependencyProperty, _
                         ByVal value As Object)

        Private Sub Process()
            ProgressBar1.Visibility = Windows.Visibility.Visible
            'Configure the ProgressBar
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = Short.MaxValue
            ProgressBar1.Value = 0

            'Stores the value of the ProgressBar
            Dim value As Double = 0

            'Create a new instance of our ProgressBar Delegate that points
            ' to the ProgressBar's SetValue method.
            Dim updatePbDelegate As New  _
                UpdateProgressBarDelegate(AddressOf ProgressBar1.SetValue)

            'Tight Loop: Loop until the ProgressBar.Value reaches the max
            Do Until ProgressBar1.Value = ProgressBar1.Maximum

                value += 1

                'Update the Value of the ProgressBar:
                ' 1) Pass the "updatePbDelegate" delegate
                '    that points to the ProgressBar1.SetValue method
                ' 2) Set the DispatcherPriority to "Background"
                ' 3) Pass an Object() Array containing the property
                '    to update (ProgressBar.ValueProperty) and the new value
                Dispatcher.Invoke(updatePbDelegate, _
                    System.Windows.Threading.DispatcherPriority.Background, _
                    New Object() {ProgressBar.ValueProperty, value})

            Loop
            ' ProgressBar1.Visibility = Windows.Visibility.Hidden
        End Sub
    End Class


Resore database also same as Backup database

Restore database @pvc_DataBase to disk=@pvc_Filename

No comments:

Post a Comment

SQL SERVER – Disk Space Monitoring – Detecting Low Disk Space on Server

CREATE PROCEDURE [CSMSDVLP].[DiskSpaceMonitor] @mailProfile nvarchar(500), @mailto nvarchar(4000), @threshold INT, @logfile nvarchar(40...