在缓存中对内存表的查询、写入



        ///


        /// 判断本对象中指定的内存表是否存在
        ///

        /// 表名
        public static bool Contains(string TableName)
        {
            return hashTable.ContainsKey(TableName);
        }

  ///


        /// 从缓存表中查询指定数据  返回符合表名为TableName,字段名ID的值为Value的那条信息的数据
        ///

        /// 内存表名与数据库中的表名一样
        /// 表字段名
        /// 表字段值
        /// 一行数据
        public static DataRow GetDataRow(string TableName, string ID, string Value)
        {
                  DataTable dt = GetData(TableName);
                  DataRow[] row = dt.Select(ID + "=" + "'" + Value + "'");
                   return row[0];

        }     返回的row 可以直接通过 row["字段名"]进行获取表内其他字段值

 public static DataTable GetData(string TableName)
        {
            try
            {
                if (hashTable.Contains(TableName))
                {
                    return (DataTable)hashTable[TableName];
                }
                else
                {
                    //MessageBox.Show("指定内存表不存在!");
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

 ///


        /// 给指定内存表里的固定字段添加信息
        ///

        /// 表名
        public static bool Add(string TableName)
        { DataTable dt = GetData("TableName");
                        dr = dt.NewRow();
                        dr["字段名"] = value;}
    ///
        /// 建立缓存表
        ///

        ///
        ///
        public static void AddInhaseTable(string TableName, DataTable table)
        {
            hashTable.Add(TableName, table);
        }
        public static unsafe Hashtable hashTable = new Hashtable(); 
       
    ///
        /// 建立缓存表
        ///
 
      public void AddTabl() 
       {
                    //建立内存表
                    DataTable TableName = new DataTable();
                    //添加字段
                    TableName.Columns.Add("字段名");
                   
                    //添加一条新记录
                    DataRow dr = TableName.NewRow();
                    dr["字段名"] = value;

                    TableName.Rows.Add(dr);
                    AddInhaseTable("TableName", TableName);
        }


你可能感兴趣的:(缓存)