Example #1
0
        /// <summary>
        /// Attempt to check database file for the specified users for past information.
        /// </summary>
        /// <param name="channel">Referenced channel</param>
        /// <param name="sqlListofUsers">List of users to check database for</param>
        public void loadDatabase(string channel, string sqlListofUsers)
        {
            if (!connDict.ContainsKey(channel))
            {
                onNoFileOpen?.Invoke(this, new OnNoFileOpenArgs {
                    channel        = channel, fileError = FileErrorType.NoDatabaseFile,
                    listofUsersSql = sqlListofUsers
                });
                return;
            }
            else
            {
                try
                {
                    Debug.WriteLine("sqlilistofusers is " + sqlListofUsers);

                    SqlCommand newCommand = new SqlCommand();
                    newCommand.Connection  = connDict[channel];
                    newCommand.CommandText = "SELECT * FROM dbo.Users WHERE " + sqlListofUsers;
                    newCommand.Parameters.Add("@channel", SqlDbType.Text);
                    newCommand.Parameters["@channel"].Value = channel;
                    newCommand.Connection.Open();
                    AsyncCallback     callback = new AsyncCallback(LoadDatabaseCallback);
                    CommandandChannel cc       = new CommandandChannel()
                    {
                        theCommand = newCommand, channel = channel
                    };
                    newCommand.BeginExecuteReader(callback, cc);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error code is " + e.HResult + " from the method " + e.TargetSite);
                    if (connDict[channel].State == ConnectionState.Open)
                    {
                        connDict[channel].Close();
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Callback for loadDatabase method.
        /// </summary>
        /// <param name="result"></param>
        private void LoadDatabaseCallback(IAsyncResult result)
        {
            CommandandChannel retrievedcc = (CommandandChannel)result.AsyncState;
            //string channel = retrievedCommand.Parameters["@channel"].ToString();
            SqlCommand retrievedCommand = retrievedcc.theCommand;
            string     theChannel       = retrievedcc.channel;

            SqlDataReader dbData = retrievedCommand.EndExecuteReader(result);

            try
            {
                while (dbData.Read())
                {
                    //name, mod, id, points, modifier, alias, lastCheckIn
                    User tempUser = new User($"{dbData.GetString(0)}")
                    {
                        mod         = dbData.GetBoolean(1), points = dbData.GetInt32(3),
                        lastCheckIn = dbData.GetDateTime(6), regular = dbData.GetBoolean(7), existsDB = true
                    };
                    onDBActionArgs?.Invoke(this, new OnDBActionArgs()
                    {
                        channel = theChannel,
                        user    = tempUser, dbActionType = DBActionType.LoadCall
                    });
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.StackTrace);
            }

            if (retrievedCommand.Connection.State == ConnectionState.Open)
            {
                retrievedCommand.Connection.Close();
            }
        }