⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlaccount.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
📖 第 1 页 / 共 3 页
字号:
        /// <summary>
        /// Returns the Username of this Account.
        /// </summary>
        /// <returns>The Username</returns>
        public override string ToString()
        {
            return m_Username;
        }
        /// <summary>
        /// Checks if the <see cref="ID"/> of the two Accounts is equal.
        /// </summary>
        /// <param name="a1">Account 1</param>
        /// <param name="a2">Account 2</param>
        /// <returns>True if the ID's are equal false otherwise</returns>
        public static bool operator ==  (SqlAccount a1, SqlAccount a2)
        {
            return a1.ID == a2.ID;
        }
        /// <summary>
        /// Checks if the <see cref="ID"/> of the two Accounts is equal.
        /// </summary>
        /// <param name="a1">Account 1</param>
        /// <param name="a2">Account 2</param>
        /// <returns>False if the ID's are equal true otherwise</returns>
        public static bool operator !=(SqlAccount a1, SqlAccount a2)
        {
            return a1.ID != a2.ID;
        }
        /// <summary>
        /// Returns the HashCode of this instance
        /// </summary>
        /// <returns>The HashCode of the ID.</returns>
        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }
        /// <summary>
        /// Checks if the <see cref="ID"/> of the two Accounts is equal.
        /// If obj is null then it checks if ID is equal to -1.
        /// </summary>
        /// <param name="obj">Account</param>
        /// <returns>True if the ID's are equal false otherwise.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
                return ID == -1;
            else if (obj is SqlAccount)
                return ID == ((SqlAccount)obj).ID;
            else
                return false;
        }
        /// <summary>
        /// An Account with the ID of -1.
        /// </summary>
        public static SqlAccount Zero = new SqlAccount(-1);


        #region IIdable Members


        public IIdable Parent
        {
            get { return Gib; }
        }

        public int Count { get { return 0; } }

        #endregion

        #region IEnumerable<IIdable> Members

        public IEnumerator<IIdable> GetEnumerator()
        {
            yield break;
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            yield break;
        }

        #endregion
    }

    /// <summary>
    /// A List of the Accounts for a specified Gib.
    /// </summary>
    public class SqlAccountList : IEnumerable<SqlAccount>
    {

        private int m_gid;

        /// <summary>
        /// Gets/Sets the ID of the Gib that owns this AccountList.
        /// </summary>
        public int GibID
        {
            get { return m_gid; }
            set { m_gid = value; }
        }
        /// <summary>
        /// Creates a AccountList that does not belong to any Gib.
        /// </summary>
        protected SqlAccountList()
        {
            m_gid = -1;
        }
        /// <summary>
        /// Creates a AccountList that belongs to the Specified Gib.
        /// </summary>
        /// <param name="GibID">The ID of the owning Gib.</param>
        public SqlAccountList(int GibID)
        {
            this.GibID = GibID;
        }
        /// <summary>
        /// Creates a AccountList that belongs to the specified Gib.
        /// </summary>
        /// <param name="Gib">The Owning Gib.</param>
        public SqlAccountList(SqlGib Gib)
        {
            this.GibID = Gib.ID;
        }

        #region IList Members

        /// <summary>
        /// Returns the ID of the specified account.
        /// </summary>
        /// <param name="item">The Account you are looking for.</param>
        /// <returns>The ID</returns>
        public int IndexOf(SqlAccount item)
        {
            return item.ID;
        }

        /// <summary>
        /// Removes the Account with the ID of ID.
        /// </summary>
        /// <param name="ID">The ID of the Account you wish to remove.</param>
        public void RemoveAt(int ID)
        {
            //Sql.ExecuteNonQuery("Delete from Accounts where AccountID = ?", ID);
            Remove(new SqlAccount(ID));
        }

        /// <summary>
        /// Gets/Sets the Account with the ID.
        /// </summary>
        /// <param name="ID">The ID of the Account to change.</param>
        public SqlAccount this[int ID]
        {
            get
            {
                return new SqlAccount(ID);
            }
            set
            {
                Sql.ExecuteNonQuery("Update Accounts set GibID = ?, Username = ? , ProtocolType = ?, where AccountID = ?", value.GibID, value.Username, value.ProtocolTypeString, ID);
            }
        }

        #endregion

        
        #region ICollection<SqlAccount> Members
        /// <summary>
        /// Adds the Account to the SqlTable.
        /// </summary>
        /// <param name="item">The Item to add</param>
        public void Add(SqlAccount item)
        {
            Sql.ExecuteNonQuery("Insert into accounts (GibID, Username, ProtocolType) values(?, ?,?)", GibID, item.Username, item.ProtocolTypeString);
        }

        /// <summary>
        /// Deletes all of the Accounts of this AccountList
        /// </summary>
        public void Clear()
        {
            foreach (SqlAccount a in this)
            {
                foreach (Protocol p in a.Protocols)
                {
                    p.Accounts.Remove(a);
                }
            }
            Sql.ExecuteNonQuery("Remove from accounts where GibID =?", GibID);
        }

        /// <summary>
        /// Chceks if the specified Item is in this Accountlist.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(SqlAccount item)
        {
            object o = Sql.ExecuteScalar("select count(AccountID) from Accounts where AccountID = ? and GibID = ?", item.ID,m_gid);
            return (o == null ? false : (int)o > 0);
        }

        /// <summary>
        /// Gets the number of Accounts in this Accountlist.
        /// </summary>
        public int Count
        {
            get
            {
                object o = Sql.ExecuteScalar("Select Count(AccountID) from Accounts where GibID = ?", m_gid);
                return (o == null ? 0 : (int)o);
            }
        }

        /// <summary>
        /// Puts the contents of this AccountList into array.
        /// </summary>
        /// <param name="array">The non empty array to put this into.</param>
        public void CopyTo(SqlAccount[] array)
        {
            CopyTo(array, 0);
        }
        /// <summary>
        /// Puts the contents of this AccountList into array.
        /// </summary>
        /// <param name="array">The non empty array to put this into.</param>
        /// <param name="arrayindex">The index to start copying into array.</param>
        public void CopyTo(SqlAccount[] array, int arrayindex)
        {
            List<SqlAccount> gl = new List<SqlAccount>();
            foreach (SqlAccount a in this)
            {
                gl.Add(a);
            }
            Array.Copy(gl.ToArray(), 0, array, arrayindex, gl.Count);
        }
        /// <summary>
        /// Checks if the there is a account that matches the specified predicate.
        /// </summary>
        /// <param name="match">The condition to check.</param>
        /// <returns></returns>
        public bool Exists(Predicate<SqlAccount> match)
        {
            foreach (SqlAccount a in this)
            {
                if (match(a))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Returns the First Account that returns true for the given
        /// Predicate.
        /// </summary>
        /// <param name="match">The condition to search for.</param>
        /// <returns>The First Account that specifies the condition.</returns>
        public SqlAccount Find(Predicate<SqlAccount> match)
        {
            foreach (SqlAccount a in this)
            {
                if (match(a))
                    return a;
            }
            return new SqlAccount();
        }

        /// <summary>
        /// Returns the ID of the First Account that returns true for the given
        /// Predicate
        /// </summary>
        /// <param name="match">The condition to search for</param>
        /// <returns>The ID of the First Account that specifies the condition</returns>
        public int FindIndex(Predicate<SqlAccount> match)
        {
            foreach (SqlAccount a in this)
            {
                if (match(a))
                    return a.ID;
            }
            return -1;
        }
        /// <summary>
        /// Returns the ID of the First Account after startindex that returns true for the given
        /// Predicate.
        /// </summary>
        /// <param name="match">The condition to search for.</param>
        /// <param name="startindex">The ID to start the search from</param>
        /// <returns>The ID of the First Account after startindex that specifies the condition.</returns>
        public int FindIndex(int startindex, Predicate<SqlAccount> match)
        {
            foreach (SqlAccount a in this)
            {
                if (a.ID >= startindex && match(a))
                    return a.ID;
            }
            return -1;
        }
        /// <summary>
        /// Returns the Last Account that returns true for the given
        /// Predicate.
        /// </summary>
        /// <param name="match">The condition to search for.</param>
        /// <returns>The Last Account that specifies the condition.</returns>
        public SqlAccount FindLast(Predicate<SqlAccount> match)
        {
            foreach (SqlAccount a in Reverse)
            {
                if (match(a))
                    return a;
            }
            return new SqlAccount();
        }

        /// <summary>
        /// Returns the ID of the Last Account that returns true for the given

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -