ADO未传递第一个参数值

伊恩·博伊德

我正在使用ADO Command对象来针对SQL Server生成参数化查询。

如果我生成并提供多个参数,则传递所有参数值-第一个参数除外

如果您想象这样的查询:

SELECT ?, ?, ?, ?, ?

并使用ADO Command对象提供参数值:

command.Parameters.Append(command.CreateParameter('', adInteger, adParamInput, 0, 1);
command.Parameters.Append(command.CreateParameter('', adInteger, adParamInput, 0, 2);
command.Parameters.Append(command.CreateParameter('', adInteger, adParamInput, 0, 3);
command.Parameters.Append(command.CreateParameter('', adInteger, adParamInput, 0, 4);
command.Parameters.Append(command.CreateParameter('', adInteger, adParamInput, 0, 5);

您可以使用Profiler来查看第一个参数值为null:

在此处输入图片说明

exec sp_executesql N'SELECT @P1, @P2, @P3, @P4, @P5',N'@P1 int,@P2 int,@P3 int,@P4 int,@P5 int',NULL,2,3,4,5

我用不同的类型,不同的查询,不同的顺序尝试了它。它始终是拒绝提供给数据库服务器第一个参数。

而且我可以在调用Execute之前确认该参数确实具有值:

command.Parameters[0].Value

我究竟做错了什么?

  • 客户端: Windows Vista,Windows 7,Windows 10
  • OLEDB提供程序: SQLOLEDB,SQLNCLI11,MSOLEDBSQL
  • 服务器: SQL Server 2000,SQL Server 2005,SQL Server 2008 R2,SQL Server 2012,SQL Sever 2017
  • 编译器:Delphi 5,Delphi XE6,Delphi 10.3 Electric Boogaloo

招商局

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  ActiveX,
  ComObj,
  ADOint,
  Variants;

procedure Main;
var
    cs: string;
    cn: Connection;
    cmd: Command;
    p: _Parameter;
    recordsAffected: OleVariant;
begin
    cs := 'Provider=SQLOLEDB;Data Source=screwdriver;User ID=frog;Password=hunter2';
    cn := CoConnection.Create;
    WriteLn('Connecting to database...');
    cn.Open(cs, '', '', Integer(adConnectUnspecified));

    cmd := CoCommand.Create;
    cmd.CommandType := adCmdText;
    cmd.CommandText := 'IF ? IS NULL RAISERROR(''It was null'', 16, 1);';

    cmd.Parameters.Append(cmd.CreateParameter('', adinteger, adParamInput, 0, 1));

    cmd.Set_ActiveConnection(cn);

    WriteLn('Executing command');
    cmd.Execute({out}recordsAffected, Null, adExecuteNoRecords);
end;

begin
  try
    CoInitialize(nil);
     Main;
        WriteLn('Success');
  except
     on E: Exception do
        begin
            Writeln(E.ClassName, ': ', E.Message);
        end;
  end;
    WriteLn('Press enter to close...');
    ReadLn;

end.

奖励阅读

科比克

您应该声明中使用EmptyParam而不是(这是参数)。Nullcmd.ExecuteParameters

cmd.Execute({out}recordsAffected, EmptyParam, adExecuteNoRecords);

请参阅:执行方法(ADO命令)

EmptyParam应是一个可选的OLE参数兼容。您也可以OleVariant(cmd).Execute在示例中使用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

检查是否设置了传递给bash脚本的第一个参数?

将对象指针作为成员函数的第一个参数传递:这是标准的吗?

第一个参数索引

如何将参数传递给第一个参数为self的python函数?

当参数传递的第一个可观察对象完成时,combinateLatest的变体完成

将Promise传递给`.then`的第一个参数

可变参数函数无法正确传递第一个参数

针对相同的第一个请求在第二个响应中声明json参数值

仅更改列表中第一个元素的方法(将列表作为参数传递)

为什么IEnumerable的Where将第一个参数传递为“ this”

为什么必须将子例程的第一个参数传递给ByVal

如何将AWK命令输出作为第一个参数传递给Python

使xargs作为第一个参数传递

为什么xargs传递给subshell时会跳过第一个参数?

JavaScript传递“;” 作为“ for”语句中的第一个参数

为什么我不能打印传递给程序的第一个参数?

BASH:如果没有传递第一个参数,则如何传递默认参数

$ @除了第一个参数

无论是否传递参数,bash的第一个参数都不同

传递给包装脚本的第一个参数将被忽略

在具有第一个参数预设的方法中传递动作

传递的对象不被视为javascript中函数的第一个参数

传递 __VA_ARGS__ 时第一个参数是错误的

如何使 apply() 在函数的一个参数(不是第一个)中传递对象?

Powershell string -replace 记得第一个参数值吗?

如何在两个 Activity 之间传递参数但一个必须只支持第一个

在 kubernetes 部署中,第一个参数不会传递给图像

通过部分指定第一个参数值来设置 select 的值

打字稿:如何让第二个参数类型依赖于第一个参数值