日期:2014-05-17  浏览次数:20484 次

.net读取数据库中blob类型的二进制数据来显示问题
一个b/s架构的小项目,有个读取用户身份证显示的问题。数据是客户导入的。就是把照片转换成二进制来存储的。现在我需要读取出来,百度的那些方法,读取出来都是这样的错误,

 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["byte_img"].ConnectionString);
            conn.Open();
            string sql = "select top 1 * from byte_img order by ID desc";
         
            SqlDataAdapter sda = new SqlDataAdapter(sql.ToString(),conn);
            DataSet ds = new DataSet();
          
            ds.Clear();
            sda.Fill(ds);
            conn.Close();
            DataTable dt = ds.Tables[0];
            byte[] b2 = (byte[])dt.Rows[0]["ImgByte"];
            string type = (string)dt.Rows[0]["ImgType"];
            Response.Clear();
            string Type = checktype(type);
            Response.ContentType = Type; 
          
            byte[] photo = (byte[])dt.Rows[0]["ImgByte"];
            Response.BinaryWrite(b2);
            Response.End();
 //根据文件的扩展名来获取对应的“输出流的HTTP MIME“类型
    private string checktype(string type)
    {
        string ContentType;
        switch (type)
        {
          
             
            case ".gif ":
                ContentType = "image/gif "; break;
            case ".jpg ":
                ContentType = "image/jpeg "; break;
            case "jpeg ":
                ContentType = "image/jpeg "; break;
            case ".png":
              &nb