none
Backup & Restore a sql 2008 DataBase in vb.net 2010

    Question

  • Hello every body,

    i have a database(in sql server 2008) how can i use backup and restore in vb.net 2010?

    thank u!

    • Moved by Bob BeaucheminMVP Thursday, January 13, 2011 1:36 AM Moved to a more relevent forum (From:.NET Framework inside SQL Server)
    Wednesday, January 12, 2011 5:11 AM

Answers

All replies

  • Have a look at the SMO classes for this. There's some examples at http://msdn.microsoft.com/en-us/library/ms162133.aspx. In a few hours, I'll move this question/answer to the SMO forum as that's where to return if you run into issues.

    Cheers, Bob Beauchemin

    • Marked as answer by KJian_ Tuesday, January 18, 2011 8:03 AM
    Wednesday, January 12, 2011 1:08 PM
  • Hi,

    In .NET, we can backing up and restore a SQL Server database using SQL Server Management Objects (SMO). In addition to Bob’s reply, you can also refer to the following links although they are for SQL Server 2005:

    Getting Started with SMO in SQL 2005 - Backups
    http://www.sqldbatips.com/showarticle.asp?ID=37
     
    Getting Started with SMO in SQL 2005 - Restores
    http://www.sqldbatips.com/showarticle.asp?ID=40

    Alternatively, we can use ADO.NET objects to execute T-SQL statements or stored procedures to restore SQL Server databases. Please see:

    BACKUP (Transact-SQL)
    http://technet.microsoft.com/en-us/library/ms186865.aspx

    RESTORE (Transact-SQL)
    http://msdn.microsoft.com/en-us/library/ms186858.aspx

    Backup databases stored procedure 
    http://weblogs.sqlteam.com/tarad/archive/2004/08/04/1876.aspx

    SQL SERVER – Restore Database Backup using SQL Script (T-SQL)
    http://blog.sqlauthority.com/2007/02/25/sql-server-restore-database-backup-using-sql-script-t-sql/


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by KJian_ Tuesday, January 18, 2011 8:03 AM
    Thursday, January 13, 2011 6:27 AM
  • hello

    You can try these sql queries:

    For BackUp:


    BACKUP DATABASE  databaseName TO disk= ‘Filename


    For Restore:


    RESTORE DATABASE databaseName FROM disk=’Filename


    The code example shows how to back up an existing SQL database to a file and then how to restore it using VB..Net

    how-to-backup-and-restore-of-sql-server-database-through-vb-net


    Hirendra
    Monday, December 05, 2011 5:40 AM
  • Hey try this code

     

    Try

     Dim s As StreamWriter

    Dim portfolioPath As String = My.Application.Info.DirectoryPath

    If Not Directory.Exists("C:\Backup") Then

    Directory.CreateDirectory("C:\Backup")

    File.Create("C:\ Backup\PIS.Mdb").Close()

    File.Create("C: \Backup\Backup log.rtf").Close()

    s = New StreamWriter("C:\Backup\Backup log.rtf", True)

    s.WriteLine("This backup was initially taken on - " & Date.Now)

                    s.Flush()

                    s.Close()

    FileCopy(portfolioPath & "\PIS.mdb", "C:\Backup\PIS.Mdb")

    s = New StreamWriter("C:\Backup\Backup log.rtf", True)

     MsgBox("New directory and backup file created")

                Else

                    'Creating a directory by specifying a path in the TextBox, of the form c:\examples

                    'Instead of using a TextBox you can directly type the location of the directory like this

                    'Directory.CreateDirectory("c:\examples")

    FileCopy(portfolioPath & "\PIS.mdb", "C:\Backup\PIS.Mdb")

    s = New StreamWriter("C:\Backup\Backup log.rtf", True)

    s.WriteLine("This backup was latest updated on - " & Date.Now)

                    s.Flush()

                    s.Close()

    MsgBox("Back up completed successfully")

                End If

     

            Catch ex As Exception

    Dim MessageString As String = "Report this error to the system administrator: " & ControlChars.NewLine & ex.Message

          Dim TitleString As String = "Employee Master Details Data Load Failed"

           MessageBox.Show(MessageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

    and for detail please visit: 

    http://ram-a-singh.blogspot.in/2011/12/how-to-take-backup-from-database-vbnet.html





    • Proposed as answer by rasingh1 Tuesday, December 20, 2011 7:35 PM
    • Edited by rasingh1 Saturday, September 07, 2013 9:09 AM link update
    Tuesday, December 20, 2011 7:35 PM