在数据库中插入和检索图像 [英] insert and retrieve an image in/from database

查看:121
本文介绍了在数据库中插入和检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据库中保存图像,但是路径的路径名如
c:user \\ abc \\ desert.jpg,我的数据库名称为tblAbc,列名称为Image,数据类型为nvarchar(50),用于存储路径名,但无法存储图像.我不知道使用字节数组?有人请帮助我提供代码或其他内容
bishnu karki

i want save an image in database but path has pathname like
c:user\\abc\\desert.jpg and i have database name tblAbc with column name Image and datatype nvarchar(50) to store the name of path but i m not able to store the image.i dont know y to use byte array? anybody please help me with the code or anything
bishnu karki

推荐答案

您不能将图像数据存储在50个字符串中:如果您可以看到图像,则该图像不适合. br/> 我也不会将文件路径存储在50个字符串中-文件路径可能比这大得多!增大字段,并将路径存储为字符串.
该代码几乎是我上次给您的代码,但是没有先将文件加载到字节数组中:
You can''t store the image data in a 50 character string: if you can see the image, it just won''t fit.
I also would not store the file path in a 50 character string - a file path can be a lot bigger than that! Make the field bigger, and store the path in it as a string.
The code is pretty much what I gave you last time, but without loading the file into a byte array first:
using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand com = new SqlCommand("INSERT INTO tblAbc ([Image]) VALUES (@IM)", con))
        {
        com.Parameters.AddWithValue("@IM", "c:user\\abc\\desert.jpg");
        com.ExecuteNonQuery();
        }
    }


这篇关于在数据库中插入和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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