sql-server – 使用Delphi中的ADOConnection查看’print’语句的输出

前端之家收集整理的这篇文章主要介绍了sql-server – 使用Delphi中的ADOConnection查看’print’语句的输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的一些MS sql存储过程使用’print’命令产生消息.在我使用TADOConnection连接到MS sql的Delphi 2007应用程序中,如何查看这些“打印”命令的输出

主要要求:
1)我不能多次运行查询;它可能是更新的东西.
2)即使返回数据集,我也需要看到“打印”结果.

解决方法

那是一个有趣的…
来自ADOConnection的OnInfoMessage事件起作用,但恶魔在细节中!

要点:
使用CursorLocation = clUseServer而不是默认的clUseClient.
使用Open而不是ExecProc与您的ADOStoredProc.
使用当前的NextRecordset来获取以下内容,但一定要检查你是否有一个打开.
在您的存储过程中使用SET NOCOUNT = ON.

sql边:你的存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FG_TEST]') AND type in (N'P',N'PC'))
  DROP PROCEDURE [dbo].[FG_TEST]
GO
-- =============================================
-- Author:      François
-- Description: test multi ADO with info
-- =============================================
CREATE PROCEDURE FG_TEST
AS
BEGIN
    -- SET NOCOUNT ON absolutely NEEDED
    SET NOCOUNT ON;

    PRINT '*** start ***'

    SELECT 'one' as Set1Field1

    PRINT '*** done once ***'

    SELECT 'two' as Set2Field2

    PRINT '*** done again ***'

    SELECT 'three' as Set3Field3

    PRINT '***finish ***'
END
GO

德尔福方面:
创建一个新的VCL表单应用程序.
在您的表格中添加备忘录和按钮.

复制以下文本,更改目录和数据源并将其粘贴到表单上

object ADOConnection1: TADOConnection
  ConnectionString = 
    'Provider=sqlOLEDB.1;Integrated Security=SSPI;Persist Security In' +
    'fo=False;Initial Catalog=xxxYOURxxxDBxxx;Data Source=xxxYOURxxxSERVERxxx'
  CursorLocation = clUseServer
  LoginPrompt = False
  Provider = 'sqlOLEDB.1'
  OnInfoMessage = ADOConnection1InfoMessage
  Left = 24
  Top = 216
end
object ADOStoredProc1: TADOStoredProc
  Connection = ADOConnection1
  CursorLocation = clUseServer
  ProcedureName = 'FG_TEST;1'
  Parameters = <>
  Left = 24
  Top = 264
end

在OnInfoMessage中的ADOConnection put

Memo1.Lines.Add(Error.Description);

对于ButtonClick,粘贴此代码

procedure TForm1.Button1Click(Sender: TObject);
const
  adStateOpen = $00000001; // or defined in ADOInt
var
  I: Integer;
  ARecordSet: _Recordset;
begin
  Memo1.Lines.Add('==========================');

  ADOStoredProc1.Open; // not ExecProc !!!!!

  ARecordSet := ADOStoredProc1.Recordset;
  while Assigned(ARecordSet) do
  begin
    // do whatever with current RecordSet
    while not ADOStoredProc1.Eof do
    begin
      Memo1.Lines.Add(ADOStoredProc1.Fields[0].FieldName + ': ' + ADOStoredProc1.Fields[0].Value);
      ADOStoredProc1.Next;
    end;
    // switch to subsequent RecordSet if any
    ARecordSet := ADOStoredProc1.NextRecordset(I);
    if Assigned(ARecordSet) and ((ARecordSet.State and adStateOpen) <> 0) then
      ADOStoredProc1.Recordset := ARecordSet
    else
      Break;
  end;

  ADOStoredProc1.Close;
end;
原文链接:https://www.f2er.com/mssql/76019.html

猜你在找的MsSQL相关文章