从数据库读取图像 [英] reading images from database
本文介绍了从数据库读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
在表格中,我有一个名称为images的字段.此表格中有很多记录.
我不知道如何从这张表格中读取所有图像.
这段代码只给我一张图片;第一个:
hi
in a tabel i have a field which its name is images.this tabel have many records.
i dont know how i can read all images from this tabel.
this code just take me 1 image;the first one:
string strselect="SELECT bannerImg FROM tb_banner" ;
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbtestConnectionString"].ConnectionString);
SqlCommand sqlcom1 = new SqlCommand(strselect, sqlcon);
sqlcon.Open();
object img=sqlcom1.ExecuteScalar();
if (img=!null)
{
Response.BinaryWrite((byte[])img);
sqlcon.Close();
}
推荐答案
这并不困难-您可以在此处查看我的操作方法: ^ ]
It''s not difficult - you can see how I do it here: A generic Image-From-DB class for ASP.NET[^]
您正在使用ExecuteScalar
,而我猜应该是ExecuteReader
.
You are usingExecuteScalar
while I guess it shouldExecuteReader
.
Function SaveImage _
(ByVal FileName As String) As Integer
' Create a FileInfo instance
' to retrieve the files information
Dim fi As New FileInfo(FileName)
' Open the Image file for Read
Dim imgStream As Stream = _
fi.OpenRead
' Create a Byte array the size
' of the image file for the Read
' methods buffer() byte array
Dim imgData(imgStream.Length) As Byte
imgStream.Read(imgData, 0, fi.Length)
Dim cn As New SqlConnection _
(ConfigurationSettings.AppSettings("cn"))
Dim cmd As New SqlCommand("Images_ins", cn)
cmd.CommandType = CommandType.StoredProcedure
With cmd.Parameters
.Add("@FileName", VarChar, 100).Value = _
fi.Name
.Add("@FileType", VarChar, 10).Value = +
fi.Extension
.Add("@Image", Image).Value = imgData
.Add("@FileSize", Int, 4).Value = fi.Length
End With
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
cn.Dispose()
End Function
这篇关于从数据库读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文