-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Backup only structure #79
Comments
Yes, you can set it at: mb.ExportInfo.TablesToBeExportedDic Here is some explanation: Example of code: string constr = "server=127.0.0.1;user=root;pwd=1234;database=dbtest;convertzerodatetime=true;";
using (MySqlConnection conn = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand())
{
conn.Open();
cmd.Connection = conn;
// assuming exporting all tables
DataTable dtTables = new DataTable();
cmd.CommandText = "show full tables where table_type='BASE TABLE';";
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dtTables);
using (MySqlBackup mb = new MySqlBackup(cmd))
{
foreach (DataRow drTable in dtTables.Rows)
{
string tablename = drTable[0] + "";
string filename = $@"C:\backup\{tablename}.sql";
mb.ExportInfo.RecordDumpTime = false;
mb.ExportInfo.ExportRows = false;
mb.ExportInfo.ResetAutoIncrement = true;
mb.ExportInfo.AddCreateDatabase = false;
mb.ExportInfo.GetTotalRowsMode = GetTotalRowsMethod.Skip;
mb.ExportInfo.SetDocumentHeaders(new List<string>());
mb.ExportInfo.SetDocumentFooters(new List<string>());
mb.ExportInfo.TablesToBeExportedDic.Clear();
mb.ExportInfo.TablesToBeExportedDic[tablename] = $"select * from `{tablename}` where 1=2;";
mb.ExportToFile(filename);
}
}
conn.Close();
}
} |
Thanks for quick and elaborate response. |
Here is an alternative method without using MySqlBackup.NET: by using the following sql query SHOW CREATE TABLE <table name> read more at: Sample code: string constr = "server=127.0.0.1;user=root;pwd=1234;database=testdb;convertzerodatetime=true;";
using (MySqlConnection conn = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand())
{
conn.Open();
cmd.Connection = conn;
DataTable dtTables = new DataTable();
cmd.CommandText = "show full tables where table_type='BASE TABLE';";
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dtTables);
foreach (DataRow drTable in dtTables.Rows)
{
string tablename = drTable[0] + "";
string filename = $@"C:\backup\{tablename}.sql";
DataTable dtTb = new DataTable();
cmd.CommandText = $"show create table `{tablename}`;";
da.SelectCommand = cmd;
da.Fill(dtTb);
string sqlCreateTable = dtTb.Rows[0]["Create Table"] + "";
File.WriteAllText(filename, sqlCreateTable);
}
conn.Close();
}
} |
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
Hello @adriancs2 ,
First of all, Thanks much for the wonderful solution.
Kindly suggest on the usage of parameters to dump only the structure of objects like tables, SP etc.
Alongside, is there a provision for script into multiple objects/individual files(one file per object).
Your response is much appreciated.
The text was updated successfully, but these errors were encountered: