protected void btExport_Click(object sender, EventArgs e) { try { string output = ""; using (MySqlConnection conn = new MySqlConnection(txtConnString.Text)) { MySqlCommand cmd = new MySqlCommand(); MySqlBackup mb = new MySqlBackup(cmd); cmd.Connection = conn; conn.Open(); output = mb.ExportToString(); } if (output.Length > (1024 * 100)) { lbError.Text = "The length of the output file has exceeded 100KB maximum length limit.<br />Try to use a smaller size of MySQL database sample."; lbError.Visible = true; } else { StoreFile.StoreSqlText(output); Server.Transfer("~/Result.aspx", true); } } catch (Exception ex) { lbError.Text = ex.Message; lbError.Visible = true; } }
void Step2() { MessageBox.Show("Running Step 2..."); using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { conn.Open(); cmd.Connection = conn; cmd.CommandText = string.Format("use `{0}`", txtDatabaseName.Text); cmd.ExecuteNonQuery(); mb.ExportInfo.RecordDumpTime = false; txtStep2.Text = mb.ExportToString(); conn.Close(); } } } byte[] ba = System.Text.Encoding.UTF8.GetBytes(txtStep2.Text); SHA1Managed sha = new SHA1Managed(); ba = sha.ComputeHash(ba); string shastr = BitConverter.ToString(ba).Replace("-", string.Empty); txtSHA1.Text = shastr; }
private void btExport_Click(object sender, EventArgs e) { textBox1.Clear(); textBox1.Refresh(); this.Refresh(); try { using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = conn; conn.Open(); textBox1.Text = mb.ExportToString(); conn.Close(); } } } MessageBox.Show("Export completed."); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btBackup_Click(object sender, EventArgs e) { string sql = ""; MySqlBackup mb = null; try { using (MySqlConnection conn = new MySqlConnection(txtConStr.Text)) { using (MySqlCommand cmd = new MySqlCommand()) { mb = new MySqlBackup(cmd); conn.Open(); cmd.Connection = conn; sql = mb.ExportToString(); conn.Close(); } } txtSql.Text = sql; MessageBox.Show("Done"); } catch (Exception ex) { var er = mb.LastError; MessageBox.Show(ex.ToString() + "\r\n\r\n" + er.ToString()); } }
/// <summary> /// Returns the exportet database as a string /// </summary> public string GetBackupAsString() { this.EnsureOpenConnection(); // Creates the mysql command using (MySqlCommand cmd = new MySqlCommand(string.Empty, this.connection)) // Creates the backup con using (MySqlBackup mb = new MySqlBackup(cmd)) return(mb.ExportToString()); }
private string GetSchema(MySqlConnection connection, string db) { TestContext.Progress.WriteLine($"Чтение схемы {db}..."); connection.ChangeDatabase(db); using MySqlBackup mb = new MySqlBackup(connection.CreateCommand()); mb.ExportInfo.ExportRows = false; mb.ExportInfo.RecordDumpTime = false; mb.ExportInfo.ResetAutoIncrement = true; var result = mb.ExportToString(); return(result.Replace(" ", " ")); //Особенности MySqlBackup в месте где удаляется AutoIncrement образуется двойной пробел. }
public string BackupTable(string tableName) { _mySqlCommand.Connection = _mySqlConnection; _mySqlConnection.Open(); _mySqlBackup.ExportInfo.ExcludeTables = new List <string> { tableName }; _mySqlBackup.ExportInfo.ExportTableStructure = true; _mySqlBackup.ExportInfo.ExportRows = true; var result = _mySqlBackup.ExportToString(); return(result); }
private static string GetDatabaseSqlDump(string planName, string connectionString) { var sql = string.Empty; using (var connection = new MySqlConnection(connectionString)) { using (var command = new MySqlCommand()) { using (var backup = new MySqlBackup(command)) { command.Connection = connection; connection.Open(); sql = backup.ExportToString(); connection.Close(); } } } return(sql); }
private void btExport_Click(object sender, EventArgs e) { using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { conn.Open(); cmd.Connection = conn; txtError.Text = mb.ExportToString(); conn.Close(); } } } }
private void btTest_Click(object sender, EventArgs e) { try { OpenFileDialog of = new OpenFileDialog(); if (of.ShowDialog() != DialogResult.OK) { lbResult.Text = string.Empty; return; } // Step 1: Compute the hash value of the bytes of the file byte[] ba1 = File.ReadAllBytes(of.FileName); string hash1 = BitConverter.ToString(System.Security.Cryptography.SHA1Managed.Create().ComputeHash(ba1)).Replace("-", string.Empty).ToLower(); lbHash1.Text = hash1; lbHash2.Text = string.Empty; lbResult.Text = string.Empty; lbDumpSize.Text = string.Empty; this.Refresh(); // Step 2: Insert blob into database using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { conn.Open(); cmd.Connection = conn; cmd.CommandText = "delete from testblob"; cmd.ExecuteNonQuery(); cmd.CommandText = "insert into testblob(blobdata)values(@blobdata);"; cmd.Parameters.AddWithValue("@blobdata", ba1); cmd.ExecuteNonQuery(); conn.Close(); } } if (MessageBox.Show("File inserted into database. Do you want to continue?", "Next", MessageBoxButtons.YesNo) != DialogResult.Yes) { lbResult.Text = "Operation Cancelled"; return; } // Step 3: Export the database string sqlDump = string.Empty; using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = conn; conn.Open(); mb.ExportInfo.AddDropTable = true; mb.ExportInfo.ExportTableStructure = true; mb.ExportInfo.ExportRows = true; if (dropBlobExportMode.SelectedIndex < 1) { mb.ExportInfo.BlobExportMode = BlobDataExportMode.HexString; } else { mb.ExportInfo.BlobExportMode = BlobDataExportMode.BinaryChar; mb.ExportInfo.BlobExportModeForBinaryStringAllow = true; } sqlDump = mb.ExportToString(); conn.Close(); } } } lbDumpSize.Text = sqlDump.Length.ToString(); this.Refresh(); // Step 4: Reimport the database using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = conn; conn.Open(); mb.ImportFromString(sqlDump); conn.Close(); } } } // Step 5: Get the blob data from database byte[] ba2 = null; using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { cmd.Connection = conn; conn.Open(); cmd.CommandText = "select blobdata from testblob;"; ba2 = (byte[])cmd.ExecuteScalar(); conn.Close(); } } // Step 6: Compute the hash value of the bytes from database string hash2 = BitConverter.ToString(System.Security.Cryptography.SHA1Managed.Create().ComputeHash(ba2)).Replace("-", string.Empty).ToLower(); lbHash2.Text = hash2; if (hash1 == hash2) { lbResult.Text = "SUCCESS"; lbResult.ForeColor = Color.DarkGreen; } else { lbResult.Text = "FAIL!"; lbResult.ForeColor = Color.Red; } this.Refresh(); if (MessageBox.Show("test completed. Do you want to write the output bytes to file?", "completed", MessageBoxButtons.YesNo) == DialogResult.Yes) { SaveFileDialog sf = new SaveFileDialog(); sf.FileName = of.FileName; if (sf.ShowDialog() == DialogResult.OK) { File.WriteAllBytes(sf.FileName, ba2); } } } catch (Exception ex) { lbResult.Text = ex.Message; lbResult.ForeColor = Color.Red; MessageBox.Show("Error: " + ex.ToString()); } }