备份和恢复mysql数据库c#

发布于 2024-10-03 12:13:30 字数 115 浏览 10 评论 0原文

我正在寻找一种方法,如何通过 C# 从 mysql 备份一些数据库(文件备份)。 还可以通过 C# 将数据库从备份文件恢复到某个新位置。

您能帮我提供一些如何开始这里的想法吗?

谢谢。

I am looking for a way how I can via c# back up some database from mysql (file backup).
And also via c# restore database from backup file to some new location.

Can you help me with some ideas how to get started here .

Thanks.

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

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

发布评论

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

评论(4

清风挽心 2024-10-10 12:13:30
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
Process.Start(startInfo);
startInfo.Arguments = "mysqldump -u admin -p admin test > c:\backupfile.sql";
Process.Start(startInfo);

如果需要,您可以使用 startInfo.WindowStyle 隐藏 dos 提示符。

ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
Process.Start(startInfo);
startInfo.Arguments = "mysqldump -u admin -p admin test > c:\backupfile.sql";
Process.Start(startInfo);

You can hide the dos prompt with startInfo.WindowStyle if you need.

小鸟爱天空丶 2024-10-10 12:13:30

作为 MySqlDump 的替代方案,您可以尝试 MySqlBackup.NEThttps://github .com/MySqlBackupNET/MySqlBackup.Net

示例

备份/导出 MySQL 数据库

string constring = "server=localhost;user=root;pwd=qwerty;database=test;";

// Important Additional Connection Options
constring += "charset=utf8;convertzerodatetime=true;";

string file = "C:\\backup.sql";

using (MySqlConnection conn = new MySqlConnection(constring))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ExportToFile(file);
            conn.Close();
        }
    }
}

导入/恢复 MySQL 数据库

string constring = "server=localhost;user=root;pwd=qwerty;database=test;";

// Important Additional Connection Options
constring += "charset=utf8;convertzerodatetime=true;";

string file = "C:\\backup.sql";

using (MySqlConnection conn = new MySqlConnection(constring))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ImportFromFile(file);
            conn.Close();
        }
    }
}

As alternative to MySqlDump, you can try MySqlBackup.NET: https://github.com/MySqlBackupNET/MySqlBackup.Net

Example

Backup/Export a MySQL Database

string constring = "server=localhost;user=root;pwd=qwerty;database=test;";

// Important Additional Connection Options
constring += "charset=utf8;convertzerodatetime=true;";

string file = "C:\\backup.sql";

using (MySqlConnection conn = new MySqlConnection(constring))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ExportToFile(file);
            conn.Close();
        }
    }
}

Import/Restore a MySQL Database

string constring = "server=localhost;user=root;pwd=qwerty;database=test;";

// Important Additional Connection Options
constring += "charset=utf8;convertzerodatetime=true;";

string file = "C:\\backup.sql";

using (MySqlConnection conn = new MySqlConnection(constring))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ImportFromFile(file);
            conn.Close();
        }
    }
}
獨角戲 2024-10-10 12:13:30

您找到的 CodeProject 通过调用 mysqldump.exe 进行备份,并通过从 C# 程序中调用 mysql.exe 进行恢复(如 Marc B 推荐的那样)。

作为替代方案,此 CodeProject 实际上会自行生成 SQL 语句,而不是调用外部程序:

(它不像使用 mysqldump.exe / mysql.exe 那样快速或可靠,但您可以从中学到很多东西。)

The CodeProject you found does backups by calling mysqldump.exe and does restores by calling mysql.exe from within a C# program (as Marc B recommended).

As an alternative, this CodeProject actually generates the SQL statements itself instead of calling an external program:

(It's not as fast or reliable as using mysqldump.exe / mysql.exe, but you can learn a lot from it.)

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