HashTable 使用例子

    技术2022-05-20  26

    读取数据库中数据

    1.使用Dictionary、HashTable 散列表(快速读取,但在内存中占的空间较大)

    在内存中按照“键值对”存储,Key和Value是成对的。

    例:Step1:存储数据到HashTable中

     

    public static Hashtable GetDownLoadRecords()

            {

                try

                {

                    //首先从Cache中取数据,如果没找到,才从数据库中取数据

                    if (CacheBase.Instance[CACHE_KEY] == null)

                    {

                        DataSet ds = 从数据库中取数据;

     

                        Hashtable hashTb = new Hashtable();

                        if (ds != null && ds.Tables[0] != null)

                        {

                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                            {

    //存储

                                hashTb.Add(ds.Tables[0].Rows[i][0], ds.Tables[0].Rows[i][1]);

                            }

                        }

                        CacheBase.Instance.Add_ExpireByDBDepend(CACHE_KEY, ConfigBLL.MACHINEID, hashTb, 300);

                        return hashTb;

                    }

                    return (Hashtable)CacheBase.Instance[CACHE_KEY]; 

                }

                catch (Exception e)

                {

                    return null;

                }

            }

    Step 2:读HashTable数据

     

    protected string GetRecordsNum(string resourceNo)

        {

            Hashtable tb = new Hashtable();

            tb = GetDownLoadRecords();

            if (tb != null && tb.ContainsKey(resourceNo))

            {

                return tb[resourceNo].ToString();

            }

            return "0";

        }

    2.使用数据库中索引实现

    在数据库中添加索引

    适用于数据量较多,读取频繁且数据修改的不是很频繁

     

     


    最新回复(0)