예제 #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);
        }
예제 #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);
        }
예제 #3
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;
        }
예제 #4
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;
        }