need much better coding for my restore database.
I am trying to restore my MySQL (dump.sql) using C#
My code is
Code:
private void btnRestore_Click(object sender, EventArgs e)
{
lbLogs.Items.Add("You are about to restore backup database...");
string filename = "";
OpenFileDialog f2 = new OpenFileDialog();
f2.Title = "Open Database Backup File";
f2.Filter = "*." + "sql" + "|*." + "sql" + "|All Files|*.*";
f2.InitialDirectory = "C:\\";
if (DialogResult.OK != f2.ShowDialog())
return;
filename = f2.FileName;
FileInfo file = new FileInfo(filename);
string script = file.OpenText().ReadToEnd();
MySqlConnection conn = default(MySqlConnection);
conn = new MySqlConnection();
conn.ConnectionString = "Server=" + settings.ReadSetting("SERVER", "IP", 64, "localhost") +
";Port=" + settings.ReadSetting("SERVER", "Port", 32, "3306") +
";Database=;Uid=" + settings.ReadSetting("SERVER", "User", 64, "root") +
";Pwd=" + settings.ReadSetting("SERVER", "Password", 64, string.Empty) + ";";
conn.Open();
MySqlCommand cmd = new MySqlCommand(script, conn);
cmd.ExecuteNonQuery();
conn.Close();
try
{
lbLogs.Items.Add("Restore finished");
}
catch (Exception ex) // Log any error that occur during the backup process
{
string errorMessage = "Restore fail.\r\n\r\n" + ex.ToString();
MessageBox.Show(errorMessage, "Error");
}
}
That code is working aside from the thing that when my dump .sql file became bigger more than a 100MB backup that contains BLOB data, when I try to restore from my application my pc almost hung in state, restore process almost eat all the resources before restoring done.
Is there any good revision on my code to prevent that?
And here is the code for generating may backup dump of mysql.
Code:
{
ProcessStartInfo proc = new ProcessStartInfo();
string cmd = " --host=" + settings.ReadSetting("SERVER", "IP",64,"127.0.0.1") +
" --user=" + settings.ReadSetting("SERVER", "User", 64, "root") +
" --password=" + settings.ReadSetting("SERVER", "Password", 64, string.Empty) +
" --add-drop-database --hex-blob --databases db_barangay";
proc.FileName = Application.StartupPath + "\\mysqldump.exe";
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = true;
proc.UseShellExecute = false;
proc.CreateNoWindow = false;
proc.Arguments = cmd;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
SW.Write(p.StandardOutput.ReadToEnd());
p.WaitForExit();
lbLogs.Items.Add("Backuping data finished....");
p.Close();
SW.Close();
StreamDB.Close();
}