如何从使用对象“ADODB.Command”进行的查询中恢复数据?

时间:2015-01-25 21:44:13

标签: sql-server delphi object com

根据下面的代码,我可以使用动态对象检索查询结果" ADODB.Command"?

此单元将始终使用存储过程,并且将使用的参数类型是输入和输出类型。

使用Delphi 7 + SQL Server Express 2012。

unit uadolibrary;

interface

uses
  ComObj, Variants;

function ADOStart(strConexao: AnsiString): Boolean;
function ADOStop: Boolean;
function ADOConfigParams(ParamName: string; ParamType, ParamIO,
  ParamSize: integer; Val: variant; CanClear: Boolean = False): Boolean;
function ADOSetParamVal(ParamName: string; val: variant): Boolean;
function ADOGetParamVal(ParamName: string): Variant;
function ADOGetValue(_Indece: integer): Variant;
function ADOExecute(procname: string): Boolean;

const
  {Param Data Types}
  adInteger = 3;
  adSingle = 4;
  adDate = 7;
  adBoolean = 11;
  adVarBites = 16;
  adUnsignedTinyInt = 17;
  adDateTime = 135;
  advarChar = 200;

  {Param Directions}
  adParamInput = 1;
  adParamOutput = 2;
  adParamReturnValue = 4;

  {Command Types}
  adCmdText = 1;
  adCmdTable = 2;
  adCmdStoredProc = 4;
  adCmdTableDirect = 512;
  adCmdFile = 256;  

implementation

var
  ADODBConnection,
  ADORecordSet,
  ADOCommand: Variant;

function ADOStart(strConexao: AnsiString): Boolean;
begin
  try
    ADODBConnection:= CreateOleObject('ADODB.Connection');
    ADODBConnection.ConnectionString:= strConexao;
    ADODBConnection.Open;
    ADORecordSet:= CreateOLEObject('ADODB.Recordset');
    ADOCommand:= CreateOLEObject('ADODB.Command');
    ADOCommand.ActiveConnection:= ADODBConnection;
    ADOCommand.CommandText:= 'Procedure';
    ADOCommand.CommandType:= adCmdStoredProc;
    Result:= True;
  except
    begin
      if not (VarIsEmpty(ADODBConnection)) then
      begin
        ADODBConnection.Close;
        ADODBConnection:= Unassigned;
      end;
      if not (VarIsEmpty(ADOCommand)) then
        ADOCommand:= Unassigned;
      if not (VarIsEmpty(ADORecordSet)) then
        ADORecordSet:= Unassigned;
      Result:= False;
    end;
  end;
end;

function ADOStop: Boolean;
begin
  try
    if not (VarIsEmpty(ADODBConnection)) then
      ADODBConnection.Close;
    ADODBConnection:= Unassigned;
    ADORecordSet:= Unassigned;
    ADOCommand:= Unassigned;
    Result:= True;
  except
    Result:= False;
  end;
end;

function ADOConfigParams(ParamName: string; ParamType, ParamIO,
  ParamSize: integer; Val: variant; CanClear: Boolean): Boolean;
var
  _W: integer;
  DerivedParamSize: integer;
begin
  try
    case ParamType of
      adInteger: DerivedParamSize:= 4;
      adSingle : DerivedParamSize:= 4;
      adDate   : DerivedParamSize:= 8;
      adBoolean: DerivedParamSize:= 1;
      adVarBites: DerivedParamSize:= 1;
      adUnsignedTinyInt: DerivedParamSize:= 1;
      adDateTime: DerivedParamSize:= 8;
      advarChar: DerivedParamSize:= ParamSize;
    end;

    if CanClear then
      for _W:= 0 to (ADOCommand.parameters.count) - 1 do
        ADOCommand.parameters.delete(0);

    ADOCommand.parameters.append(
      ADOCommand.createparameter(ParamName, ParamType, ParamIO,
        DerivedParamSize, Val));
    Result:= True;
  except
    Result:= False;
  end;
end;

function ADOSetParamVal(ParamName: string; val: variant): Boolean;
begin
  try
    ADOCommand.Parameters[ParamName].Value := val;
    Result:= True;
  except
    Result:= False;
  end;
end;

function ADOGetParamVal(ParamName: string): Variant;
begin
  try
    Result:= ADOCommand.Parameters[ParamName].Value;
  except
    Result:= varEmpty;
  end;
end;

function ADOExecute(procname: string): Boolean;
begin
  try
    ADOCommand.CommandText:= procname;
    ADOCommand.CommandType:= adCmdStoredProc;
    ADORecordSet:= ADOCommand.Execute;
    Result:= True;
  except
    Result:= False;
  end;
end;

function ADOGetValue(_Indece: integer): Variant;
begin
  Result:= ADORecordSet.Fields.Item[_Indece].Value; {<- I thought it was here}
end;

end.

1 个答案:

答案 0 :(得分:0)

管理在我的存储过程中解决此问题。此函数可以检索返回的数据。

function ADOGetParamVal (ParamName: string): Variant; begin try Result:= ADOCommand.Parameters [ParamName] .Value; except Result:= varEmpty; end; end;