Backup and attach MySQL database through C #

Source: Internet
Author: User

Recent backup and attachment capabilities for MYSQ databases through C #

Because MySQL has similar backup and additional CMD commands, but has not been used, today, under the practice, feel very efficient, more efficient than their own writing. Below I list the backup and additional function functions that C # calls MySQL.

1. Back up the MySQL database

Defines a string straddress = string. Format ("Mysqldump--host={0}--default-character-set=utf8--lock-tables--routines--force--port=3306--user={1}-- PASSWORD={2}--quick ", the server name of the connection, user name, password);

String strdb= the name of the database you need to back up;

This.mysqlpath = "C:\\Program Files\\mysql\\mysql Server 5.5\\bin";

if (!string. IsNullOrEmpty (STRDB))
{
SfD. Filter = "Database file |*.sql";
SfD. FilterIndex = 0;
SfD. Restoredirectory = true;
SfD. FileName = "backup-" + strDB + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". sql";
if (SFD. ShowDialog () = = DialogResult.OK)
{
String FilePath = sfd. FileName;
string cmd = this.straddress + strDB + ">" + filePath;
string result = Runcmd (M_mysqlpath, CMD);
if (result. Trim () = = "")
{
Show ("Database backup succeeded!") "," hint ",);
}
Else
{
Show (result, "hint");
}
}
}

Main operating functions

Private String Runcmd (String strpath, String strcmd)
        {
             process P = new process ();
            p.startinfo.filename = "cmd.exe";
            p.startinfo.workingdirectory = strpath;
            P.startinfo.useshellexecute = false;
            p.startinfo.redirectstandardinput = true;
            p.startinfo.redirectstandardoutput = true;
            p.startinfo.redirectstandarderror = true;
            P.startinfo.createnowindow = true;

P.start ();
P.standardinput.writeline (Strcmd);
P.standardinput.writeline ("Exit");
return p.StandardError.ReadToEnd ();
}

Perform a backup of the selected database.

Backup and attach MySQL database through C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.