.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to take Backup of Database at runtime SQL-2005

Posted By:Santosh       Posted Date: October 10, 2009    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  
 

How to take Backup of Database at runtime SQL-2005.


Try with below code to take backup at run-time in sql server 2005
call TakeBackUp in page load or button click
Please add reference below packages


Microsoft.SqlServer.Server
Microsoft.SqlServer.Management.Smo



Imports Microsoft.SqlServer.Server
Imports Microsoft.SqlServer.Management.Smo
Imports System.IO
 Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
        'my.Application.
        TakeBackUp("DMS", "D:\Mail\ss.bak") 'DMS is the correct database name exist in server
    End Sub


  Private Sub TakeBackUp(ByVal BackupDBName As String, ByVal FileNamePath As String)
        Try
            Dim sqlServerInstance As New Server(New Microsoft.SqlServer.Management.Common.ServerConnection _
            (New System.Data.SqlClient.SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=;Data Source=SANTOSH\SQLEXPRESS"))) 'This is only for connection if u give Initial Catalog null it will work
            Dim objBackup As New Backup
            objBackup.Devices.AddDevice(FileNamePath, DeviceType.File)
            objBackup.Database = BackupDBName
            objBackup.Action = BackupActionType.Database
            objBackup.SqlBackup(sqlServerInstance)
            MessageBox.Show("The backup of database " & "'" & BackupDBName & "'" & " completed sccessfully", "Microsoft SQL Server Management Studio", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


Santosh
http://santoshdotnetarena.blogspot.com/


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend