不知道你是jsp还是asp,给你个框架思路:
比如文件夹绝对路径为D:\IMAGE,数据库列“照片”中存的是文件名
那在输出时,需要将路径与文件名拼接起来,做为html的img元素,
比如假设rs为结果集,以asp为例:
dim imgpath
imgpath="D:\IMAGE\"
responsewrite("<img src='" & imgpath & rs("照片") & "'>)
打开数据库
set rec=servercreateobject("ADODBrecordset")
myimagedata=rec("pic")getChunk(7500000)
ResponseContentType = "image/"
ResponseBinaryWrite myimagedata
recclose
set rec=nothing
set connGraph=nothing
===================
PICTURE 的路径设为这个ASP文件!
回答者: xybi - 助理 三级 8-20 04:25
如果数据源中只包含的链接, 要实现该功能,可通过非绑定列的方式来实现。具体实现方法如下: 1 创建了一个非绑定列并设置其相应的属性,属性设置如下: FieldName设为 Image (该字段名必须是唯一的) UnboundType设为 UnboundColumnTypeObject ColumnEdit设为RepositoryItemPictureEdit类的实例(该 *** 作PictureEdit 为该列的内置编辑器) 2 处理View的CustomUnboundColumnData事件,用于为非绑定列填充数据。在该事件中需加载,将其存放在一个hashtable中,然后再将其提供给对应的单元格。 关键代码: //获取文件路径 string GetFileName(string color) { if(color == null || color == stringEmpty) return stringEmpty; return color + "jpg"; } //处理CustomUnboundColumnData事件,为非绑定列填充数据 private void gridView1_CustomUnboundColumnData(object sender, DevExpressXtraGridViewsBaseCustomColumnDataEventArgs e) { if(eColumnFieldName == "Image" && eIsGetData) { GridView view = sender as GridView; string colorName = (string)((DataRowView)eRow)["Color"]; string fileName = GetFileName(colorName)ToLower(); if(!ImagesContainsKey(fileName)) { Image img = null; try { string filePath = DevExpressUtilsFilesHelperFindingFileName(ApplicationStartupPath, ImageDir + fileName, false); img = ImageFromFile(filePath); } catch { } ImagesAdd(fileName, img); } eValue = Images[fileName]; } } 点击下载示例 本站文章除注明转载外,均为本站原创或翻译
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, DB, ADODB,jpeg, DBCtrls, Mask;type
TForm1 = class(TForm)
con1: TADOConnection;
qry1: TADOQuery;
img1: TImage;
btn1: TBitBtn;
btn2: TBitBtn;
OpenDialog1: TOpenDialog;
lbl1: TLabel;
edt1: TEdit;
lbl2: TLabel;
edt2: TEdit;
dbnvgr1: TDBNavigator;
ds1: TDataSource;
qry2: TADOQuery;
img2: TImage;
btn3: TBitBtn;
procedure FormShow(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure dbnvgr1Click(Sender: TObject; Button: TNavigateBtn);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R dfm}procedure TForm1btn1Click(Sender: TObject);
var
ms:TMemoryStream;
jpg:TJPEGImage;
begin
ms:=TMemoryStreamCreate;
jpg:=TJPEGImageCreate;
jpgAssign(img1PictureGraphic);
jpgSaveToStream(ms);
msPosition:=0;
qry1SQLClear;
qry1SQLAdd('Insert Into TIMG(PNAME,PINFO,PPHOTO) values(:pname,:pinfo,:pphoto)');
qry1ParametersParamByName('pname')Value:=edt1Text;
qry1ParametersParamByName('pinfo')Value:=edt2Text;
qry1ParametersParamByName('pphoto')LoadFromStream(ms,ftBlob);
qry1ExecSQL;
jpgFree;
msFree;
end;procedure TForm1btn2Click(Sender: TObject);
begin
if OpenDialog1Execute then
img1PictureLoadFromFile(OpenDialog1FileName);
end;procedure TForm1btn3Click(Sender: TObject);
var
ms:TMemoryStream;
jpg:TJPEGImage;
begin
if not qry2FieldByName('PPHOTO')IsNull then
begin
ms:=TMemoryStreamCreate;
jpg:=TJPEGImageCreate;
TBlobField(qry2FieldByName('PPHOTO'))SaveToStream(ms);
msPosition:=0;
jpgLoadFromStream(ms);
img2PictureAssign(jpg);
end;
end;procedure TForm1dbnvgr1Click(Sender: TObject; Button: TNavigateBtn);
begin
btn3Click;
end;procedure TForm1FormShow(Sender: TObject);
begin
con1Close;
con1ConnectionString:='Driver=Firebird/Interbase(r) driver;UID=SYSDBA;PWD'+
'=123456;Dbname='+ExtractFilePath(ApplicationExeName)+'IMGFDB';
con1LoginPrompt:=False;
con1Connected:=True; qry2Active:=True;
//img2DataField:='PPHOTO';
end;end 我用的是Firebird数据库,存放的字段名是PPHOTO,数据类型是BLOB,其他数据库应该也差不多。
实例:如何用C#读写数据库Image字段实例:如何用C#读写数据库Image字段 1、数据库Image字段读写文件 写文件:写文件的过程为将文件以流文件形式打开并将内容读取到一个byte数组,然后将此byte数组写入数据库的Image字段。
//--用自己的初始连接 初始一个SQL连接实体
MyDataSQLDataContext dbc = new MyDataSQLDataContext("Data Source=;Initial Catalog=TEXT;uid=sa;pwd=8888;");
DataSet ds = null;
string commads = "SELECT [name],[photo] FROM [TABE1]";
ds = dbcGetDataSet(commads);
if (ds != null && dsTablesCount > 0 && dsTables[0]RowsCount > 0)
{
for (int i = 0; i < dsTables[0]RowsCount; i++)
{
try
{
//--判断字段数据是否有数据且有照片流数据
if (((Byte[])dsTables[0]Rows[i]["photo"])Length > 0 && ((Byte[])dsTables[0]Rows[i]["photo"])Max() > 0)
{
byte[] aa = (Byte[])dsTables[0]Rows[i]["photo"];
Bitmap bit = new Bitmap(new SystemIOMemoryStream((Byte[])dsTables[0]Rows[i]["photo"]));
//--保存照片
pictureBox1Image = bit;
}
}
catch (Exception ex)
{ MessageBoxShow(exToString()); }
dsClear(); dsDispose();
}
}
试试
Public Function GetfilType(Tbname as string,Filname as string) as string'功能:获得字段类型Dim rs As New ADODBRecordsetrsOpen Tbname, CurrentProjectConnection, adOpenKeyset, adLockOptimisticGetfilType=rs(Filname)TypeEnd FunctionPublic Function GetfilType(Tbname as string) as string'功能:获得字段类型列表,可用做组合框或列表框数据源Dim rs As New ADODBRecordsetDim i as longrsOpen Tbname, CurrentProjectConnection, adOpenKeyset, adLockOptimisticfor i=0 to rsfieldscount-1 GetfilType=GetfilType & rs(i)name & ";" & rs(i)TypenextEnd Function
以上就是关于怎么从数据库中调取照片全部的内容,包括:怎么从数据库中调取照片、数据库中的图片怎么用Image显示、如何使用 devexpress 里 pivotGridControl 控件 从SQL数据库读取image类型显示图片等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)