我可以使用SQL Server数据库的varbinary类型来存储图像? [英] Can I use varbinary type to store image in SQL Server database?

查看:1144
本文介绍了我可以使用SQL Server数据库的varbinary类型来存储图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的图像存储在我的SQL Server数据库,我应该用什么数据类型?

在下面的 code aspx.cs ,我试图从请求InputStream读的所有字节,并将其存储在数据库中,但字节[] 阵列没有正确表更新。我缺少的东西吗?

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
        Request.InputStream.Position = 0;        字节[]内容=新的字节[Request.InputStream.Length]        Request.InputStream.Read(目录,0,(int)的Request.InputStream.Length);        con.Open();        尝试
        {
            查询字符串=更新tblImageUpload设置+ IMAGE_ID += @imageBytes,其中Image_ID ='+ CID +';            INT I = 0;            使用(CMD =新的SqlCommand(查询,CON))
            {
                cmd.Parameters.Add(@ imageBytes,SqlDbType.VarBinary,Contents.Length).value的内容=;                 I = cmd.ExecuteNonQuery();
            }            的Response.Write(上传查询=+查询);
            的Response.Write(上传code =+ I);
        }赶上(例外前){
            的Response.Write(上传code =+ EX);
        }


解决方案

您可以使用 VARBINARY 是。你可能最好关闭与 VARBINARY(MAX)要保存。

您可以使用它是这样的:

  cmd.Parameters.Add(@ imageBytes,SqlDbType.VarBinaryMax);
cmd.Parameters [@ imageBytes]值=内容。

I am trying to store an image in my SQL Server database, what datatype should I use?

In the below code aspx.cs, I am trying to read all the bytes from the request inputstream and store it in the database, but the byte[] array is not updated properly in table. Am I missing something?

protected void Page_Load(object sender, EventArgs e)
{
        Request.InputStream.Position = 0;

        byte[] Contents = new byte[Request.InputStream.Length];

        Request.InputStream.Read(Contents, 0, (int)Request.InputStream.Length);

        con.Open();

        try
        {
            string query = "update tblImageUpload set " + IMAGE_ID + " = @imageBytes where Image_ID='" + CID + "'";

            int i = 0;

            using (cmd = new SqlCommand(query, con))
            {
                cmd.Parameters.Add("@imageBytes", SqlDbType.VarBinary, Contents.Length).Value = Contents;

                 i = cmd.ExecuteNonQuery();
            }

            Response.Write("Upload Query = " + query);
            Response.Write("Upload Code = " + i);
        } catch (Exception ex) {
            Response.Write("Upload Code=" + ex);
        }

解决方案

You can use VARBINARY yes. You're probably best off going with VARBINARY(MAX) to store them.

You can use it like this:

cmd.Parameters.Add("@imageBytes", SqlDbType.VarBinaryMax);
cmd.Parameters["@imageBytes"].Value = Contents;

这篇关于我可以使用SQL Server数据库的varbinary类型来存储图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆