// Methods. private void OpenConnection(ServerInfo server, string database = "") { // Create the connection string. string connectionString = "Server=" + server.Hostname + ";" + "Port=" + server.Port + ";" + "Uid=" + server.GetAdminUsername() + ";" + "Pwd=" + server.GetAdminPassword() + ";"; if (database != "") { connectionString = connectionString + "Database=" + database + ";"; } mysqlConnection = new MySqlConnection(connectionString); // Attempt to connect. try { mysqlConnection.Open(); } catch (MySqlException e) { if (e.Number == 0) { HttpContext.Current.Session["ErrorMessage"] = "SERVER_UNREACHABLE"; HttpContext.Current.Session["ErrorInfo"] = "Unable to connect to database server '" + server.Hostname + "'."; } else if (e.Number == 1045) { HttpContext.Current.Session["ErrorMessage"] = "INVALID_ADMIN_CREDENTIALS"; HttpContext.Current.Session["ErrorInfo"] = "Unable to login to '" + server.Hostname + "'.\n" + "This is an internal error and must be resolved by the system administrator."; } else { HttpContext.Current.Session["ErrorMessage"] = "MYSQL_ERROR_" + e.Number; HttpContext.Current.Session["ErrorInfo"] = "Connecting to the MySQL server returned the following error code.\n" + "Search MySQL documentation for the solution."; } throw e; } // Set default error message, in case we get disconnected. HttpContext.Current.Session["ErrorMessage"] = "SERVER_DISCONNECTED"; // Check that the server has been configured. if (server.NewToDbMgr != "0") { try { ConfigServer(server); } catch (Exception e) { CloseConnection(); throw e; } } }
// Methods. private void OpenConnection(ServerInfo server, string database = "") { // Create connection string. string connectionString = "Server=" + server.Hostname + "," + server.Port + ";" + "User Id=" + server.GetAdminUsername() + ";" + "Password="******";"; if (database != "") { connectionString = connectionString + "Database=" + database + ";"; } sqlserverConnection = new SqlConnection(connectionString); // Attempt to open database connection. try { sqlserverConnection.Open(); } catch (Exception e) { HttpContext.Current.Session["ErrorMessage"] = "SERVER_UNREACHABLE"; HttpContext.Current.Session["ErrorInfo"] = "Unable to connect to database server '" + server.Hostname + "'."; throw e; } // Set error message, in case we get disconnected. HttpContext.Current.Session["ErrorMessage"] = "SERVER_DISCONNECTED"; // Check that the server has been configured. if (server.NewToDbMgr != "0") { try { ConfigServer(server); } catch (Exception e) { CloseConnection(); throw e; } } }
public void Restore(ServerInfo server, string database, string filepath) { try { // Set error message. HttpContext.Current.Session["ErrorMessage"] = "FILE_NOT_FOUND"; // Fetch file contents. string fullpath = HttpContext.Current.Server.MapPath(@"~\" + filepath); FileInfo file = new FileInfo(fullpath); StreamReader reader = file.OpenText(); string sqldump = reader.ReadToEnd(); reader.Close(); // Set error message. HttpContext.Current.Session["ErrorMessage"] = "BINARY_CONNECTION_FAILED"; // Create command for the MySQL dump. string command = HttpContext.Current.Server.MapPath("/bin/mysql/mysql.exe"); string parameters = "-P " + server.Port + " -h " + server.Hostname + " -u " + server.GetAdminUsername() + " -p" + server.GetAdminPassword() + " " + database; ProcessStartInfo startInfo = new ProcessStartInfo(command, parameters); startInfo.RedirectStandardError = true; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardInput = true; startInfo.UseShellExecute = false; startInfo.ErrorDialog = false; // Create process for MySQL restore. Process process = Process.Start(startInfo); StreamReader error = process.StandardError; StreamWriter input = process.StandardInput; // Input SQL dump file. input.AutoFlush = true; input.Write(sqldump); input.Close(); // Check for errors. string errortext = error.ReadToEnd(); if (errortext.IndexOf("ERROR") != -1) { HttpContext.Current.Session["ErrorMessage"] = "INVALID_RESTORE_FILE"; throw new Exception(); } } catch (Exception e) { throw e; } }
public void Backup(ServerInfo server, string database, string filepath) { try { // Set error message. HttpContext.Current.Session["ErrorMessage"] = "BINARY_CONNECTION_FAILED"; // Create command for the MySQL dump. string command = HttpContext.Current.Server.MapPath("/bin/mysql/mysqldump.exe"); string parameters = "-P " + server.Port + " -h " + server.Hostname + " -u " + server.GetAdminUsername() + " -p" + server.GetAdminPassword() + " " + database; ProcessStartInfo startInfo = new ProcessStartInfo(command, parameters); startInfo.RedirectStandardError = true; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.UseShellExecute = false; startInfo.ErrorDialog = false; // Create process for MySQL dump. Process process = Process.Start(startInfo); StreamReader output = process.StandardOutput; // Set error message. HttpContext.Current.Session["ErrorMessage"] = "BACKUP_SAVE_FAILED"; // Write results to text file. string fullpath = HttpContext.Current.Server.MapPath(@"~\" + filepath); StreamWriter file = new StreamWriter(fullpath, true); using (file) file.Write(output.ReadToEnd()); } catch (Exception e) { throw e; } }