三层结构中怎样通过ClientDataSet把jpg,bmp文件保存到服务器端的SQL SERVER 数据库的一个字段中呢(200分)

  • 主题发起人 主题发起人 wwwone
  • 开始时间 开始时间
W

wwwone

Unregistered / Unconfirmed
GUEST, unregistred user!
三层结构中怎样通过ClientDataSet把jpg,bmp文件保存到服务器端的SQL SERVER 数据库的一个字段中呢<br>procedure Tjlsz_form.BitBtn5Click(Sender: TObject);<br>var myfilestream: tfilestream;<br> &nbsp;myblob: tblobfield;<br>begin<br> &nbsp;try<br> &nbsp; &nbsp;with dm.ClientDataSet_Add do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp;CommandText := '';<br> &nbsp; &nbsp; &nbsp;CommandText := 'select * from jpg';<br> &nbsp; &nbsp; &nbsp;open;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;OpenDialog1.Filter := '(*.*)|*.*';<br> &nbsp; &nbsp;if OpenDialog1.Execute then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;dm.ClientDataSet_Add.Append;<br> &nbsp; &nbsp; &nbsp;myfilestream := tfilestream.Create(OpenDialog1.filename, fmopenread);<br> &nbsp; &nbsp; &nbsp;myblob := tblobfield(dm.ClientDataSet_Add.FieldByName('jpg'));<br> &nbsp; &nbsp; &nbsp;myblob.LoadFromStream(myfilestream);<br> &nbsp; &nbsp; &nbsp;myfilestream.Free;<br> &nbsp; &nbsp; &nbsp;dm.ClientDataSet_Add.Post;<br> &nbsp; &nbsp; &nbsp;dm.ClientDataSet_Add.ApplyUpdates(0);<br> &nbsp; &nbsp; &nbsp;dm.ClientDataSet_Add.ApplyUpdates(0);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;showmessage('ok');<br> &nbsp;except<br> &nbsp;end;<br>end;<br>我这样做,但是保存不了数据,是什么原因呢,或用什么方法实现,最好出源码,谢谢大家
 
服务器层写个接口,其中param是传到服务器层的图片数据(OleVariant类型)<br>服务器端:<br>var<br> &nbsp;ADataSet: TADODataSet;<br> &nbsp;Stream : TMemoryStream;<br>begin<br> &nbsp;ADataSet := TADODataSet.Create(nil);<br> &nbsp;ADataSet.Connection := Base.ADOConnection;<br> &nbsp;ADataSet.CommandText := 'SELECT * FROM aaa where .....';<br> &nbsp;ADataSet.Open;<br> &nbsp;if ADataSet.IsEmpty then<br> &nbsp;begin<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;ADataSet.Append;<br> &nbsp; &nbsp; &nbsp;Stream := TMemoryStream.Create;<br> &nbsp; &nbsp; &nbsp;VariantToStream(param, Stream);<br> &nbsp; &nbsp; &nbsp;TBlobField(ADataSet.FieldByName('imgData')).LoadFromStream(Stream);<br> &nbsp; &nbsp; &nbsp;Stream.Free;<br> &nbsp; &nbsp; &nbsp;ADataSet.Post;<br> &nbsp; &nbsp; &nbsp;Result := 0;<br> &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp;Result := -1;<br> &nbsp; &nbsp;end;<br> &nbsp;end else<br> &nbsp;begin<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;ADataSet.Edit;<br> &nbsp; &nbsp; &nbsp;Stream := TMemoryStream.Create;<br> &nbsp; &nbsp; &nbsp;VariantToStream(param, Stream);<br> &nbsp; &nbsp; &nbsp;TBlobField(ADataSet.FieldByName('imgData')).LoadFromStream(Stream);<br> &nbsp; &nbsp; &nbsp;Stream.Free;<br> &nbsp; &nbsp; &nbsp;ADataSet.Post;<br> &nbsp; &nbsp; &nbsp;Result := 0;<br> &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp;Result := -1;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;ADataSet.Close;<br> &nbsp;ADataSet.Free;<br>客户端:<br> &nbsp; &nbsp; &nbsp;Stream := TMemoryStream.Create;<br> &nbsp; &nbsp; &nbsp;TBlobField(cdsDoc.FieldByName('imgData')).SaveToStream(Stream);<br> &nbsp; &nbsp; &nbsp;StreamToVariant(Stream, param);<br> &nbsp; &nbsp; &nbsp;Stream.Free;<br> &nbsp; &nbsp; &nbsp;sSaveImg(param, ....);<br>你再改写吧
 
先保存为流传递。再保存到数据库
 
代碼如下:<br>服務端:<br>function TTmrpserver.SaveImageField(const pSqlstr: WideString;<br> &nbsp;var pStr: OleVariant): WordBool;<br>Var nStrm:TMemoryStream;<br> &nbsp; &nbsp;nRs:TADODataSet;<br>begin<br> &nbsp; nRs:=TADODataSet.Create(nil);<br> &nbsp; nStrm:=TMemoryStream.Create;<br> &nbsp; try<br> &nbsp; &nbsp; &nbsp;nRs.ConnectionString:=server_path;<br> &nbsp; &nbsp; &nbsp;nRs.CommandText:=pSQLStr;<br> &nbsp; &nbsp; &nbsp;nRs.Open;<br> &nbsp; &nbsp; &nbsp;If nRs.RecordCount&gt;0 Then<br> &nbsp; &nbsp; &nbsp;Begin<br> &nbsp; &nbsp; &nbsp; &nbsp; nRs.Edit;<br> &nbsp; &nbsp; &nbsp; &nbsp; nStrm.Position:=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; nStrm.LoadFromStream(OleVariantToMemoryStream(pStr));<br> &nbsp; &nbsp; &nbsp; &nbsp; TBlobField(nRs.FieldByName('Pic_Name')).LoadFromStream(nStrm);<br> &nbsp; &nbsp; &nbsp; &nbsp; nRs.Post;<br> &nbsp; &nbsp; &nbsp;End; <br> &nbsp; finally<br> &nbsp; &nbsp; &nbsp;nRs.Free;<br> &nbsp; &nbsp; &nbsp;nStrm.Free;<br> &nbsp; end;<br>end;<br><br>function OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;<br>var Data:PByteArray;<br> &nbsp; &nbsp;Size:Integer;<br>begin<br> &nbsp; Result:=TMemoryStream.Create;<br> &nbsp; try<br> &nbsp; &nbsp; &nbsp;Size:=VarArrayHighBound(OV,1)-VarArrayLowBound(OV,1)+1;<br> &nbsp; &nbsp; &nbsp;Data:=VarArrayLock(OV);<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; Result.Position:=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; Result.WriteBuffer(Data^,Size);<br> &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp; VarArrayUnlock(OV);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; except<br> &nbsp; &nbsp; &nbsp;Result.Free;<br> &nbsp; &nbsp; &nbsp;Result:=nil;<br> &nbsp; end;<br>end;<br>客戶端:<br>Function MemoryStreamToOleVariant(Strm: TMemoryStream): OleVariant;<br>Var<br> &nbsp;Data: PByteArray;<br>Begin<br><br> &nbsp;Result := VarArrayCreate([0, Strm.Size - 1], varByte);<br> &nbsp;Data := VarArrayLock(Result);<br> &nbsp;Try<br> &nbsp; &nbsp;Strm.Position := 0;<br> &nbsp; &nbsp;Strm.ReadBuffer(Data^, Strm.Size);<br> &nbsp;Finally<br> &nbsp; &nbsp;VarArrayUnlock(Result);<br> &nbsp;End;<br>End;<br>保存圖片:<br>procedure TFrmPo_Delivb_Picture.Pub_GridColumns2EditButtonClick(<br> &nbsp;Sender: TObject; var Handled: Boolean);<br>Var LsSql:String;<br>begin<br> &nbsp; Strm:=TMemoryStream.Create;<br> &nbsp; Try<br> &nbsp; &nbsp; &nbsp;If OpenPictureDialog1.Execute Then<br> &nbsp; &nbsp; &nbsp;Begin<br> &nbsp; &nbsp; &nbsp; &nbsp; Strm.LoadFromFile(OpenPictureDialog1.FileName);<br> &nbsp; &nbsp; &nbsp; &nbsp; SubData.Edit;<br> &nbsp; &nbsp; &nbsp; &nbsp; SubDataPic_Name.LoadFromStream(Strm);<br> &nbsp; &nbsp; &nbsp; &nbsp; LsSql:='Select Pic_Name From Po_Delivb_Picture Where Del_Mark=0 And Sn='<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+IntToStr(SubData.FieldByName('Sn').Value)+' And Delivb_Sn='+ModSn;<br> &nbsp; &nbsp; &nbsp; &nbsp; SaveImageField(LsSql,MemoryStreamToOleVariant(Strm));<br> &nbsp; &nbsp; &nbsp; &nbsp; //tbnRefreshClick(self);<br> &nbsp; &nbsp; &nbsp;End;<br> &nbsp; Finally<br> &nbsp; &nbsp; &nbsp;Strm.Free;<br> &nbsp; End;<br>end;
 
谢谢大家的帮助,我先试试,
 
delphisuccess &nbsp;你好<br><br>我的是三层,用的是socket连接 <br>客户端应该怎样写啊,可否帮我写一下客户端呢上传和下载图片呢
 

Similar threads

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