cmd执行mysql备份数据_C#程序调用cmd执行命令-MySql备份还原

本文介绍如何使用C#程序调用cmd命令行来执行MySQL数据库的备份和还原操作。通过指定不同的mysqldump和mysql命令参数,实现对数据库的快速备份和完整还原。
摘要由CSDN通过智能技术生成

//备份还原mysql

public static voidTestOne()

{

Process p= newProcess();

p.StartInfo.FileName= "cmd.exe";

p.StartInfo.UseShellExecute= false;

p.StartInfo.RedirectStandardInput= true;

p.StartInfo.RedirectStandardOutput= true;

p.StartInfo.RedirectStandardError= true;

p.StartInfo.CreateNoWindow= true;//指定MySql程序的bin 目录

p.StartInfo.WorkingDirectory = @"E:\mysql-5.6.26-winx64\bin";

p.Start();/*************

* 备份命令

**************/

//简单格式//string strSql = "mysqldump --quick --host=localhost -u root -p123 test > d:\\test_20151227110010.sql";

/*命令指定格式*/

//String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址";//string strSql = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose --force --port=3306 --user=root --password=123 test -r d:\\one.sql";

/*************

* 还原命令

**************/

//简单格式//string strSql = "mysql --host=localhost -u root -p123 test < d:\\test_20151227110010.sql";

/**还原命令格式**/

//string s = "mysql --host=localhost --default-character-set=gbk --port=端口号 --user=用户名 --password=密码 数据库名

string strSql = "mysql --host=localhost --port=3306 --user=root --password=123 test < d:\\test_20151227110010.sql";

p.StandardInput.WriteLine(strSql+ "&exit");

p.StandardInput.AutoFlush= true;/****执行命令,没有返回可验证的结果*****/

//显示方式1//StreamReader reader = p.StandardOutput;//string line = reader.ReadLine();//while (!reader.EndOfStream)//{//Console.WriteLine(line);//line = reader.ReadLine();//}//显示方式2

string result =p.StandardOutput.ReadToEnd();

Console.WriteLine(result);//返回警告结果 --Warning: Using a password on the command line interface can be insecure.

string result2 =p.StandardError.ReadToEnd();

Console.WriteLine(result2);//显示方式3

ShowValue(p);

p.WaitForExit();

p.Close();

}private static async voidShowValue(Process p)

{string result = awaitp.StandardOutput.ReadToEndAsync();

Console.WriteLine(result);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值