如何在dbexpress tsqlstoredproc中获取多个结果集

时间:2014-07-12 05:50:13

标签: delphi stored-procedures resultset delphi-xe3 recordset

我希望在Delphi XE3中从tsqlstoredproc获取多个记录或结果集,或者我想从tsqlquery获取多个记录或结果集。

例如:

ds1,ds2: tsqldataset;
 begin
 sqlstoredproc1.open; //or Active:=true
 ds1:=sqlstoredproc1;
 ds2:=sqlstoredproc1.nextrecordset;

感谢所有

1 个答案:

答案 0 :(得分:2)

下面的Button1Click处理程序显示了如何为TSqlStoredProc执行此操作。 TSqlQuery没有实现NextRecordSet函数。这与存储的Proc + DataSet组件的TAdoXXX集不同,它们都提供对其底层MDac RecordSet对象的NextRecordSet函数的访问。

您需要做的就是声明一个TCustomSqlDataset变量,然后将SqlStoredProc的NextRecordSet函数结果赋给它。

需要注意的是,TSqlStoredProc.NextRecordSet返回的CustomSqlDataSet与TSqlStoredProc具有相同的所有者。如果SqlStoredProc在表单上,​​这很好,因为它和表单时将释放CustomSqlDataSet。但是如果你在代码中创建一个无主的SqlStoredProc,你需要自己释放其NextRecordSet返回的任何CustomSqlDataSet,否则你将有内存泄漏。

顺便说一下,你指定的变量.NextRecordSet需要有一个生命周期,至少只要你想使用NextRecordSet。换句话说,将NextRecordSet的结果分配给后来立即退出的过程的局部变量是没有用的(我不确定你的q中的代码是否应该是本地过程)。

  TForm1 = class(TForm)
    SQLConnection1: TSQLConnection;
    SQLStoredProc1: TSQLStoredProc;
    DataSetProvider1: TDataSetProvider;
    ClientDataSet1: TClientDataSet;
    ClientDataSet2: TClientDataSet;
    DataSetProvider2: TDataSetProvider;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    NextDataSet : TCustomSqlDataSet;
  end;

var Form1: TForms;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  NextDataSet := SqlStoredProc1.NextRecordset; 
  DataSetProvider2.DataSet := NextDataSet;
  ClientDataSet2.Open;
end;

部分DFM(出于安全原因编辑TSqlConnection)

object Form1: TForm1
  Left = 265
  Top = 197
  Width = 527
  Height = 358
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 8
    Top = 8
    Width = 320
    Height = 120
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object DBGrid2: TDBGrid
    Left = 8
    Top = 160
    Width = 320
    Height = 120
    DataSource = DataSource2
    TabOrder = 1
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object Button1: TButton
    Left = 352
    Top = 24
    Width = 137
    Height = 25
    Caption = 'Get next recordset'
    TabOrder = 2
    OnClick = Button1Click
  end
  object DataSource1: TDataSource
    DataSet = ClientDataSet1
    Left = 232
    Top = 32
  end
  object SQLStoredProc1: TSQLStoredProc
    Active = True
    MaxBlobSize = -1
    Params = <>
    SQLConnection = SQLConnection1
    StoredProcName = 'getfilesdetails2'
    Left = 72
    Top = 32
  end
  object DataSource2: TDataSource
    DataSet = ClientDataSet2
    Left = 176
    Top = 232
  end
  object DataSetProvider1: TDataSetProvider
    DataSet = SQLStoredProc1
    Left = 112
    Top = 32
  end
  object ClientDataSet1: TClientDataSet
    Active = True
    Aggregates = <>
    Params = <>
    ProviderName = 'DataSetProvider1'
    Left = 192
    Top = 32
  end
  object SQLConnection1: TSQLConnection
    ConnectionName = 'MSSQLConnection'
    DriverName = 'MSSQL'
    GetDriverFunc = 'getSQLDriverMSSQL'
    LibraryName = 'dbexpmss.dll'
    LoginPrompt = False
    Params.Strings = (
      'DriverName=MSSQL'
      'HostName=aaaa'
      'DataBase=bbbb'
      'User_Name=cccc'
      'Password='
      'BlobSize=-1'
      'ErrorResourceFile='
      'LocaleCode=0000'
      'MSSQL TransIsolation=ReadCommited'
      'OS Authentication=False')
    VendorLib = 'oledb'
    Connected = True
    Left = 32
    Top = 32
  end
  object ClientDataSet2: TClientDataSet
    Aggregates = <>
    Params = <>
    ProviderName = 'DataSetProvider2'
    Left = 136
    Top = 232
  end
  object DataSetProvider2: TDataSetProvider
    Left = 88
    Top = 232
  end
end