MyDAC中存储的函数的返回值 [英] Return value of stored functions in MyDAC

查看:99
本文介绍了MyDAC中存储的函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Devart的MyDac 和MySQL Server 5.0.41.这是文档中的有关使用TMyConnection.ExecProc执行存储过程的部分:

注意:与存储过程不同,存储函数返回通过RESULT参数在内部获得的结果值.您将不再需要在Params数组中提供匿名值来描述函数的结果.存储的函数结果是从Params [0]索引属性或通过ParamByName('RESULT')方法调用获得的.

他们还提供了有关如何执行存储函数的示例:

aStringVariable1 := TMyConnection.ExecProc('StoredFunctionName',['Param1','Param2']); aStringVariable2 := TMyConnection.ParamByName('Result').AsString;

通过下面的示例,我执行的存储函数正在变量aStringVariable2中返回Param1.在查询浏览器中执行函数将返回正确的结果.任何使用TMyConnectionTMyStoredProc在MyDAC中执行存储功能的正确方法的指针,将不胜感激.

谢谢.

解决方案

这是我们用来调用存储过程的代码-希望对您有所帮助

function TDbControl.DatabaseStoredProc(FConnectionsAddr: integer; SpName: string;var Params: TDAParams): boolean;
var
  MyStoredProc: TMyStoredProc;
  PramsTxt: String;
  Idx, Idx2: Integer;
begin
  result := False;
  MyStoredProc := nil;
  try
    try
      MyStoredProc := TMyStoredProc.Create(nil);
      MyStoredProc.Connection := TMyConnection(FConnectionsAddr);
      MyStoredProc.StoredProcName := SpName;
      MyStoredProc.ParamCheck := False;
      if assigned(Params) then
      begin
        for Idx := 0 to Params.Count - 1 do
        begin
            MyStoredProc.ParamByName(Params[Idx].Name).DataType := Params[Idx].DataType;
            MyStoredProc.ParamByName(Params[Idx].Name).Value := Params[Idx].Value;
        end;
      end;
      MyStoredProc.Execute;
      if assigned(Params) then
      begin
        for Idx := 0 to Params.Count - 1 do
        begin
         if (Params[Idx].ParamType =  ptOutput ) then
            Params[Idx].Value := MyStoredProc.ParamByName(Params[Idx].Name).Value;
        end;
      end;
      result := True;
    except
      on E: Exception do
      begin
        PramsTxt := '';
        if assigned(Params) then
        begin
          for Idx2 := 0 to Params.Count - 1 do
          begin
            PramsTxt := PramsTxt + Params.Items[Idx2].Name + '=' + Params[Idx2].AsString + ',';
          end;
        end;
        LogText(FConnectionsAddr, 'DatabaseStoredProc Err:' + E.Message + '  SpName:' + SpName + '  Prams:' + PramsTxt);
        raise ;
      end;
    end;
  finally
    FreeAndNil(MyStoredProc);
  end;
end;

I am working with Devart's MyDac and MySQL Server 5.0.41. Here is a section from the documentation on executing stored procedures with TMyConnection.ExecProc:

Note: Stored functions unlike stored procedures return result values that are obtained internally through the RESULT parameter. You will no longer have to provide anonymous value in the Params array to describe the result of the function. The stored function result is obtained from the Params[0] indexed property or with the ParamByName('RESULT') method call.

They also give an example on how to execute a stored function:

aStringVariable1 := TMyConnection.ExecProc('StoredFunctionName',['Param1','Param2']); aStringVariable2 := TMyConnection.ParamByName('Result').AsString;

By Following these examples, my execution of the stored functions are returning Param1 in the variable aStringVariable2.The execution of the functions in the Query Browser returns the right results. Any pointers on the right way to execute stored functions in MyDAC with TMyConnection or TMyStoredProc will be appreciated.

Thanks in advance.

解决方案

Here is the code we use to call stored procedures - hope it helps

function TDbControl.DatabaseStoredProc(FConnectionsAddr: integer; SpName: string;var Params: TDAParams): boolean;
var
  MyStoredProc: TMyStoredProc;
  PramsTxt: String;
  Idx, Idx2: Integer;
begin
  result := False;
  MyStoredProc := nil;
  try
    try
      MyStoredProc := TMyStoredProc.Create(nil);
      MyStoredProc.Connection := TMyConnection(FConnectionsAddr);
      MyStoredProc.StoredProcName := SpName;
      MyStoredProc.ParamCheck := False;
      if assigned(Params) then
      begin
        for Idx := 0 to Params.Count - 1 do
        begin
            MyStoredProc.ParamByName(Params[Idx].Name).DataType := Params[Idx].DataType;
            MyStoredProc.ParamByName(Params[Idx].Name).Value := Params[Idx].Value;
        end;
      end;
      MyStoredProc.Execute;
      if assigned(Params) then
      begin
        for Idx := 0 to Params.Count - 1 do
        begin
         if (Params[Idx].ParamType =  ptOutput ) then
            Params[Idx].Value := MyStoredProc.ParamByName(Params[Idx].Name).Value;
        end;
      end;
      result := True;
    except
      on E: Exception do
      begin
        PramsTxt := '';
        if assigned(Params) then
        begin
          for Idx2 := 0 to Params.Count - 1 do
          begin
            PramsTxt := PramsTxt + Params.Items[Idx2].Name + '=' + Params[Idx2].AsString + ',';
          end;
        end;
        LogText(FConnectionsAddr, 'DatabaseStoredProc Err:' + E.Message + '  SpName:' + SpName + '  Prams:' + PramsTxt);
        raise ;
      end;
    end;
  finally
    FreeAndNil(MyStoredProc);
  end;
end;

这篇关于MyDAC中存储的函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆