Exemplo n.º 1
0
 protected virtual void OnConnected()
 {
     MySQL.Connected(this);
 }
Exemplo n.º 2
0
        /// <summary>
        ///     Attempts to connect to the specified MySQL Server with the given settings.
        /// </summary>
        /// <param name="retries">Retry connection attempts this many times if the initial connection fails.</param>
        /// <param name="createDB">If a database name is specified, should it try to create it if it doesn't exist?</param>
        /// <returns>True if connection successful</returns>
        public bool Connect(int retries = 0, bool createDB = false)
        {
            if (IsDisposed)
            {
                return(false);
            }

            if (Connected)
            {
                return(true);
            }

            if (Connecting)
            {
                return(false);
            }

            if (!CanConnect())
            {
                return(false);
            }

            var conStr = Credentials.GetConnectionString();

            //MySQL.CSOptions.ToConsole("{0}", conStr);

            var connected = VitaNexCore.TryCatchGet(
                () =>
            {
                if (Instance == null)
                {
                    MySQL.CSOptions.ToConsole("Connection Attempt.");

                    Instance              = new OdbcConnection(conStr);
                    Instance.InfoMessage += OnMessage;

                    Instance.Open();

                    VitaNexCore.WaitWhile(() => Instance.State == ConnectionState.Connecting, TimeSpan.FromSeconds(3));

                    if (Instance.State == ConnectionState.Broken)
                    {
                        Instance.Close();
                    }

                    if (Instance.State == ConnectionState.Open)
                    {
                        MySQL.CSOptions.ToConsole("Connection Successful.");
                        MySQL.Connected(this);

                        if (!String.IsNullOrWhiteSpace(Credentials.Database))
                        {
                            if (createDB)
                            {
                                NonQuery(
                                    @"CREATE DATABASE IF NOT EXISTS `{0}` DEFAULT CHARSET `utf8` DEFAULT COLLATE `utf8_bin`",
                                    Credentials.Database);
                            }

                            Instance.ChangeDatabase(Credentials.Database);
                        }

                        return(true);
                    }
                }

                if (Instance == null)
                {
                    return(false);
                }

                if (Instance.State != ConnectionState.Open)
                {
                    Instance.Close();

                    for (var i = 1; i <= retries; i++)
                    {
                        MySQL.CSOptions.ToConsole("Connection Attempt {0}.", i);

                        Instance.Open();

                        VitaNexCore.WaitWhile(() => Instance.State == ConnectionState.Connecting, TimeSpan.FromSeconds(3));

                        if (Instance.State != ConnectionState.Open)
                        {
                            Instance.Close();
                            continue;
                        }

                        MySQL.CSOptions.ToConsole("Connection Successful.");

                        OnConnected();

                        if (!String.IsNullOrWhiteSpace(Credentials.Database))
                        {
                            if (createDB)
                            {
                                NonQuery(
                                    @"CREATE DATABASE IF NOT EXISTS `{0}` DEFAULT CHARSET `utf8` DEFAULT COLLATE `utf8_bin` DEFAULT ENGINE `INNODB`",
                                    Credentials.Database);
                            }

                            Instance.ChangeDatabase(Credentials.Database);
                        }

                        return(true);
                    }
                }

                if (Instance.State != ConnectionState.Open)
                {
                    Instance.Close();
                }

                return(false);
            },
                MySQL.CSOptions.ToConsole);

            if (!connected)
            {
                MySQL.CSOptions.ToConsole("Connection Failed.");
                Close();
            }

            return(connected);
        }