UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Backup My SQL from Asp.net

Example code for how to take my sql backup from asp.net c#

by Athil


Posted on 03 Apr 2017 Category: Asp.net Views: 587

Edited on 10 Aug 2017



Here I am going to show how to take My SQL backup from Asp.net. 

For that you need to install MySqlBackup.NET from Package manager Console. To Install through package manager console , Open Package manager console and type the following command.

PM> Install-Package MySqlBackup.NET

Or download dll file from https://mysqlbackupnet.codeplex.com

To backup SQL file write the following code, and before running make sure that the host name in the connection string have the full permission on database.

Code

 protected void btnBakup_Click(object sender, EventArgs e)
   {
     try
     {              
     MemoryStream ms = new MemoryStream();
     using (MySqlConnection conn = new MySqlConnection(BusinessService.ConnectionString))
     {
        MySqlCommand cmd = new MySqlCommand();
        MySqlBackup mb = new MySqlBackup(cmd);
        cmd.Connection = conn;
        conn.Open();
               mb.ExportToMemoryStream(ms);
      }
           Response.ContentType = "text/plain";
           Response.AppendHeader("Content-Disposition", "attachment; filename=BackupMyPrj"+DateTime.Now+".sql");
           Response.BinaryWrite(ms.ToArray());
           Response.End();
   }
   catch(Exception E)
   {
            ScriptManager.RegisterStartupScript(this,typeof(Page),"a","<script>alert('"+E.Message.ToString().Replace("'","")+"');</script>",false);
   }

}

 



Leave a Comment:


Advertisement