BlottoBeats.Server.Database.TestConnection C# (CSharp) Method

TestConnection() private method

Tests the connection to the database
private TestConnection ( ) : int
return int
        internal int TestConnection()
        {
            int isConn = 0;
            MySqlConnection conn = null;
            try {
                conn = new MySqlConnection(connString);
                conn.Open();
                isConn = 1;
            } catch (ArgumentException) {
                throw new DatabaseException("Invalid connection string");
            } catch (MySqlException ex) {
                switch (ex.Number) {
                    case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
                        isConn = -1;
                        break;
                    case 0: // Access denied (Check DB name,username,password)
                        isConn = -2;
                        break;
                    default:
                        break;
                }
            } finally {
                conn.Close();
            }
            return isConn;
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Starts the server thread.
        /// </summary>
        internal int Start()
        {
            int dbStatus = database.TestConnection();

            if (dbStatus == 1)
            {
                listenAbort  = false;
                listenThread = new Thread(new ThreadStart(ListenForClients));
                listenThread.Start();
                Log("Server started", 0);
            }
            else
            {
                switch (dbStatus)
                {
                case -1:
                    Log("DATABASE ERROR: Can't connect to specified host", 0);
                    break;

                case -2:
                    Log("DATABASE ERROR: Access Denied", 0);
                    break;

                default:
                    Log("DATABASE ERROR: Can't connect to database", 0);
                    break;
                }
                Log("Server not started", 0);
            }

            return(dbStatus);
        }