ALFA.SystemInfo.GetSQLConnectionSettings C# (CSharp) Метод

GetSQLConnectionSettings() публичный статический Метод

Get the database connection settings from the SQL plugin, e.g. for use in setting up an auxiliary database connection. Throws an exception if the settings could not be found.
public static GetSQLConnectionSettings ( ) : SQLConnectionSettings
Результат SQLConnectionSettings
        public static SQLConnectionSettings GetSQLConnectionSettings()
        {
            SQLConnectionSettings Settings = new SQLConnectionSettings();
            string[] Lines = File.ReadAllLines(GetNWNX4InstallationDirectory() + "xp_mysql.ini");
            string[] Keywords = {"server", "user", "password", "schema"};

            foreach (string Line in Lines)
            {
                foreach (string Keyword in Keywords)
                {
                    if (!Line.StartsWith(Keyword))
                        continue;

                    //
                    // Skip to the equals sign and check that the intervening
                    // characters were all whitespace.
                    //

                    int Equals = Line.IndexOf('=');

                    if (Equals == -1 || Equals < Keyword.Length)
                        continue;

                    for (int i = Keyword.Length; i < Equals; i += 1)
                    {
                        if (!Char.IsWhiteSpace(Line[i]))
                        {
                            Equals = -1;
                            break;
                        }
                    }

                    if (Equals == -1)
                        continue;

                    //
                    // Now get the value and set it in the structure.
                    //

                    string Value = Line.Substring(Equals + 1).TrimStart(new char[] { '\t', ' '});
                    FieldInfo Field = typeof(SQLConnectionSettings).GetField(Keyword, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
                    
                    Field.SetValue(Settings, Value);
                }
            }

            if (String.IsNullOrWhiteSpace(Settings.Server))
                throw new ApplicationException("No database server specified in the configuraiton file.");
            if (String.IsNullOrWhiteSpace(Settings.User))
                throw new ApplicationException("No database user specified in the configuration file.");
            if (String.IsNullOrWhiteSpace(Settings.Password))
                throw new ApplicationException("No database password specified in the configuration file.");
            if (String.IsNullOrWhiteSpace(Settings.Schema))
                throw new ApplicationException("No database schema specified in the configuration file.");

            return Settings;
        }

Usage Example

        /// <summary>
        /// This method sets up the database connection string based on the
        /// default connection information set for the MySQL plugin.
        ///
        /// The pool size of 3 is based on :
        ///
        /// - One connection for polling for input by the GameWorldManager
        ///   synchronization thread.
        ///
        /// - One connection for background ad-hoc queries kicked off by the
        ///   server communicator on work items (account record lookup, etc.).
        ///
        /// - One connection for miscellaneous use.
        /// </summary>
        private void SetupConnectionString()
        {
            SystemInfo.SQLConnectionSettings ConnectionSettings = SystemInfo.GetSQLConnectionSettings();

            ConnectionString = String.Format("Server={0};Uid={1};Password={2};Allow Batch=true;Treat Tiny As Boolean=false",
                                             ConnectionSettings.Server,
                                             ConnectionSettings.User,
                                             ConnectionSettings.Password,
                                             ConnectionSettings.Schema);

            //
            // If a dedicated connection is not in use, prepend the database
            // with a USE statement to slightly increase performance.
            //

            if (Dedicated == false)
            {
                ConnectionString += ";Max Pool Size=3;Pooling=true";

                this.DatabaseName = ConnectionSettings.Schema;
                this.QueryPrepend = String.Format("USE {0}; ", DatabaseName);
            }
            else
            {
                //
                // Explicitly set the database for a dedicated connection.
                //

                ConnectionString += String.Format(";Database={0}", ConnectionSettings.Schema);
            }
        }