博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#备份MySQL数据库 --转载
阅读量:7021 次
发布时间:2019-06-28

本文共 1463 字,大约阅读时间需要 4 分钟。

namespace
 BackupMySQL
{
    
class Program
    
{
        
public static void DoBackup()
        
{
            
string[] ary = ReadFromText();
            
string host = ary[0];
            
string port=ary[1];
            
string user = ary[2];
            
string password = ary[3];
            
string database = ary[4];
            
string fileName = database + "_bak_" + DateTime.Now.ToString("yyyyMMddhhmmss");
            
string bakPath = ary[5+ "\\" + fileName + ".sql";
            
string logPath = ary[6];
            
string cmdStr = "/c mysqldump -h" + host + " -P" + port + " -u" + user + " -p" + password + " " + database + " > " + bakPath;
            
            
try
            
{
                System.Diagnostics.Process.Start(
"cmd", cmdStr);
            }
            
catch (Exception ex)
            
{
                WriteLog(logPath, ex.Message);
            }
            
        }
        
/// <summary>
        
/// 读配置
        
/// </summary>
        
/// <returns></returns>
        public static string[] ReadFromText()
        
{
            
string FileName = @"d:\BackupIni.txt";
            ArrayList list 
= new ArrayList();
            
if (File.Exists(FileName))
            
{
                StreamReader sr 
= new StreamReader(FileName, Encoding.Default);
                
string s = "";
                
while ((s = sr.ReadLine()) != null)
                
{
                    list.Add(s);
                }
            }
            
return (string[])list.ToArray(typeof(string));
        }
        
/// <summary>
        
/// 写日志
        
/// </summary>
        
/// <param name="filePath"></param>
        
/// <param name="theStr"></param>
        public static void WriteLog(string filePath, string theStr)
        
{
            
if (!File.Exists(filePath))
            
{
                File.Create(filePath);
            }
            StreamWriter sw 
= new StreamWriter(filePath, true, Encoding.Default);
            sw.WriteLine(theStr);
            sw.Flush();
            sw.Close();
        }
        
static void Main(string[] args)
        
{
            DoBackup();
        }
    }
}

转载于:https://www.cnblogs.com/maliya/archive/2012/06/21/2557927.html

你可能感兴趣的文章
Java NIO系列教程(一) Java NIO 概述
查看>>
JQuery判断元素是否存在
查看>>
周鸿祎:很多程序员一看就知道不会创业
查看>>
Web前端测试
查看>>
Parcelable encountered IOException writing seriali
查看>>
可靠消息服务实现(分布式事务)
查看>>
“什么编程课?不重要!等以后再学吧!”
查看>>
Dao层系列-5-Hibernate JPA
查看>>
iOS 开发小技巧 (持续更新)
查看>>
安装redis
查看>>
CFileDialog用法
查看>>
基于代理和注解机制,实现消息重发(APP,SMS)效果
查看>>
你和高级工程师的差距在哪里?
查看>>
使用maven构建android项目
查看>>
自然语言处理-分词工具
查看>>
Linux配置运行环境
查看>>
敏捷开发的特点
查看>>
zipfile
查看>>
线程中断 interrupt() 与 线程终止方法
查看>>
ubuntu下为Intellij IDEA 添加启动器
查看>>