Ejemplo n.º 1
0
        /// <summary>
        /// Executes the specified sql commandtext using MySql application itself.
        /// </summary>
        /// <param name="connectionstring">MySql database connection string</param>
        /// <param name="file">MySql dump file to where the sql statements resides</param>
        /// <param name="parameters">Additional MySql parameters</param>
        /// <returns></returns>
        public static MySqlResult Execute(string connectionstring, FileInfo file, MySqlParameterCollection parameters)
        {
            MySqlResult _result = null;

            if (file != null)
            {
                string _sql = file.Read();

                if (!String.IsNullOrEmpty(_sql.RLTrim()))
                {
                    _result = Execute(connectionstring, _sql, parameters);
                }
                else
                {
                    _result = new MySqlResult(file.FullName, "No sql statement has been red from the file.");
                }
            }
            else
            {
                _result = new MySqlResult("", "No file has been specified.");
            }

            return(_result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the specified sql commandtext using MySql application itself.
        /// </summary>
        /// <param name="connectionstring">MySql database connection string</param>
        /// <param name="sql">Sql command statements</param>
        /// <param name="parameters">Additional MySql parameters</param>
        /// <returns></returns>
        public static MySqlResult Execute(string connectionstring, string sql, MySqlParameterCollection parameters)
        {
            MySqlResult _result = null; ExtractResourceApplications();

            if (File.Exists(Application.StartupPath + "\\mysql.exe"))
            {
                string   _filename = Application.StartupPath + "\\tempsql.sql";
                FileInfo _file     = Materia.WriteToFile(_filename, sql);

                if (_file != null)
                {
                    string _batfilepath = Application.StartupPath + "\\execsql.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 + "\\mysql\" -h " + _server + " -u " + _uid + " -p" + _pwd + " " + _database + (String.IsNullOrEmpty(_parameters.RLTrim()) ? "" : " ") + _parameters + " --max_allowed_packet=" + MaxAllowedPacket + "M --default-character-set=utf8 < \"" + _filename.RLTrim().Replace("\\", "/") + "\"";
                    FileInfo _batfile  = Materia.WriteToFile(_batfilepath, _contents);

                    if (_batfile != null)
                    {
                        string _error = ""; Process _process = new Process();

                        IDbConnection _connection = Database.CreateConnection(connectionstring);
                        QueResult     _qresult    = Que.Execute(_connection, "SET GLOBAL max_allowed_packet=(1024 * 1024) * " + MaxAllowedPacket.ToString() + ";");
                        _qresult.Dispose(QueResultDisposition.WithAssociatedQue);

                        if (_connection != null)
                        {
                            if (_connection.State == ConnectionState.Open)
                            {
                                try { _connection.Close(); }
                                catch { }
                            }
                            _connection.Dispose(); _connection = null; Materia.RefreshAndManageCurrentProcess();
                        }

                        _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, sql);

                        int _counter = 0;

                        while (_counter < 30 &&
                               File.Exists(_batfilepath))
                        {
                            try { File.Delete(_batfilepath); }
                            catch { }
                            Materia.RefreshAndManageCurrentProcess();
                            Thread.Sleep(100); Application.DoEvents();
                            _counter += 1;
                        }

                        _counter = 0;

                        while (_counter < 30 &&
                               File.Exists(_filename))
                        {
                            try { File.Delete(_filename); }
                            catch { }
                            Materia.RefreshAndManageCurrentProcess();
                            Thread.Sleep(100); Application.DoEvents();
                            _counter += 1;
                        }
                    }
                    else
                    {
                        _result = new MySqlResult("", "Can't completely initialize database execution.");
                    }
                }
                else
                {
                    _result = new MySqlResult("", "Can't completely initialize sql statement.");
                }
            }
            else
            {
                _result = new MySqlResult("", "Can't extract MySql application resources into default directory : " + Application.StartupPath + ".");
            }

            return(_result);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes the specified sql commandtext using MySql application itself.
        /// </summary>
        /// <param name="connectionstring">MySql database connection string</param>
        /// <param name="sql">Sql command statements</param>
        /// <param name="parameters">Additional MySql parameters</param>
        /// <returns>Development.Materia.Database.MySqlResult that contains the direct MySql application SQL execution information.</returns>
        public static MySqlResult Execute(string connectionstring, string sql, MySqlParameterCollection parameters)
        {
            MySqlResult _result = null; ExtractResourceApplications();

            if (File.Exists(Application.StartupPath + "\\mysql.exe"))
            {
                string _filename = Application.StartupPath + "\\tempsql.sql";
                FileInfo _file = Materia.WriteToFile(_filename, sql);

                if (_file != null)
                {
                    string _batfilepath = Application.StartupPath + "\\execsql.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 + "\\mysql\" -h " + _server + " -u " + _uid + " -p" + _pwd + " " + _database + (String.IsNullOrEmpty(_parameters.RLTrim()) ? "" : " ") + _parameters + " --max_allowed_packet=" + MaxAllowedPacket + "M --default-character-set=utf8 < \"" + _filename.RLTrim().Replace("\\", "/") + "\"";
                    FileInfo _batfile = Materia.WriteToFile(_batfilepath, _contents);

                    if (_batfile != null)
                    {
                        string _error = ""; Process _process = new Process();

                        IDbConnection _connection = Database.CreateConnection(connectionstring);
                        QueResult _qresult = Que.Execute(_connection, "SET GLOBAL max_allowed_packet=(1024 * 1024) * " + MaxAllowedPacket.ToString() + ";");
                        _qresult.Dispose(QueResultDisposition.WithAssociatedQue);

                        if (_connection != null)
                        {
                            if (_connection.State == ConnectionState.Open)
                            {
                                try { _connection.Close(); }
                                catch { }
                            }
                            _connection.Dispose(); _connection = null; Materia.RefreshAndManageCurrentProcess();
                        }

                        _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, sql);

                        int _counter = 0;

                        while (_counter < 30 &&
                               File.Exists(_batfilepath))
                        {
                            try { File.Delete(_batfilepath); }
                            catch { }
                            Materia.RefreshAndManageCurrentProcess();
                            Thread.Sleep(100); Application.DoEvents();
                            _counter += 1;
                        }

                        _counter = 0;

                        while (_counter < 30 &&
                             File.Exists(_filename))
                        {
                            try { File.Delete(_filename); }
                            catch { }
                            Materia.RefreshAndManageCurrentProcess();
                            Thread.Sleep(100); Application.DoEvents();
                            _counter += 1;
                        }

                    }
                    else _result = new MySqlResult("", "Can't completely initialize database execution.");
                }
                else _result = new MySqlResult("", "Can't completely initialize sql statement.");
            }
            else _result = new MySqlResult("", "Can't extract MySql application resources into default directory : " + Application.StartupPath + ".");

            return _result;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Executes the specified sql commandtext using MySql application itself.
        /// </summary>
        /// <param name="connectionstring">MySql database connection string</param>
        /// <param name="file">MySql dump file to where the sql statements resides</param>
        /// <param name="parameters">Additional MySql parameters</param>
        /// <returns>Development.Materia.Database.MySqlResult that contains the direct MySql application SQL execution information.</returns>
        public static MySqlResult Execute(string connectionstring, FileInfo file, MySqlParameterCollection parameters)
        {
            MySqlResult _result = null;

            if (file != null)
            {
                string _sql = file.Read();

                if (!String.IsNullOrEmpty(_sql.RLTrim())) _result = Execute(connectionstring, _sql, parameters);
                else _result = new MySqlResult(file.FullName, "No sql statement has been red from the file.");
            }
            else _result = new MySqlResult("", "No file has been specified.");

            return _result;
        }
Ejemplo n.º 6
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>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;
        }