从SQL数据库中获取图像 [英] Fetch image from sql database

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

问题描述

我想从sqldatabase获取所有图像...我为此目的使用了此查询..但是它仅获取了databse的第一条记录...请指导我是什么问题?

I want to fetch all images from sqldatabase... I used this query for this purpose.. but it is fetching only first record of databse... please guide me what is the problem?

SqlCommand cmd = new SqlCommand("select Images from Table", con);
       SqlDataReader dr = cmd.ExecuteReader();
       while (dr.Read())
       {
           byte[] b = new byte[0];
           b = (Byte[])(dr["Images"]);
           MemoryStream ms = new MemoryStream(b);
           Bitmap pic = new Bitmap(ms);
           Image<Bgr, Byte> image1 = new Image<Bgr, Byte>(pic);
           }

推荐答案

^ ]


您是否在codeproject上进行搜索?
代码项目搜索 [
Have you search on codeproject ?
codeproject search[^]

you can find many questions with good solutions and links


此代码已成功运行...


SqlConnection con =新的SqlConnection(服务器= AAA-PC \\ SQLEXPRESS;集成安全性= true;数据库= systemdb");
con.Open();

SqlDataAdapter empadap1 =新的SqlDataAdapter();
empadap1.SelectCommand =新的SqlCommand("SELECT ImageF,Id FROM Table",con);
DataSet dset = new DataSet("dset");
empadap1.Fill(dset);
数据表dtable;
dtable = dset.Tables [0];



foreach(dtable.Rows中的DataRow dataRow)
{
//使用filestream对象将列写为字节并存储
//作为图片

FileStream FS1 = new FileStream("image.jpg",FileMode.Create);

字符串id = dataRow [1] .ToString();
//int id_int = Convert.ToInt32(id);

byte [] blob =(byte [])dataRow [0];
//对此语句给出错误
FS1.Write(blob,0,blob.Length);

位图pic =新位图(FS1);

FS1.Close();
FS1 = null;
..........
This code runs successfully...


SqlConnection con = new SqlConnection("Server = AAA-PC\\SQLEXPRESS; integrated security = true; database= systemdb");
con.Open();

SqlDataAdapter empadap1 = new SqlDataAdapter();
empadap1.SelectCommand = new SqlCommand("SELECT ImageF,Id FROM Table", con);
DataSet dset = new DataSet("dset");
empadap1.Fill(dset);
DataTable dtable;
dtable = dset.Tables[0];



foreach (DataRow dataRow in dtable.Rows)
{
//using filestream object write the column as bytes and store
// it as an image

FileStream FS1 = new FileStream("image.jpg", FileMode.Create);

string id= dataRow[1].ToString();
// int id_int = Convert.ToInt32(id);

byte[] blob = (byte[])dataRow[0];
//gives error on this statement
FS1.Write(blob, 0, blob.Length);

Bitmap pic = new Bitmap(FS1);

FS1.Close();
FS1 = null;
..........


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

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