UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Take My Sql Backup - Asp.Net

Instructions fro How to take MySql Batabase Backup From Asp.net -C#

by Athil


Posted on 27 Jul 2018 Category: Asp.net Views: 1458


Here I am going to show how to take MySql backup from C# and download backup.

1. use the name space and include the reference for mysql in your project 

    1. Mysql.Data.dll

    2. MySqlBackup.dll
   

using MySql.Data.MySqlClient;

2. Add connection string to your database  

 

public static string ConnectionString = "server=20*.**.**.***;uid=loginid;pwd=DBPwd;database=yourDBname";

C# code for backup

 public void dbBackup()
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                string connectionstring = ConnectionString;
                connectionstring += "charset=utf8;convertzerodatetime=true;";

                using (MySqlConnection conn = new MySqlConnection(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=backupfilename" + DateTime.Now.ToString("ddMMMyyyy") + ".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:


Click here to register

Popular articles