Skip to content

Conditional Rows Export for Each Table

Adrian Voo edited this page Sep 16, 2021 · 2 revisions

Sample codes:

void Export()
{
    var dic = new Dictionary<string,string>();

    dic["tableA"] = "SELECT * FROM `tableA`;";
    dic["tableB"] = "SELECT * FROM `tableB` WHERE 1 = 2;";
    dic["tableC"] = "SELECT * FROM `tableC` WHERE membershipid = 1;";
    dic["tableD"] = "SELECT id,name,address,tel FROM `tableD`;";
    dic["tableE"] = "SELECT * FROM `tableE` WHERE dateregister >= '2014-01-01 00:00:00' and dateregister <= '2014-12-31 23:59:59';";

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

                mb.ExportInfo.TablesToBeExportedDic = dic;
                mb.ExportToFile("C:\\backup.sql");
            }
        }
    }
}

Explain:

  1. tableA - All rows will be exported.
  2. tableB - No rows will be exported.
  3. tableC - Only rows with membershipid = 1 will be exported.
  4. tableD - Only 4 columns (id, name, address, tel) of data will be exported for all rows.
  5. tableE - Only export rows which the dateregister is within specific date range.

Case Study 1: Export Table's Structure Only to Individual File

https://github.com/MySqlBackupNET/MySqlBackup.Net/issues/79