Parameters转换为Variant数组

http://www.ccw.com.cn/applic/prog/htm2003/20030423_096DZ.htm

ado控件的參數轉化為Variant数组

 

使用ClientDataSet的DataRequest方法把Variant数组上传

 

触发服务器DataSetProvider的OnDataRequest事件,在OnDataRequest对上传的Variant数组进行解析

 

服務器上的ADO控件根據上傳的參數操作數據庫

 

把操作的結果轉換為Variant数组,下發客戶端

 

客戶端解析Variant数组,顯示數據

 

//Parameters转换为Variant数组

function ParametersToVariant(par:TParameters;ProcedureName:Variant): OleVariant;

var

tmpv:Variant;

n,i:integer;

begin

tmpv:=VarArrayCreate([0,par.Count*5+1],VarVariant);

tmpv[0]:=ProcedureName;

tmpv[par.Count*5+1]:='this is fro Midas';

n:=0;

i:=0;

while par.Count>i do begin

tmpv[n+1]:=par.Items[i].Name;

tmpv[n+2]:=par.Items[i].DataType;

tmpv[n+3]:=par.Items[i].Direction;

tmpv[n+4]:=par.Items[i].Size;

tmpv[n+5]:=par.Items[i].Value;

i:=i+1;

n:=n+5;

end;

result:=tmpv;

end;

 

////Variant数组转换到Parameters

function VariantToParameters(input:Variant;par:TParameters):Variant;

var

n,i:integer;

begin

n:=0;

i:=0;

while VarArrayHighBound(input,1)>(n+5)do begin

par.CreateParameter(input[n+1],input[n+2],input[n+3],input[n+4],input[n+5]);

n:=n+5;

end;

result:=true;

end;

 

//MIDAS调用PROC的通用程序

procedure MidasAdoProc(MyProc:TADOStoredProc;MyClientDataSet:TClientDataSet);

var

ins,outs:Variant;

begin

ins:=ParametersToVariant(MyProc.Parameters,MyProc.ProcedureName);

outs:=MyClientDataSet.DataRequest(ins);

MyProc.ProcedureName:=outs[0];

MyProc.Parameters.Clear;

VariantToParameters(outs,MyProc.Parameters);

end;

end.

 

function TMidasAdoProc.DataSetProvider1DataRequest(Sender: TObject;

Input: OleVariant): OleVariant;

begin

if VarIsArray(input) then

begin

if input[VarArrayHighBound(input,1)]='this is fro Midas' then

begin //调存储过程

Form1.Caption:='proc='+input[0];

self.ADOStoredProc1.ProcedureName:=input[0];

self.ADOStoredProc1.Parameters.Clear;

VariantToParameters(input,self.ADOStoredProc1.Parameters);

self.ADOStoredProc1.ExecProc; result:=ParametersToVariant(self.ADOStoredProc1.Parameters,self.ADOStoredProc1.ProcedureName);

end else

Form1.Caption:='来源信息非存储过程信息不明';

end else

Form1.Caption:='来源信息不明';

end;

 

procedure TForm1.Button1Click(Sender: TObject);

begin

if self.SocketConnection1.Connected=false then self.SocketConnection1.Open;

self.ADOStoredProc1.Parameters.Clear;

self.ADOStoredProc1.ProcedureName:='TESTDELPHI'; //动态调用在本处改存储过程名

self.ADOStoredProc1.Parameters.CreateParameter('INS',ftString,pdInput,2000, self.Edit1.Text);

self.ADOStoredProc1.Parameters.CreateParameter('OUTS',ftString,pdOutput,2000,'NUll');

//原来的self.ADOStoredProc1.ExecProc; 改为下面的

MidasAdoProc(self.ADOStoredProc1,self.ClientDataSet1);

ShowMessage('存储过程返回'+self.ADOStoredProc1.Parameters.ParamValues['OUTS']);

end;

posted @ 2010-12-23 16:07  delphi中间件  阅读(433)  评论(0编辑  收藏  举报