在三层结构中,如何向SQL Server数据库增加和取出图片(100分)

  • 主题发起人 主题发起人 JOL
  • 开始时间 开始时间
J

JOL

Unregistered / Unconfirmed
GUEST, unregistred user!
我知道这个问题在
http://www.delphibbs.com/delphibbs/dispq.asp?lid=730756
已经有人问过了,可是回答的却是以c/s模型的,
希望有人能给出一个三层结构的例子,谢谢!!!!
 
如果你用Midas,那就和c/s的方法是一样的,直接Post就行了。就是使用blob流的方法。
如果不使用Midas,那么使用Stream和Variant转换传递图片参数。
procedure VariantToStream(const Data: OleVariant;
Stream: TStream);
var
p: Pointer;
begin
p := VarArrayLock(Data);
try
Stream.Write(p^, VarArrayHighBound(Data,1) + 1);

finally
VarArrayUnlock(Data);
end;
end;

function StreamToVariant(Stream: TStream): OleVariant;
var
p: Pointer;
begin
Result := VarArrayCreate([0, Stream.Size - 1], varByte);
p := VarArrayLock(Result);
try
Stream.Position := 0;

Stream.Read(p^, Stream.Size);
finally
VarArrayUnlock(Result);
end;
end;
 
我用的是Midas,怎么用post的方法呢?
我尝试写了一个,可是不行,代码如下:
procedure TForm1.Button2Click(Sender: TObject);
var
strm:TMemoryStream;
sSQL:String;
begin
if OpenPictureDialog1.Execute then
begin
strm := TMemoryStream.Create ;
strm.Clear ;
strm.LoadFromFile (OpenPictureDialog1.FileName);
sSQL := 'select * from TABLE2';
ClientDataSet1.Close ;
ClientDataSet1.CommandText := sSQL;
ClientDataSet1.Open;
ClientDataSet1.Insert ;
ClientDataSet1.FieldByName('IID').AsInteger := 1;
ClientDataSet1.Params.ParamByName('IMAGES').LoadFromStream(strm,ftblob);
ClientDataSet1.Post;
ClientDataSet1.ApplyUpdates(-1);
end;
end;
数据库返回的信息是说我的IMAGES字段是空的,也就是没有数据传过去,
能告诉我如何传吗?
第一次用三层,对图片的传输也不清楚,请指教。
 
if OpenPictureDialog1.Execute then
begin
ClientDataSet1.Append;
TBlobField(ClientDataSet1.FieldByName('Picture')).LoadFromFile(.....);
ClientDataSet1.Post;
ClientDataSet1.ApplyUpdate(-1);
end;
 
使用 棂上 的可以。
 
数据库的图象存取范例程序
http://www.lvyin.net/download/list.asp?id=31
 
procedure VariantToStream(const Data: OleVariant;
Stream: TStream);
function StreamToVariant(Stream: TStream): OleVariant;
在三层中GANQUAN说的这两个函数怎样用呢。
我在三层中存图片老有问题,BMP有的可以存进去,有的不可以
JPG一点都不行,是OLE的错误,说TEXT与IMAGE不兼容,这是什么原因呢?
 
这是我在人事系统com+三层中的应用示例,看了以前的帖子抄的
注意dbimage1的datasource,datafield为空;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DB, DBTables,jpeg, DBCtrls, ADODB, Mask,
ExtDlgs;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
ADOTable1: TADOTable;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Panel2: TPanel;
DBImage1: TDBImage;
ADOQuery1: TADOQuery;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
Button3: TButton;
Button4: TButton;
Panel3: TPanel;
Image1: TImage;
OpenPictureDialog1: TOpenPictureDialog;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
//保存
procedure TForm1.Button1Click(Sender: TObject);
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
MyJPEG := TJPEGImage.Create;
try
IF Image1.Picture.Graphic<>NIL then
with MyJPEGdo
begin
Assign(Image1.Picture.Graphic);
MS:=TMemoryStream.create;
SaveToStream(MS);
MS.Position:=0;
ADOTable1.Open;
ADOTable1.Append;
ADOTable1.FieldbyName('p_no').asstring:=edit1.Text;
ADOTable1.FieldbyName('p_na').asstring:=edit2.Text;
TBlobField(ADOTable1.FieldbyName('photos')).LoadFromStream(MS);
ADOTable1.Post;
// messagebox(getactivewindow(),'图像保存完毕!','保存',mb_ok);
end;
finally
MyJPEG.Free;
edit1.Text:='';
edit2.Text:='';
Image1.Picture:=nil;
end;
end;

//显示
procedure TForm1.Button3Click(Sender: TObject);
var tempstream:TStringStream;
tempjpeg:TJPEGImage;
begin
try
ADOQuery1.Open;
tempstream:=TStringStream.Create(' ');
TBlobField(ADOQuery1.FieldByName('photos')).SaveToStream(tempstream);
tempstream.Position:=0;
tempjpeg:=TJPEGImage.Create;
tempjpeg.LoadFromStream(tempstream);
DBImage1.Picture.Bitmap.Assign(tempjpeg);
finally
tempstream.Free;
tempjpeg.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//调入图片
try
// OpenPictureDialog1.Filter := 'BMP files (*.bmp)|*.BMP|JPG files (*.jpg,*.jpeg)|*.JPG';
if OpenPictureDialog1.Execute then
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
except
showmessage('图片调入错误!');
abort;
end;

end;

procedure TForm1.Button4Click(Sender: TObject);
//下笔
var tempstream:TStringStream;
tempjpeg:TJPEGImage;
begin
try
ADOQuery1.next;
tempstream:=TStringStream.Create(' ');
TBlobField(ADOQuery1.FieldByName('photos')).SaveToStream(tempstream);
tempstream.Position:=0;
tempjpeg:=TJPEGImage.Create;
tempjpeg.LoadFromStream(tempstream);
DBImage1.Picture.Bitmap.Assign(tempjpeg);
finally
tempstream.Free;
tempjpeg.Free;
end;
end;

end.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
后退
顶部