通过adostoredproc执行存储过程必须先手动创建参数列表,这个创建有点麻烦,所以自已写了个自动创建参数列表的函数,供自已使用,哪位有需要的话也可以直接使用。
//自动创建存储过程列表 function TForm1.CreateProcParaList(ADOStore:TadostoredProc;Pubquery:tadoquery;var MyMsg:string):boolean; var FnstrList:array of string; tmpstr,ProcName,ParaName,ParaDataType:string; FnList:tstrings; i,fnLength:integer; ft:Tdatatype; pd:TParameterDirection; begin Result:=true; MyMsg:=''; if ADOStore.ProcedureName='' then begin Result:=false; MyMsg:='请设置存储过程名称'; Exit; end; ADOStore.Parameters.Clear; FnstrList:=vararrayof(['varchar','int','money','decimal','datetime']); FnList:=TStringList.Create; for I := low(FnstrList) to High(FnstrList) do begin FnList.Add(fnstrlist[i]); end; if not adostore.Connection.Connected then adostore.Connection.Connected:=true; ProcName:=ADOStore.ProcedureName; //取存储过程参数列表 tmpstr:='select A.[Name] ParaName,B.[Name] [DateType],A.Length,A.xprec,' +'A.xscale,A.isoutparam,B.xusertype from syscolumns A ' +'inner join systypes B on A.Xusertype=B.Xusertype ' +'where ID=object_id('+QuotedStr(ProcName)+')'; with Pubquery do begin close; sql.text:=tmpstr; try open; Except Result:=false; MyMsg:='打开存储过程相关对象失败'; Exit; end; try try //创建参数列表 while not Eof do begin ParaName:=FieldByName('ParaName').AsString; ParaName:=copy(ParaName,2,length(ParaName)-1); ParaDataType:=FieldByName('DateType').asstring; fnLength:=FieldByName('Length').AsInteger; if FieldByName('isoutparam').AsInteger=0 then pd:=pdInput else pd:=pdOutput; case FnList.IndexOf(ParaDataType) of 0:ft:=ftString; 1:ft:=ftInteger; 2:ft:=ftCurrency; 3:ft:=ftBCD; 4:ft:=ftDateTime; end; ADOStoredProc1.Parameters.CreateParameter(ParaName,ft,pd,fnlength,Null); next; end; except Result:=false; MyMsg:='创建存储过程参数列表失败'; Exit; end; finally FreeAndNil(FnList); end; end; end;
--以下是调用的方法
procedure TForm1.Button1Click(Sender: TObject); var tmpstr:string; begin //自动创建存储过程列表 if not CreateProcParaList(ADOStoredProc1,adoquery1,tmpstr) then begin MessageDlg(tmpstr,mtwarning,[mbok],0); Exit; end; //调用存储过程 with ADOStoredProc1.Parameters do begin Items[0].Value:='sfsdfsdfsd'; Items[1].Value:=20; Items[2].Value:=15.6; Items[3].Value:=45.4; Items[4].Value:=strtodate('2010-03-04'); end; with ADOStoredProc1 do begin ExecProc; showmessage(Parameters.Items[5].Value); end; end;