Example #1
0
        /// <summary>
        /// Backup a MySql database using the specified database connection string into the specified file.
        /// </summary>
        /// <param name="connectionstring">MySql connection string</param>
        /// <param name="filename">Backup file path</param>
        /// <param name="parameters">MySql dump parameter</param>
        /// <returns></returns>
        public static MySqlResult Dump(string connectionstring, string filename, MySqlDumpParameterCollection parameters)
        {
            MySqlResult _result        = null; ExtractResourceApplications();
            string      _mysqldumppath = Application.StartupPath + "\\mysqldump.exe";

            if (File.Exists(_mysqldumppath))
            {
                string _batfilepath = Application.StartupPath + "\\dump.bat";
                string _server      = connectionstring.ConnectionStringValue(ConnectionStringSection.Server);
                string _database    = connectionstring.ConnectionStringValue(ConnectionStringSection.Database);
                string _uid         = connectionstring.ConnectionStringValue(ConnectionStringSection.UID);
                string _pwd         = connectionstring.ConnectionStringValue(ConnectionStringSection.PWD);
                string _parameters  = "";

                if (parameters != null)
                {
                    foreach (string _parameter in parameters)
                    {
                        if (!String.IsNullOrEmpty(_parameter.RLTrim()))
                        {
                            _parameters += (String.IsNullOrEmpty(_parameters.RLTrim()) ? "" : " ") + _parameter;
                        }
                    }
                }

                string   _contents = "\"" + Application.StartupPath + "\\mysqldump\" --host=" + _server + " --user="******" --password="******" " + _database + (String.IsNullOrEmpty(_parameters.RLTrim()) ? "" : " ") + _parameters + " --set-charset --default-character-set=utf8 > \"" + filename + "\"";
                FileInfo _batfile  = Materia.WriteToFile(_batfilepath, _contents);

                if (_batfile != null)
                {
                    string _error = ""; Process _process = new Process();
                    _process.StartInfo.FileName              = _batfilepath;
                    _process.StartInfo.CreateNoWindow        = true; _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    _process.StartInfo.RedirectStandardError = true; _process.StartInfo.UseShellExecute = false;
                    _process.Start();

                    while (!_process.HasExited)
                    {
                        Application.DoEvents();
                    }

                    if (_process.StandardError != null)
                    {
                        try { _error = _process.StandardError.ReadToEnd().Replace("The handle is invalid.\n", "").Replace("The handle is invalid.", "").RLTrim(); }
                        catch { _error = ""; }
                    }
                    else
                    {
                        _error = "";
                    }

                    _process.Dispose(); Materia.RefreshAndManageCurrentProcess();
                    RemoveResourceApplications(); _result = new MySqlResult(filename, _error);

                    int _counter = 0;

                    while (_counter < 30 &&
                           File.Exists(_batfilepath))
                    {
                        try { File.Delete(_batfilepath); }
                        catch { }
                        Materia.RefreshAndManageCurrentProcess();
                        Thread.Sleep(100); Application.DoEvents();
                        _counter += 1;
                    }
                }
                else
                {
                    _result = new MySqlResult("", "Can't create executable batch file into default directory : " + Application.StartupPath + ".");
                }
            }
            else
            {
                _result = new MySqlResult("", "Can't extract MySql application resources into default directory : " + Application.StartupPath + ".");
            }

            RemoveResourceApplications();

            return(_result);
        }
        /// <summary>
        /// Backup a MySql database using the specified database connection string into the specified file.
        /// </summary>
        /// <param name="connectionstring">MySql connection string</param>
        /// <param name="filename">Backup file path</param>
        /// <param name="parameters">MySql dump parameter</param>
        /// <returns>Development.Materia.Database.MySqlResult that contains the MySql dump operations information.</returns>
        public static MySqlResult Dump(string connectionstring, string filename, MySqlDumpParameterCollection parameters)
        {
            MySqlResult _result = null; ExtractResourceApplications();
            string _mysqldumppath = Application.StartupPath + "\\mysqldump.exe";

            if (File.Exists(_mysqldumppath))
            {
                string _batfilepath = Application.StartupPath + "\\dump.bat";
                string _server = connectionstring.ConnectionStringValue(ConnectionStringSection.Server);
                string _database = connectionstring.ConnectionStringValue(ConnectionStringSection.Database);
                string _uid = connectionstring.ConnectionStringValue(ConnectionStringSection.UID);
                string _pwd = connectionstring.ConnectionStringValue(ConnectionStringSection.PWD);
                string _parameters = "";

                if (parameters != null)
                {
                    foreach (string _parameter in parameters)
                    {
                        if (!String.IsNullOrEmpty(_parameter.RLTrim())) _parameters += (String.IsNullOrEmpty(_parameters.RLTrim()) ? "" : " ") + _parameter;
                    }
                }

                string _contents = "\"" + Application.StartupPath + "\\mysqldump\" --host=" + _server + " --user="******" --password="******" " + _database + (String.IsNullOrEmpty(_parameters.RLTrim()) ? "" : " ") + _parameters + " --set-charset --default-character-set=utf8 > \"" + filename + "\"";
                FileInfo _batfile = Materia.WriteToFile(_batfilepath, _contents);

                if (_batfile != null)
                {
                    string _error = ""; Process _process = new Process();
                    _process.StartInfo.FileName = _batfilepath;
                    _process.StartInfo.CreateNoWindow = true; _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    _process.StartInfo.RedirectStandardError = true; _process.StartInfo.UseShellExecute = false;
                    _process.Start();

                    while (!_process.HasExited) Application.DoEvents();

                    if (_process.StandardError != null)
                    {
                        try { _error = _process.StandardError.ReadToEnd().Replace("The handle is invalid.\n", "").Replace("The handle is invalid.", "").RLTrim(); }
                        catch { _error = ""; }
                    }
                    else _error = "";

                    _process.Dispose(); Materia.RefreshAndManageCurrentProcess();
                    RemoveResourceApplications(); _result = new MySqlResult(filename, _error);

                    int _counter = 0;

                    while (_counter < 30 &&
                           File.Exists(_batfilepath))
                    {
                        try { File.Delete(_batfilepath); }
                        catch { }
                        Materia.RefreshAndManageCurrentProcess();
                        Thread.Sleep(100); Application.DoEvents();
                        _counter += 1;
                    }
                }
                else _result = new MySqlResult("", "Can't create executable batch file into default directory : " + Application.StartupPath + ".");
            }
            else _result = new MySqlResult("", "Can't extract MySql application resources into default directory : " + Application.StartupPath + ".");
            
            RemoveResourceApplications();

            return _result;
        }