如何使用mysql和c#.net备份数据库

时间:2011-10-07 10:52:42

标签: c# .net mysql winforms backup

我找到了使用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

任何人都会帮助这些家伙

非常感谢..

3 个答案:

答案 0 :(得分:3)

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

没有连接字符串。

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

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

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

答案 1 :(得分:1)

如果要备份数据库,可以执行mysqldump:

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

  

mysqldump -u root -p --databases [my db name]> file.sql

提示输入密码时,请输入密码。

答案 2 :(得分:0)

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

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 the system that the mysql server is running on
dbname: the name of the database schema you are trying to back up