function THRDM.UpFile(aStrWhere,upFieldName,upFile:string;otherUpFieldsVal:string=''): string;
var
fileOle
levariant;
begin
result:='失败';
try
p.ChangeCursor();
fileOle:=FileToVariant(upFile);
result:=self.sckCon.AppServer.UpBlobData(aStrWhere,upFieldName,fileOle,otherUpFieldsVal);
//对指定表和字段上传BLOB值,系统自动判断增加还是修改,最后一个参数把字段名和值用;隔开
// self.UpFile('select * from 生产_调度日志表 where ID=1','调度日志内容',Upfile);
if result='ok' then
result:='成功';
finally
p.RestoreCursor;
end;
end;
function FileToVariant(FileName: String): OleVariant;
var
AStream: TFileStream;
MyBuffer: Pointer;
begin
AStream:=TFileStream.create(FileName,fmOpenRead);
try
aStream.Seek(0, soFrombegin
ning);
Result:=VarArraycreate([0, AStream.size-1], VarByte);
MyBuffer:=VarArrayLock(Result);
AStream.ReadBuffer(MyBuffer^, AStream.Size);
VarArrayUnlock(Result);
finally
AStream.Free;
end;
end;
----------------------------以上是客户端代码
-----------------以下是中间层函数的实现
function THRRDM.UpBlobData(const strSelSql, strUpField: WideString;
var varData: OleVariant;
const FieldsAndVal: WideString): OleVariant;
var //更新BLOB字段内容,根据strSelSql查找,有则更新,无或者多就新增
//通常strSelSql 根据 主键来定位
//FieldsAndVal 用来传入当要新增的时候需要一起提交的字段=值;隔开
tmpDb:TadoDataSet ;
tmpStream:TmemoryStream;
whereStr:string;
FieldValList:TstringList;
i:integer;
begin
result:='发生未知错误';
tmpDb:=TadoDataSet.Create(self);
try
tmpDb.Connection:=TheMainForm.ADOConnection1;
if FieldsAndVal='' then
begin
//表示更新
DBRunSQL(tmpDb,strSelSql);
tmpStream:=TmemoryStream.Create;
try
VariantToStream(varData,tmpStream);
tmpDb.Edit;
tBlobField(tmpDb.fieldbyname(strUpField)).LoadFromStream(tmpStream);
tmpDb.Post;
// tBlobField(tmpDb.fieldbyname(strUpField)).SaveToFile('c:/ldl');
result:='ok';
finally
tmpStream.Free;
end;
end
else
//以下新增
begin
//begin
11 1
//
DBRunSQL(tmpDb,'select top 1 * from '+after('from',strSelSql));
tmpStream:=TmemoryStream.Create;
try
VariantToStream(varData,tmpStream);
tmpDb.insert;
tBlobField(tmpDb.fieldbyname(strUpField)).LoadFromStream(tmpStream);
FieldValList:=TstringList.Create;
TRY
FieldValList.Text:=stringReplace(FieldsAndVal,';',chr(13),[rfReplaceAll]);
for i:= 0 to FieldValList.Count-1do
begin
if FieldValList
<>'' then
tmpDb.FieldByName(before('=',FieldValList)).AsString:=UnQuotedStr(after('=',FieldValList));//
end;
//for 得到每个更新字段
tmpDb.Post;
result:='ok';
FINALLY
FieldValLisT.Free;
end;
finally
tmpStream.Free;
end;
end;
//begin
11 1
finally
tmpDb.Free ;
end;
end;