Skip to content
adriancs edited this page Apr 4, 2025 · 25 revisions

MySqlBackup.NET Guide

Last Updated: March 12, 2025

Welcome to the MySqlBackup.NET guide! This document provides a clear and elegant introduction to using the MySQL Backup library with the MySQL .NET Connector. Whether you're exporting or importing database content, this guide will walk you through the essentials and advanced customization options.


Prerequisites

This guide assumes you have a foundational understanding of the MySqlConnector.NET (MIT) or MySQL .NET Connector (MySql.Data.dll) by Oracle, and are comfortable performing basic SQL operations such as SELECT, INSERT, UPDATE, and DELETE.

To begin coding, include the following using statement at the top of your file:

// if you are using MySqlConnector.NET (MIT)
using MySqlConnector;

// if you are using Connector/NET by Oracle
using MySql.Data.MySqlClient;

// if you are using dotConnect.Express.for.MySQL by Devart
using Devart.Data.MySql;

Getting Started

Simple Database Export

Exporting a MySQL database to a file is straightforward. Below is an elegant example demonstrating the process:

string connectionString = "server=localhost;user=root;pwd=qwerty;database=test;";
string filePath = @"C:\backup.sql";

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

This code connects to the test database and exports its contents to C:\backup.sql with default settings.

Simple Database Import

Importing a database from a file is just as simple. Here’s an example:

string connectionString = "server=localhost;user=root;pwd=qwerty;database=test;";
string filePath = @"C:\backup.sql";

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

This imports the contents of C:\backup.sql into the test database using default options.


Customizing Export and Import Behavior

For more control over the export and import processes, you can modify options via:

  • ExportInfo: Customize export behavior.
  • ImportInfo: Customize import behavior.

Example: Exporting Table Structures Only

Suppose you want to export only the table structures without row data. Here’s how:

string connectionString = "server=localhost;user=root;pwd=1234;database=test1;";
string filePath = @"Y:\backup.sql";

using (MySqlConnection conn = new MySqlConnection(connectionString))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ExportInfo.ExportTableStructure = true;
            mb.ExportInfo.ExportRows = false;
            mb.ExportToFile(filePath);
            conn.Close();
        }
    }
}

This exports only the schema of test1 to Y:\backup.sql.


ExportInfo Options

Below is a comprehensive list of ExportInfo properties to tailor the export process. Each option includes its default value and purpose.

Property Type Default Value Description
AddCreateDatabase bool false Adds a CREATE DATABASE statement to the dump file.
AddDropDatabase bool false Adds a DROP DATABASE statement to the dump file.
AddDropTable bool true Adds DROP TABLE statements before CREATE TABLE statements.
BlobExportMode BlobDataExportMode HexString Defines how BLOB data is exported (HexString or BinaryChar).
EnableComment bool true Includes comments in the dump file.
ExcludeTables List<string> Empty list Specifies tables to exclude from export (blacklist).
ExportFunctions bool true Exports stored functions.
ExportProcedures bool true Exports stored procedures.
ExportRows bool true Exports table row data.
ExportTableStructure bool true Exports table structures (CREATE TABLE statements).
ExportTriggers bool true Exports triggers.
ExportViews bool true Exports views.
IntervalForProgressReport int 100 Time interval (ms) for progress update events.
MaxSqlLength int 5 * 1024 * 1024 (5 MB) Maximum length for combining multiple INSERT statements.
RecordDumpTime bool true Records the dump time in the file.
ResetAutoIncrement bool false Resets auto-increment values to 1.
RowsExportMode RowsDataExportMode INSERT Defines row export behavior (e.g., INSERT, REPLACE, ONDUPLICATEKEYUPDATE).
TextEncoding System.Text.Encoding UTF8Encoding(false) Sets the encoding for the dump file.
WrapWithinTransaction bool false Wraps row exports in a transaction.
AdjustColumnValue N/A Added in v2.3.9 (Mar 12, 2025) Customizes column values (see dedicated wiki page).

Note: For detailed usage of AdjustColumnValue, refer to the Adjust Column Value Wiki.


ImportInfo Options

Here are the key ImportInfo properties for customizing imports:

Property Type Default Value Description
IgnoreSqlError bool false Ignores SQL errors during import if set to true.
IntervalForProgressReport int 100 Time interval (ms) for progress update events.
ErrorLogFile string string.Empty File path for logging errors during import.

Conclusion

With this guide, you can effortlessly export and import MySQL databases using the MySqlBackup.NET library. Customize your workflows with ExportInfo and ImportInfo to suit your specific needs. For further details or advanced topics, explore the official GitHub repository.

Happy coding!