三层中如何用clientdataset通过sql语句加载word文件到sql数据库中,急求大家帮助(200分)

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

wwwone

Unregistered / Unconfirmed
GUEST, unregistred user!
三层中如何用clientdataset通过sql语句加载word文件到sql数据库中,急求大家帮助<br>sql数据表的字段是:<br>字段名 &nbsp;类型<br>bh &nbsp; &nbsp; &nbsp;char<br>doc &nbsp; &nbsp; image<br>我在客户端通这样的sql语句上传和下载文件<br>上传文件:<br>procedure Tbjltz_form.BitBtn3Click(Sender: TObject);<br>var Stream: TMemoryStream;<br>begin<br> &nbsp;OpenDialog1.Filter := 'WORD文档(*.DOC)|*.DOC';<br> &nbsp;if OpenDialog1.Execute then<br> &nbsp;begin<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;Stream := TMemoryStream.Create;<br> &nbsp; &nbsp; &nbsp;Stream.Position := 0;<br> &nbsp; &nbsp; &nbsp;Stream.LoadFromFile(OpenDialog1.filename);<br> &nbsp;with dm.ClientDataSet_Add do<br> &nbsp;begin<br> &nbsp; &nbsp;Close;<br> &nbsp; &nbsp;CommandText := '';<br> &nbsp; &nbsp;CommandText := 'insert into 基本资料 (bh,doc) values(:bh,:doc)';<br> &nbsp; &nbsp;Params.ParamByName('bh').value:='003';<br> &nbsp; &nbsp;Params.ParamByName('doc').LoadFromStream(Stream,ftblob);<br> &nbsp; &nbsp;Execute;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;Stream.Free;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br>操作过程中没有出错<br>下传文件<br>begin<br> &nbsp;with dm.ClientDataSet_Add do<br> &nbsp;begin<br> &nbsp;Close;<br> &nbsp;CommandText := '';<br> &nbsp;CommandText := 'select * from &nbsp;基本资料';<br> &nbsp;open;<br> &nbsp;last;<br> &nbsp;try<br> &nbsp; &nbsp;TBlobField(dm.ClientDataSet_Add.FieldByName('doc')).savetoFile('c:/temp.doc');<br> &nbsp;except<br> &nbsp; &nbsp;showmessage('读取error!');<br> &nbsp; &nbsp;exit;<br> &nbsp;end;<br>end;<br>end;<br>操作过程也没有出错<br><br>但下载的文件的内容只有一个字,并是乱码的,(如果上传到的话,是可以全部看到内容的)<br>请大家帮助,为什么上传时,没有把文件内容上传上去尼,是什么原因,请帮写写好吗
 
Params.ParamByName('doc').LoadFromStream(Stream,ftblob);前面加Stream.Position := 0;<br>看看
 
还是不行啊,那位高手出手帮帮啊,比较急
 
大家好,为什么大富翁的帐号确认功能不能用了,不做确认就没法提问,我是新注册的用户,为此我注册了两个了。能提问的大伙能不能反应一下啊。
 
可能是网站出了问题
 
你先到数据库看一下doc字段有没有内容,确认一下是保存的问题还是加载有问题
 
不知道楼主做什么本身TBLOB可以直接流还要传递?<br> &nbsp; 简单问题复杂化,直接INSERT传递流感觉你缺少什么.
 
太难了点好像
 
unction THRDM.DownFile(strSelSql, UpField, FileName: string): string;<br>var<br> varData:olevariant;<br>begin<br> &nbsp;// result:=self.socketCon.AppServer.DownBlobData(strSelSql,UpField,varData); //成功返回 ok<br> &nbsp; &nbsp; &nbsp;self.GetSqlResult(cdsTmp,strSelSql);<br> &nbsp; if cdsTmp.RecordCount&gt;0 then begin<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; TblobField(cdsTmp.FieldByName(UpField)).SaveToFile(FileName);<br> &nbsp; &nbsp; &nbsp; result:='ok';<br> &nbsp; &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp; result:='保存到'+FileName+'失败';<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; end<br> &nbsp; else result:='没有找到指定记录';<br><br>end;<br>function THRDM.UpFile(aStrWhere,upFieldName,upFile:string;otherUpFieldsVal:string=''): string;<br>var<br> fileOle:olevariant;<br>begin<br> &nbsp; &nbsp;result:='失败';<br><br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; p.ChangeCursor();<br> &nbsp; &nbsp; fileOle:=FileToVariant(upFile);<br> &nbsp; &nbsp;result:=self.sckCon.AppServer.UpBlobData(aStrWhere,upFieldName,fileOle,otherUpFieldsVal);<br><br> &nbsp; &nbsp; //对指定表和字段上传BLOB值,系统自动判断增加还是修改,最后一个参数把字段名和值用;隔开<br> &nbsp; &nbsp;// self.UpFile('select * from 生产_调度日志表 where ID=1','调度日志内容',Upfile);<br> &nbsp; &nbsp; if result='ok' then result:='成功';<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; p.RestoreCursor;<br> &nbsp; &nbsp;end;<br>end;<br>-------------------------------------<br>function THRRDM.UpBlobData(const strSelSql, strUpField: WideString;<br> &nbsp;var varData: OleVariant; const FieldsAndVal: WideString): OleVariant;<br><br> &nbsp;var &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//更新BLOB字段内容,根据strSelSql查找,有则更新,无或者多就新增<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //通常strSelSql 根据 主键来定位<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //FieldsAndVal 用来传入当要新增的时候需要一起提交的字段=值;隔开<br> &nbsp; tmpDb:TadoDataSet ;<br> &nbsp; tmpStream:TmemoryStream;<br> &nbsp; whereStr:string;<br> &nbsp; FieldValList:TstringList;<br> &nbsp; i:integer;<br>begin<br> &nbsp; &nbsp;result:='发生未知错误';<br> &nbsp; &nbsp;tmpDb:=TadoDataSet.Create(self);<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; tmpDb.Connection:=TheMainForm.ADOConnection1;<br><br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FieldsAndVal='' then &nbsp;begin //表示更新<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DBRunSQL(tmpDb,strSelSql);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpStream:=TmemoryStream.Create;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VariantToStream(varData,tmpStream);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpDb.Edit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tBlobField(tmpDb.fieldbyname(strUpField)).LoadFromStream(tmpStream);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpDb.Post;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// tBlobField(tmpDb.fieldbyname(strUpField)).SaveToFile('c:/ldl');<br><br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:='ok';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpStream.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; //以下新增<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin //Begin11 &nbsp; 1<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DBRunSQL(tmpDb,'select top 1 * from '+after('from',strSelSql));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpStream:=TmemoryStream.Create;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VariantToStream(varData,tmpStream);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpDb.insert;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tBlobField(tmpDb.fieldbyname(strUpField)).LoadFromStream(tmpStream);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FieldValList:=TstringList.Create;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TRY<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FieldValList.Text:=stringReplace(FieldsAndVal,';',chr(13),[rfReplaceAll]);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for &nbsp;i:= 0 to FieldValList.Count-1 do begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if FieldValList&lt;&gt;'' then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmpDb.FieldByName(before('=',FieldValList)).AsString:=UnQuotedStr(after('=',FieldValList));//<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; //for 得到每个更新字段<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpDb.Post;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:='ok';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FINALLY<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FieldValLisT.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpStream.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; //Begin11 &nbsp; 1<br> &nbsp; &nbsp; &nbsp;<br><br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;tmpDb.Free ;<br> &nbsp; &nbsp;end;<br>end;<br>---------------------------------<br>以上是我用的代码 &nbsp;一直使用没有任何问题 你修改下应该就可以了
 
// &nbsp; &nbsp; &nbsp;有关三层应用的函数集合<br> &nbsp;// &nbsp; &nbsp; &nbsp; &nbsp;李大林 整理自网上<br> &nbsp;//<br> &nbsp;//<br><br>unit MIDAS_U;<br><br>interface<br> &nbsp;uses Windows, Messages, SysUtils, Classes,Variants;<br> &nbsp;procedure RecordToVariant(var ARec; Size: integer; var v: oleVariant);<br> &nbsp;procedure VariantToRecord(v: oleVariant; var ARec; Size: integer);<br> &nbsp;//////////////变体与记录 转换<br> &nbsp;function FileToVariant(FileName: String): OleVariant;<br> &nbsp;procedure VariantToFile(FileName: String; var AVariant: OleVariant);<br> &nbsp;////变体与文件转换<br> &nbsp;procedure VariantToStream (const v : olevariant;Stream : TMemoryStream);<br> &nbsp;procedure StreamToVariant (Stream : TMemoryStream; var v : OleVariant);<br> &nbsp;//变体与流 转换<br><br><br>implementation<br>{<br> p_p:=VarArrayCreate([0,length(记录数组)-1,0,1],varVariant);<br> &nbsp; &nbsp;for i:=0 to length(记录数组)-1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;p_p[i,0]:=记录数组.dm;<br> &nbsp; &nbsp; &nbsp; &nbsp;p_p[i,1]:=记录数组.mc;<br> &nbsp; &nbsp;end;<br>}<br>function FileToVariant(FileName: String): OleVariant;<br>var<br> &nbsp;AStream: TFileStream;<br> &nbsp;MyBuffer: Pointer;<br>begin<br> &nbsp;AStream:=TFileStream.create(FileName,fmOpenRead);<br> &nbsp;try <br> &nbsp; &nbsp;aStream.Seek(0, soFromBeginning); <br> &nbsp; &nbsp;Result:=VarArraycreate([0, AStream.size-1], VarByte); <br> &nbsp; &nbsp;MyBuffer:=VarArrayLock(Result); <br> &nbsp; &nbsp;AStream.ReadBuffer(MyBuffer^, AStream.Size); <br> &nbsp; &nbsp;VarArrayUnlock(Result); <br> &nbsp;finally <br> &nbsp; &nbsp;AStream.Free; <br> &nbsp;end; <br>end; <br><br><br>procedure VariantToFile(FileName: String; var AVariant: OleVariant);<br>var<br> &nbsp;AStream: TFileStream; <br> &nbsp;MyBuffer: Pointer; <br> &nbsp;Size: Integer; <br>begin <br> &nbsp;AStream:=TFileStream.create(FileName,fmCreate); <br> &nbsp;try <br> &nbsp; &nbsp;aStream.Seek(0, soFromBeginning); <br> &nbsp; &nbsp;Size:=VarArrayHighBound(AVariant,1)+VarArrayLowBound(AVariant,1)+1; <br> &nbsp; &nbsp;MyBuffer:=VarArrayLock(AVariant); <br> &nbsp; &nbsp;AStream.WriteBuffer(MyBuffer^, Size); <br> &nbsp; &nbsp;VarArrayUnlock(AVariant); <br> &nbsp;finally <br> &nbsp; &nbsp;AStream.Free; <br> &nbsp;end; <br>end; <br>procedure VariantToStream (const v : olevariant;Stream : TMemoryStream);<br>var <br> &nbsp;p : pointer; <br>begin <br> &nbsp;Stream.Position := 0;<br> &nbsp;Stream.Size := VarArrayHighBound (v, 1) - VarArrayLowBound(v, &nbsp;1) + 1; <br> p := VarArrayLock (v); <br> &nbsp;Stream.Write (p^, Stream.Size); <br> &nbsp;VarArrayUnlock (v); <br> &nbsp;Stream.Position := 0; <br>end; <br><br>procedure StreamToVariant (Stream : TMemoryStream; var v : OleVariant);<br>var <br> &nbsp;p : pointer; <br>begin <br> &nbsp;v := VarArrayCreate ([0, Stream.Size - 1], varByte); <br> &nbsp;p := VarArrayLock (v); <br> &nbsp;Stream.Position := 0; <br> &nbsp;Stream.Read (p^, Stream.Size); <br> &nbsp;VarArrayUnlock (v); <br>end;<br>procedure RecordToVariant(var ARec; Size: integer; var v: oleVariant); &nbsp; //待测试<br>var<br> p: PByteArray;<br> prec: Pointer;<br> i: integer;<br>begin<br> v := VarArrayCreate([0, Size -1], varByte);<br> p := VarArrayLock(v);<br> prec := @ARec;<br> for i:=0 to Size-1 do<br> &nbsp; p^ := (PByteArray(prec))^;<br> VarArrayUnLock(v);<br>end;<br><br>procedure VariantToRecord(v: oleVariant; var ARec; Size: integer); &nbsp;//待测试<br>var<br> i: integer;<br> p: PByteArray;<br> prec: Pointer;<br>begin<br> prec:=@ARec;<br> p := VarArrayLock(v);<br> for i:=0 to Size-1 do<br> &nbsp; (PByteArray(prec))^ := p^;<br> VarArrayUnLock(v);<br>end;<br> <br>end.<br>-----------以上为需要用到的函数
 
把他们位置对调一下<br> Stream.Position := 0;<br> &nbsp; &nbsp; &nbsp;Stream.LoadFromFile(OpenDialog1.filename);<br>///改为下面的试下/////////<br><br> &nbsp; &nbsp; &nbsp;Stream.LoadFromFile(OpenDialog1.filename);<br> Stream.Position := 0;
 
egetrich 你好,非常感谢你的帮助<br>我的问题如果安照我的写法,应该怎样写才行呢,或还有更简单的吗
 
上传<br>CDSVersion.Edit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CDSVersion.FieldByName('Version').AsString:=Edt_version.Text;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TBlobField(CDSVersion.FieldByName('ProgramFile')).LoadFromFile(Edt_FileName.Text);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CDSVersion.Post;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CDSVersion.ApplyUpdates(0);<br>下载:<br><br> &nbsp; &nbsp;TBlobField(dm.CDSCmd.FieldByName('ProgramFile')).SaveToFile(DirectoryName+'MCC.EXE');
 
大家好,非常感谢大家的帮助<br>我的问题如果安照我的写法,应该怎样写才行呢,或还有更简单的吗
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=845156
 
后退
顶部