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

发布于 2024-12-08 22:06:30 字数 2114 浏览 0 评论 0原文

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

任何人都可以帮助这些人,

非常感谢..

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 = "";

which value i have to give in this text box "txtfilename.txt"

and what i have to give in this values @"-u{0} -p{1} -h{2} {3}", user, passwd1, Data_Source, dbname

I have found the mysqldump.exe file in this location

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

and this is my connectionstring

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

I am not sure which values i have to give in these places user, passwd1, Data_Source, dbname

would any one pls help on this guys

Many thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光瘦了 2024-12-15 22:06:30

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

没有连接字符串。

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

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.

There is no connection string.

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;  
}
撩发小公举 2024-12-15 22:06:30

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

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

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

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

You can just do a mysqldump if you want to backup your database:

I don't use Windows but this should more or less do the trick: Navigate to where your mysqldump is using the command prompt and execute this command:

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

When prompted for the password, enter your password.

微凉 2024-12-15 22:06:30

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

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

I am not sure that I understand your question, but:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文