图像类型的blob存储

时间:2012-03-27 12:52:26

标签: c#

我想检索保存为数据库中的blobtyped数据的图像。如何获取它并将其保存到Image / Bitmap对象中?

1 个答案:

答案 0 :(得分:0)

using (SqlConnection conn = new SqlConnection(...))
{
    conn.Open();

    using (SqlCommand cmd = new SqlCommand("SELECT BlobFieldName FROM Table ...", conn);
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
        if (reader.Read())
        {
            byte[] bytes = reader["BlobFieldName"] as byte[];

            using (FileStream stream = new FileStream(...))
            {
                stream.Write(bytes, 0, bytes.Length);
                stream.Flush();
            } 
        }
    }
}

这就是我的开始。当然,这里缺少错误检查,我不保证立即编译: - )