Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Backup SQL Server Database in ASP.NET

Author: Alanna Kremer
Download Source Code : 1297_BackupSQLDatabase.zip

You can simply Backup SQL Server Database by using SQL Server Management Studio but sometimes you need to do it programmatically in ASP.NET.

 

There are different ways to do SQL Server Database Backup programmatically. You can write a stored procedure to backup your database and can execute it from code file. In this article, I will show you how to backup your SQL Server Database only using code file. You must have to add necessary references and include couple of namespaces to do this.

Follow these steps to Backup “master” Database for this example. You can change Database name as per your requirements. You can also download source code of this example.

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual C# or Visual Basic > ASP.NET Empty Web Site > Click Ok
  4. Website > Add New Item > Web Form > Click Add
  5. Drag and Drop a Button and a Label in Default.aspx page

    <asp:Button ID="Button1" runat="server" Text="Backup Database"
                onclick="Button1_Click" />
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
     
  6. Now add references.
    • Website > Add Reference > Browse
    • Go to the Assemblies folder of SQL Server
    • For SQL Server 2008 in Windows 7:
      C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies
    • Select “Microsoft.SqlServer.ConnectionInfo.dll” file and Click Ok
    • Add references for following dll files also
      Microsoft.SqlServer.Management.Sdk.Sfc.dll
      Microsoft.SqlServer.Smo.dll
      Microsoft.SqlServer.SmoExtended.dll
       
  7. Include following namespaces in your code behind file

    C#
     
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Common;
     
    VB.NET
     
    Imports Microsoft.SqlServer.Management.Smo
    Imports Microsoft.SqlServer.Management.Common

     
  8. Now Write code below in button Click event

    C#
     
    protected void Button1_Click(object sender, EventArgs e) {
        Backup backup = new Backup();
        backup.Action = BackupActionType.Database;
        backup.BackupSetName = "Database Backup";
        backup.BackupSetDescription = "Full database backup";
        backup.Database = "master";
        backup.Initialize = true;
        backup.ContinueAfterError = true;
        backup.LogTruncation = BackupTruncateLogType.Truncate;
     
        string name = "D:\\Backup\\master.Bak";
        BackupDeviceItem device = new BackupDeviceItem(name, DeviceType.File);
        backup.Devices.Add(device);
     
        ServerConnection serConn = new ServerConnection("Your Server Instance");
        Server sqlServer = new Server(serConn);
        backup.SqlBackup(sqlServer);
     
        backup.Devices.Remove(device);
        Label1.Text = "Database Backup is complete";
    }
     
    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim backup As New Backup()
        backup.Action = BackupActionType.Database
        backup.BackupSetName = "Database Backup"
        backup.BackupSetDescription = "Full database backup"
        backup.Database = "master"
        backup.Initialize = True
        backup.ContinueAfterError = True
        backup.LogTruncation = BackupTruncateLogType.Truncate
     
        Dim name As String = "D:\Backup\master.Bak"
        Dim device As New BackupDeviceItem(name, DeviceType.File)
        backup.Devices.Add(device)
     
        Dim serConn As New ServerConnection("Your Server Instance")
        Dim sqlServer As New Server(serConn)
        backup.SqlBackup(sqlServer)
     
        backup.Devices.Remove(device)
        Label1.Text = "Database Backup is complete"
    End Sub
     
    Create an instance of “Backup” class, provide BackupActionType as “Database” and then provide the database name. Create an instance of BackupDeviceItem and provide name and type in constructor. Add this device to Backup. Now make connection to the server and pass this connection to Server class constructor. At the end call SqlBackup() method by passing Server instance as parameter. 
     
  9. Press Ctrl+F5 and see result in browser. Click on Button to Backup Database

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use Wizard Control in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net