1.简单实例

//备份还原mysql
public static void TestOne()
{
Process p = new Process();
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 void ShowValue(Process p)
{
string result = await p.StandardOutput.ReadToEndAsync();
Console.WriteLine(result);
}

C#程序调用cmd执行命令-MySql备份还原的更多相关文章

  1. c# 程序调用cmd执行命令如SVN.exe

    c# 程序调用cmd执行命令如SVN.exe string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Di ...

  2. C#程序调用CMD执行命令,将参数传递给cmd.exe

    proc.StartInfo.Arguments = "/c ping 10.2.2.125"; C#程序调用CMD执行命令 将参数传递给cmd.exe的(Passing an a ...

  3. C#程序调用cmd执行命令

    对于C#通过程序来调用cmd命令的操作,网上有很多类似的文章,但很多都不行,竟是漫天的拷贝.我自己测试整理了一下. 代码: string str = Console.ReadLine(); Syste ...

  4. C#程序调用cmd执行命令(转)

    C#通过程序来调用cmd命令的操作 string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Diagnos ...

  5. C# 调用CMD执行命令行

    这几天用c#做了一个项目,其中一个功能是要把生成的临时文件隐藏,同时,不能在屏幕上有调用CMD的痕迹,这里生成的临时文件的绝对路径为delfile为文件的绝对路径, 代码如下: private voi ...

  6. C# 调用cmd执行命令

    private void CmdRun_Click(object sender, EventArgs e)        {            Process p = new Process(); ...

  7. linux 程序调用system执行命令

    正确使用system方法,判断返回值 int exeCmd(const char *cmd) { pid_t status; status = system(cmd); == status) { Wr ...

  8. java调用cmd执行maven命令

    一.原理介绍 Java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后封闭命令窗口. cmd /k di ...

  9. (4.8)mysql备份还原——binlog查看工具之show binlog的使用

    (4.8)mysql备份还原——binlog查看工具之mysqlbinlog及show binlog的使用 关键词:show binlog,mysql binlog查看,二进制文件查看,binlog查 ...

随机推荐

  1. osg三维重建的两种方法剖析:三角面片(osgUtil::DelaunayTriangulator)和四角面片(osg::HeightField) (2)

    // perform very basic sanity-check validation on a heightfield.bool validateHeightField(osg::HeightF ...

  2. 多线程08-Callable和Future

    1.简介 Callable是一个接口,与Runnable类似,包含一个必须实现的call方法,可以启动为让另一个线程来执行,执行Callable可以得到一个Future对象 该对象可以监听Callab ...

  3. storm-kafka教程

    一.原理介绍   本文内容参考:https://github.com/apache/storm/tree/master/external/storm-kafka#brokerhosts (一)使用st ...

  4. 使用padding-top实现自适应背景图片

    在父级容器中设定最大的宽度,由于背景图片会出现塌陷的情况,有宽度无高度, 则,在图片容器中添加以下属性 padding-top:%(计算方式:图片的高度/图片的宽度*100%) background- ...

  5. 用Set中元素做条件查询

    一个老师教许多学生,一个学生被许多老师教,一个学生有好多书,同一种书被许多同学拥有.查询教拥有书"a"的学生的老师:   class teacher{   String id;   ...

  6. 推荐IOS开发3个工具:Homebrew、TestFight、Crashlytics-备

    1. Homebrew 什么是Homebrew? Homebrew is the easiest and most flexible way to install the UNIX tools App ...

  7. C#的Reflection总结

    什么是反射 在.NET中的反射也可以实现从对象的外部来了解对象(或程序集)内部结构的功能,哪怕你不知道这个对象(或程序集)是个什么东西,另外.NET中的反射还可以运态创建出对象并执行它其中的方法. 反 ...

  8. 使用john破解ubuntu(linux)9.10密码

    Title:使用john破解ubuntu(linux)9.10密码 --2011-11-23 15:00 ubuntu 9.10的账户密码加密方式改用sha512了,默认的john是破不了的,还好官方 ...

  9. PHP 中的静态变量的简单使用

    静态变量的初始化只能在第一次static 声明的时候进行,这些静态变量只能在声明他的函数中访问到. 例如: <?php function do_something(){ static $firs ...

  10. 理解 break, continue, return 和 exit

    你们知道 “break”, “continue”, “return” 和 “exit”的作用吗? 它们是功能强大的语言结构体.下面通过一个测试函数来说明它们之间的不同. 1 2 3 4 5 6 7 8 ...