Ejemplo n.º 1
0
    public void                                                                            OpenDatabase(bool blnReadSettingsFile = true)
    {
                                        #if USES_STATUSMANAGER
        Status.UpdateStatus();
                                        #endif

        if (!IsServer && !ClientsCanUse)
        {
            return;
        }

        if (IsInitialized && (IsConnectedCheck || IsConnecting))
        {
            return;
        }

        // CHECK FOR SETTINGS FROM TEXT FILE
        bool     blnOkayToProcessTextFile = false;
        bool     blnCouldClientLogIn      = _blnClientCanUse;
        string[] strLines = null;

        // FORCE THE CONFIGURATION FILE THROUGH
        if (blnReadSettingsFile)
        {
            DBsettingsFile = "";
            if (DBtextFile != null)
            {
                DBsettingsFile = DBtextFile.name + ".txt";
            }

            if (!_blnDBisReadIn && DBsettingsFile != "")
            {
                if (!Util.FileExists("", DBsettingsFile))
                {
                                                                #if USES_STATUSMANAGER
                    Status.Status = "Unable to find file \"" + DBsettingsFile + "\".";
                                                                #endif
                    _blnClientCanUse = false;
                }
                else
                {
                    strLines = Util.ReadTextFile("", DBsettingsFile).Split('\n');
                    blnOkayToProcessTextFile = strLines != null && strLines.Length > 0;
                                                                #if USES_STATUSMANAGER
                    Status.Status = DBsettingsFile + " found. " + strLines.Length.ToString() + " lines Read In.";
                                                                #endif
                }

                if (DBtextFile != null && !blnOkayToProcessTextFile)
                {
                    strLines = DBtextFile.text.Split('\n');
                    blnOkayToProcessTextFile = strLines.Length > 0;
                }

                if (blnOkayToProcessTextFile)
                {
                    DBport = 0;
                    if (strLines.Length > 0)
                    {
                        foreach (string st in strLines)
                        {
                            if (!st.StartsWith("//") && st.Trim() != "" && st.Contains("="))
                            {
                                string[] s = st.Trim().Split('=');
                                if (s.Length > 2)
                                {
                                    for (int i = 2; i < s.Length; i++)
                                    {
                                        s[1] += "=" + s[i];
                                    }
                                }
                                switch (s[0].Trim().ToLower())
                                {
                                case "server":
                                    DBserver = s[1].Trim();
                                    break;

                                case "database":
                                    DBdatabase = s[1].Trim();
                                    break;

                                case "username":
                                    DBuser = Crypto.Decrypt(s[1].Trim());
                                    if (DBuser == "")
                                    {
                                        DBuser = s[1].Trim();
                                    }
                                    break;

                                case "password":
                                    DBpassword = Crypto.Decrypt(s[1].Trim());
                                    if (DBpassword == "")
                                    {
                                        DBpassword = s[1].Trim();
                                    }
                                    break;

                                case "port":
                                    try { DBport = int.Parse(s[1].Trim()); }
                                    catch { DBport = 0; }
                                    break;

                                case "retries":
                                    try { MAX_SQL_RETRIES = int.Parse(s[1].Trim()); }
                                    catch { MAX_SQL_RETRIES = 0; }
                                    break;

                                case "cmdcount":
                                    try { MAX_SQL_SAVE_CMDS = int.Parse(s[1].Trim()); }
                                    catch { MAX_SQL_SAVE_CMDS = 0; }
                                    break;

                                case "charcount":
                                    try { MAX_SQL_CHAR_COUNT = int.Parse(s[1].Trim()); }
                                    catch { MAX_SQL_CHAR_COUNT = 0; }
                                    break;

                                case "savedelay":
                                    try { SQL_SAVE_DELAY = float.Parse(s[1].Trim()); }
                                    catch { SQL_SAVE_DELAY = 0; }
                                    break;

                                case "sqlitedbfile":
                                    try { SQLiteDBfileLocation = s[1].Trim(); }
                                    catch { SQLiteDBfileLocation = ""; }
                                    break;
                                }
                            }
                        }
                    }
                    DBuseWindowsAccount = (DBuser == "" && DBpassword == "");
                }
                _blnDBisReadIn = true;
            }
        }


        try
        {
            switch (_dbType)
            {
            case ClsDAL.DBtypes.MSSQL:
                if (DBserver.Trim() != "" && DBdatabase.Trim() != "" && ((DBuser.Trim() != "" && DBpassword.Trim() != "") || DBuseWindowsAccount))
                {
                    if (!_blnClientCanUse)
                    {
                        _blnClientCanUse = blnCouldClientLogIn;
                    }
                    if (DBuseWindowsAccount)
                    {
                        DAL.OpenConnection(DBserver, DBdatabase, DBport);
                    }
                    else
                    {
                        string strUSR = Crypto.Decrypt(DBuser);
                        string strPWD = Crypto.Decrypt(DBpassword);
                        strUSR = (strUSR == "") ? DBuser : strUSR;
                        strPWD = (strPWD == "") ? DBpassword : strPWD;
                        DAL.OpenConnection(DBserver, DBdatabase, strUSR, strPWD, DBport);
                    }
                    _blnInitialized = true;
                    if (DAL.IsConnectedCheck)
                    {
                                                                                #if USES_STATUSMANAGER
                        Status.Status = "Unable to Connect to the Database.";
                        Status.UpdateStatus();
                                                                                #endif
                        _blnClientCanUse = false;
                    }
                }
                else
                {
                                                                        #if IS_DEBUGGING
                                                                        #if USES_APPLICATIONMANAGER
                    App.AddToDebugLog("-- Missing Database Connection Information.");
                                                                        #endif
                                                                        #endif
                }
                break;

            case ClsDAL.DBtypes.MYSQL:
                if (DBserver.Trim() != "" && DBdatabase.Trim() != "" && DBuser.Trim() != "" && DBpassword.Trim() != "")
                {
                    if (!_blnClientCanUse)
                    {
                        _blnClientCanUse = blnCouldClientLogIn;
                    }

                    string strUSR = Crypto.Decrypt(DBuser);
                    string strPWD = Crypto.Decrypt(DBpassword);
                    strUSR = (strUSR == "") ? DBuser : strUSR;
                    strPWD = (strPWD == "") ? DBpassword : strPWD;
                    DAL.OpenConnection(DBserver, DBdatabase, strUSR, strPWD, DBport);

                    _blnInitialized = true;
                    if (DAL.IsConnectedCheck)
                    {
                                                                                #if USES_STATUSMANAGER
                        Status.Status = "Unable to Connect to the Database.";
                        Status.UpdateStatus();
                                                                                #endif
                        _blnClientCanUse = false;
                    }
                }
                else
                {
                                                                        #if IS_DEBUGGING
                                                                        #if USES_APPLICATIONMANAGER
                    App.AddToDebugLog("-- Missing Database Connection Information.");
                                                                        #endif
                                                                        #endif
                }
                break;

            case ClsDAL.DBtypes.SQLITE:
                if (SQLiteDBfileLocation.Trim() != "")
                {
                    DAL.OpenConnection(SQLiteDBfileLocation);
                    _blnInitialized = true;
                    if (DAL.IsConnectedCheck)
                    {
                                                                                #if USES_STATUSMANAGER
                        Status.Status = "Unable to Connect to the Database.";
                        Status.UpdateStatus();
                                                                                #endif
                        _blnClientCanUse = false;
                    }
                }
                else
                {
                                                                        #if IS_DEBUGGING
                                                                        #if USES_APPLICATIONMANAGER
                    App.AddToDebugLog("-- Missing Database Flie Location Information.");
                                                                        #endif
                                                                        #endif
                }
                break;
            }
        } catch {
                                                #if USES_STATUSMANAGER
            Status.Status = "Unable to Connect to the Database.";
            Status.UpdateStatus();
                                                #endif
            _blnClientCanUse = false;
        }

                                        #if USES_STATUSMANAGER
        Status.UpdateStatus();
                                        #endif
    }
Ejemplo n.º 2
0
    public void OpenDatabase()
    {
        //Status.UpdateStatus();

        if (IsInitialized && (IsConnectedCheck || IsConnecting))
        {
            return;
        }

        // check for settings from text file
        // force the configuration file through

        try
        {
            switch (_dbType)
            {
            case ClsDAL.DBTypes.MYSQL:
                if (DBserver.Trim() != "" && DBdatabase.Trim() != "" && DBuser.Trim() != "" && DBpassword.Trim() != "")
                {
                    string strUSR = Crypto.Decrypt(DBuser);
                    string strPWD = Crypto.Decrypt(DBpassword);
                    strUSR = (strUSR == "") ? DBuser : strUSR;
                    strPWD = (strPWD == "") ? DBpassword : strPWD;
                    DAL.OpenConnection(DBserver, DBdatabase, strUSR, strPWD, DBport);

                    _blnInitialized = true;
                    if (DAL.IsConnectedCheck)
                    {
                        //Debug.Log("MySQL服务器链接成功");
                    }
                }
                else
                {
                    Debug.Log("缺少数据库链接信息");
                    //没有数据库链接信息
                }
                break;
            }
        }
        catch
        {
            Debug.Log("Unable to Connect to the Database");
            //Unable to Connect to the Database.";
            //_blnClientCanUse = false;
        }
        //Status.UpdateStatus();
    }