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. CIPAddressCtrl控件

    CIPAddressCtrl m_ipCtrl; 1.获取控件IP值 int GetAddress(byte& byteFirst, byte& byteTwo, byte& ...

  2. Android 访问权限设置

    Android开发应用程序时,有时我们要用到很多权限, 今天我就收集了一些开发时可能用到的开启权限设置. 这些权限都是在 AndroidManifest.xml设置. 设置方法 <uses-pe ...

  3. php 基于phpexcel制作的导出excel类

    最近要写个项目,由于项目中导出excel太多,因此写个类便于使用. <?php /**如果是gbk的编码,请转为UTF8 *@使用方法. *引入类库. * $excel = news excel ...

  4. TFS 如何恢复到指定版本

    一. 签出文件,然后获得指定版本. 二. 覆盖本地文件. 三.选择合并冲突. 四. 签入恢复的文件. 五. 解决冲突,选择抛弃服务器版本.

  5. Core Data-备用

    Core Data是一个功能强大的层,位于SQLite数据库之上,它避免了SQL的复杂性,能让我们以更自然的方式与数据库进行交互.Core Data将数据库行转换为OC对象(托管对象)来实现,这样无需 ...

  6. android禁用光感按键

    用RE浏览器打开/system/usr/keylayout下的qwerty.kl文件, 找到key 158 BACK key 139 MENU key 102 HOME 分别在其之前加下#号,如下: ...

  7. 微信支付开发+{ping++}微信支付托管

    ------------------------微信支付接口------------------------------- 微信支付开发并没有想象中的那么难,主要是微信提供了sdk. 微信公众号必须是 ...

  8. NUnit+mock+moq单元测试

    [TestFixture] public class InstantBatchBuyTest { private string _mallAbc; private string _itemCode; ...

  9. CA

    http://www.cmca.net/index.php?option=com_content&view=article&id=55&Itemid=16

  10. leetcode-Consecutive numbers

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...