且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-12 22:56:23

首先,您应该使用的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;  
}