如何使用mysql和c#.net备份数据库 [英] how to backup the database using mysql and c#.net

查看:181
本文介绍了如何使用mysql和c#.net备份数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了使用c#.net备份数据库(mysql)的解决方案

I have found this solution for back up my database (mysql) using c#.net

string fname = txtFileName.Text;


if (fname == "")
{

 MessageBox.Show("Please Enter the File Name!");return;
}

try
{

      btnBackup.Enabled = false;
      DateTime backupTime = DateTime.Now;

      int year = backupTime.Year;
      int month = backupTime.Month;

      int day = backupTime.Day;
     int hour = backupTime.Hour;

      int minute = backupTime.Minute;
     int second = backupTime.Second;

     int ms = backupTime.Millisecond;
    String tmestr = backupTime.ToString();

 // C:\Program Files\MySQL\MySQL Server 5.0\bin

   //tmestr = "C:\\" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + ".bak";

    tmestr = "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\" + fname + year + "-" + month + "-" + day + "-" + hour;// +".sql";

   StreamWriter file = new StreamWriter(tmestr);
  ProcessStartInfo proc = new ProcessStartInfo();

    string cmd = string.Format(@"-u{0} -p{1} -h{2} {3}", user, passwd1, Data_Source, dbname);
     proc.FileName = "mysqldump";

   proc.RedirectStandardInput = false;
   proc.RedirectStandardOutput = true;

   proc.Arguments = cmd;//"-u root -p smartdb > testdb.sql";

   proc.UseShellExecute = false;
   Process p = Process.Start(proc);

   string res;
   res = p.StandardOutput.ReadToEnd();

  file.WriteLine(res);

   p.WaitForExit();

  file.Close();

  MessageBox.Show("DataBase Backup Has Been Completed Successfully!");btnBackup.Enabled = true;
}

catch (IOException ex)
{

  MessageBox.Show("Disk full or other IO error , unable to backup!");
}

txtFileName.Text = "";

我必须在此文本框"txtfilename.txt"

以及我必须在此值@"-u{0} -p{1} -h{2} {3}", user, passwd1, Data_Source, dbname

我在此位置找到了mysqldump.exe文件

I have found the mysqldump.exe file in this location

string location = "C:\\Program Files\\MySQL\\MySQL WorkBench 5.2CE\\";

这是我的连接字符串

string connestring = "server=localhost;user=root;database=access";

我不确定在这些地方user, passwd1, Data_Source, dbname

任何人都可以帮助这个家伙

would any one pls help on this guys

非常感谢..

推荐答案

首先,您应该使用的mysqldump.exe的位置与mysql本身在同一目录中(C:\ Program Files \ MySQL \ MySQL Server 5.5 \ bin),请使用该副本,不要使用其他副本.

First off, the location of mysqldump.exe that you should be using is within the same directory as mysql itself (C:\Program Files\MySQL\MySQL Server 5.5\bin for example), use that and no other copy.

没有连接字符串.

在父目录(即C:\ Program Files \ MySQL \ MySQL Server 5.5)中,您会找到配置文件my.ini,在[client]标题下,您可以设置连接设置(用户名/密码等) .如果愿意,可以在启动mysqldump进程时将登录信息指定为参数(

In the parent directory (ie C:\Program Files\MySQL\MySQL Server 5.5) you'll find the configuration file my.ini where under the [client] heading you can set the connection settings (username/password etc). Of if you prefer, you specify the login information as arguments when starting the mysqldump process (a list of arguments is provided by MySQL).

一个示例,将在您指定的数据库中转储所有内容(数据,结构,触发器,批次,并且在您再次将其重新导入时将覆盖所有表,请使用命令行参数,具体取决于根据您的需求).

An example, will dump everything in the databases you specify (data, structure, triggers, the lot, and will overwrite any tables when you then import it back again, use the command line arguments depending on what you want).

    public static void DumpStructure()
{
    Process sd = null;
    ProcessStartInfo r1 = new ProcessStartInfo(/* Full path to MySqlDump.exe */, "--databases exampleDatabase1 exampleDatabase2 --compress --routines --triggers --add-drop-database --add-drop-table --add-locks --extended-insert --password=YOURPASSWORD --port=8307 --user=YOURUSERNAME --disable-keys --quick --comments --complete-insert --result-file=DUMPEDOUTPUT.sql");

    r1.CreateNoWindow = true;
    r1.WorkingDirectory = /* WHERE MYSQL.EXE IS STORED */;
    r1.UseShellExecute = false;
    r1.WindowStyle = ProcessWindowStyle.Minimized;
    r1.RedirectStandardInput = false;

    sd = Process.Start(r1);
    sd.WaitForExit();

    if (!sd.HasExited) {
        sd.Close();
    }
    sd.Dispose();
    r1 = null;
    sd = null;  
}

这篇关于如何使用mysql和c#.net备份数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆