• 把哈希表存储到数据库中


    最近有个项目可能会把一些复合对象比如哈希表存储到数据库中,就提前做了一些测试

    一般图片,文件等存储到数据库中都是要先转换成字节流byte[]类型的。我想这个hastable之类的集合对象也不例外

    但是就是怎么转换的时候感觉难搞,最后终于是整理出了一个二进制转换帮助类,代码如下

    // <summary>
    ///BinaryHelper 的摘要说明
    ///二进制转换帮助类,一个是对象转换成byte,一个反过来
    /// </summary>

      public class BinaryHelper
        {
             private BinaryHelper()
            {
            }
            public static byte[] BinarySerialize(object o)
            {
                if (o == null)
                    throw new ArgumentNullException("o");
                else
                {
                    MemoryStream ms = new MemoryStream();
                    IFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(ms, o);
                    byte[] bs = ms.GetBuffer();
                    ms.Seek(0, SeekOrigin.Begin);

                    return bs;
                }
            }

            public static object BinaryDeserialize(byte[] bs)
            {
                if (bs == null)
                    throw new ArgumentNullException("bs");
                else
                {
                    IFormatter formatter2 = new BinaryFormatter();
                    MemoryStream ms2 = new MemoryStream();
                    ms2.Write(bs, 0, bs.Length);
                    ms2.Seek(0, SeekOrigin.Begin);
                    return formatter2.Deserialize(ms2);
                }
            }
        }

    代码不多,运行的时候还要分别添加几个using命名空间,大家研究一下就明白了

    然后就是设计一个数据表,我简单设置一个表HS(ID,Content)(sql2005数据库)

    其中Content存储字节,我刚开始使用varbinary(50),第一次插入报错字节被截断了,应该是长度不够

    后了设置成varbinary(max),其实不是很理解max,我感觉这个max应该是根据你插入的数据而变化的,当然使用image类型也可以。

    下面是两个方法,一个是添加到数据库中,一个是从数据库读取

    //添加到数据库

    protected void Button1_Click(object sender, EventArgs e)
        {
            Hashtable table = new Hashtable();
            table.Add("a", "aa");
            table.Add("b","bb");
            table.Add("c","cc");

           byte[] bb =  BinaryHelper.BinarySerialize(table);
           string sql = "insert HS(Content) values(@Content)";

           List<SqlParameter> list = new List<SqlParameter>();
           list.Add(new SqlParameter("@Content", bb));

           SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, sql, list.ToArray());

           
        }

    //从数据库读取
        protected void Button2_Click(object sender, EventArgs e)
        {
            string sql = "select Content from HS where ID = (select max(ID) from HS) ";
            object o = SQLHelper.ExecuteScalar(SQLHelper.ConnectionString, CommandType.Text, sql);
            if (o != null)
            {
                Hashtable ht = (Hashtable)BinaryHelper.BinaryDeserialize((byte[])o);
                if (ht != null)
                {
                    string ss = string.Empty;

                    foreach (string s in ht.Keys)
                    {
                        ss += s + ":" + ht[s].ToString() + "</br>";
                    }
                    Page.Response.Write(ss);
                }
            }
        }

    本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

  • 相关阅读:
    程序的链接
    Graphviz 画图的一些总结
    C表达式中的汇编指令
    epoll(2) 源码分析
    epoll(2) 使用及源码分析的引子
    eventfd(2) 结合 select(2) 源码分析
    poll(2) 源码分析
    select 源码分析
    kfifo
    程序的机器级表示:寻址方式、指令及栈的运行机制
  • 原文地址:https://www.cnblogs.com/zjypp/p/2319453.html
Copyright © 2020-2023  润新知