//This checks both styles of connections and return false when the second(older) method ALSO fails //maybe should've done this with 2 separate methods but thought against it as those 2 new functions //would increase overhead and wouldn't be needed again. public static bool TestConnection(BackupItem itm) { //Begin NewStyle check MysqlAlias.MySqlConnection conn = null; try { conn = new MysqlAlias.MySqlConnection(itm.ConnectionString()); conn.Open(); } catch (MysqlAlias.MySqlException) { //Begin OldStyle Check OldMysqlAlias.MySqlConnection old_conn = null; try { old_conn = new OldMysqlAlias.MySqlConnection(itm.ConnectionString()); old_conn.Open(); } catch (MysqlAlias.MySqlException) { return false; }catch(Exception) { return false; } finally { if (old_conn != null) { old_conn.Close(); } } //End OldStyle Check } finally { if (conn != null) { conn.Close(); } } //End NewStyle check return true; }
bool backupDatabaseOldStyle(BackupItem bk_itm) { //This uses the version 2.0 of mysql dll so that servers that still use old style password authentication (pre 4.1) will //still be able to connect. string result_folder = bk_itm.SaveTo + "\\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString("D2") + "-" + DateTime.Now.Day.ToString("D2") + "\\" + bk_itm.Database + "\\"; OldMysqlAlias.MySqlConnection conn = null; OldMysqlAlias.MySqlDataReader rdr = null; try { conn = new OldMysqlAlias.MySqlConnection(bk_itm.ConnectionString()); conn.Open(); //get every table in this db string stm = "SHOW TABLES"; OldMysqlAlias.MySqlCommand cmd = new OldMysqlAlias.MySqlCommand(stm, conn); rdr = cmd.ExecuteReader(); //deal with directory that .sql files should be saved in if (!Directory.Exists(result_folder)) { Directory.CreateDirectory(result_folder); } string current_table = ""; while (rdr.Read()) { //Console.WriteLine(rdr.GetInt32(0) + ": " + rdr.GetString(1)); current_table = rdr.GetString(0); // Use ProcessStartInfo class ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\mysqldump.exe"; startInfo.WindowStyle = ProcessWindowStyle.Hidden; //::@C:\Users\User\Downloads\MySQLWinBackup\MySQLWinBackup\mysqldump.exe startInfo.Arguments = "--host=\"" + bk_itm.Host + "\" --port=\"" + bk_itm.Port + "\" --user=\"" + bk_itm.Username + "\" --password=\"" + bk_itm.PasswordValue + "\" -Q --result-file=\"" + result_folder + "\\" + current_table + ".sql\" \"" + bk_itm.Database + "\" \"" + current_table + "\""; try { // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); txtOutput.Text += "Database: " + bk_itm.Database + " - table: " + current_table + " done " + Environment.NewLine; } } catch (Exception ex) { ErrorLog(ex.Message + " Using Old-Style Password Authentication"); return false; } } } catch (MysqlAlias.MySqlException ex) { ErrorLog(ex.Message + " Using Old-Style Password Authentication"); return false; } finally { if (conn != null) { conn.Close(); } } return true; }