【问题标题】:how to backup the database using mysql and c#.net如何使用 mysql 和 c#.net 备份数据库
【发布时间】:2011-12-02 22:18:33
【问题描述】:

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

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文件

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

这是我的连接字符串

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

我不确定在这些地方我必须给出哪些值user, passwd1, Data_Source, dbname

有没有人能帮帮忙

非常感谢..

【问题讨论】:

标签: c# .net mysql winforms backup


【解决方案1】:

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

没有连接字符串。

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

一个示例,将转储您指定的数据库中的所有内容(数据、结构、触发器、批次,并在您再次导入时覆盖任何表,使用命令行参数取决于你想要什么)。

    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;  
}

【讨论】:

  • 我能不能把mysqldump.exe路径代替mysql.exe
  • 使用 MySQL 备份工具的先决条件是您在要运行它的机器上安装了 MySQL。在您的机器上安装 MySQL,然后输入路径,或使用适用于您将运行此工具的机器上的路径(但请记住,该工具不会在您的机器上运行)。请记住,Windows 搜索工具并不总是在 Program Files 中搜索,MySQL 通常安装在我原始答案中提到的位置。
  • @errorcode105 在stackoverflow.com/questions/7686945/… 中重新提出了这个问题(带有答案,使用上面的代码),这个答案中使用了相同的代码。
【解决方案2】:

如果你想备份你的数据库,你可以只做一个 mysqldump:

我不使用 Windows,但这或多或少应该可以解决问题:使用命令提示符导航到 mysqldump 所在的位置并执行以下命令:

mysqldump -u root -p --databases [我的数据库名称] > file.sql

当提示输入密码时,输入您的密码。

【讨论】:

    【解决方案3】:

    我不确定我是否理解您的问题,但是:

    user: the user name for the account you are using to connect to mysql
    passwd1: the associated password
    Data_Source: the host name of the system that the mysql server is running on
    dbname: the name of the database schema you are trying to back up
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      相关资源
      最近更新 更多