执行后设置CommandTimeout会导致错误列多次出现

问题描述

我有一个使用ADO组件(当前为sql Server 2017)的Delphi项目。

我注意到Microsoft has 3 possible providers,并且Microsoft建议对新项目使用“ Generation 3”。

  sqlOLEDB (Generation 1) works
  sqlNCLI11 (Generation 2) does NOT work
  MSOLEDBsql (Generation 3) does NOT work

为了测试“第3代”,我将连接字符串中的提供程序从sqlOLEDB更改为MSOLEDBsql

但是,我注意到如果在事务中使用TAdoCommand添加列,然后设置了超时,则会引起问题。执行后设置超时后,就会发生错误

以下是重现该问题的示例:

uses
  ADODB;

procedure TForm2.Button1Click(Sender: TObject);
const
  // sqlOLEDB (Generation 1) works
  // sqlNCLI11 (Generation 2) does NOT work
  // MSOLEDBsql (Generation 3) does NOT work
  sqlServerProvider= 'MSOLEDBsql';
var
  mConnection: TADoConnection;
  command: TadoCommand;
begin
  mConnection := TAdoConnection.Create(nil);
  try
    mConnection.ConnectionString := 'Provider='+sqlServerProvider+';Integrated Security=sspI;Persist Security Info=False;Initial Catalog=CORAMASTER_1;Data Source=SHS\HS2017,49010';

    mConnection.KeepConnection := true;
    mConnection.LoginPrompt := false;

    mConnection.Connected := true;

    mConnection.BeginTrans;

    command := TADOCommand.Create(nil);
    try
      command.Connection := mConnection;
      command.ParamCheck := false;
      command.CommandText := 'alter table TESTTABLE add TESTCOLUMN int;';
      command.CommandTimeout := 100;
      command.Execute;

      // If I set "CommandTimeout" here,I get the following error:
      //        Spaltennamen müssen in jeder Tabelle eindeutig sein. Der Spaltenname "..." wurde in der ...-Tabelle mehrmals angegeben.
      //        Translated: Column names must be unique in each table. The column name "..." appears multiple times.
      // The error happens with provider MSOLEDBsql (Generation 3) and sqlNCLI11 (Generation 2),but sqlOLEDB (Generation 1) works
      command.CommandTimeout := 50; // ERROR!
    finally
      FreeAndNil(command); // Note: In the real project,I am keeping the command object. This is just for the example
    end;

    mConnection.RollbackTrans;

  finally
    FreeAndNil(mConnection);
  end;

end;

我在做什么错?这是ADO组件中的错误吗?

编辑:这是使用Microsoft sql Server Profiler的sql Server跟踪日志:

第1代(有效):

enter image description here

第2代:

enter image description here

第3代:

enter image description here

当我执行第二个“ SetTimeout”命令时,sql Server执行命令exec [sys].sp_describe_first_result_set N'alter table TESTTABLE add TESTCOLUMN int;',NULL,1,这显然会导致错误。我不知道为什么调用sp_describe_first_result_set以及如何防止它...

解决方法

我发现了问题。

Microsoft在Using Connection String Keywords with OLE DB Driver for SQL Server上进行了描述:

使用FMTONLY

控制连接到SQL Server 2012时如何检索元数据 (11.x)及更高版本。可能的值为true和false。默认值 值是假的。

默认情况下,SQL Server的OLE DB驱动程序使用 sp_describe_first_result_set和sp_describe_undeclared_pa​​rameters 存储过程以检索元数据。这些存储过程有 某些限制(例如,在 临时表)。将Use FMTONLY设置为true会指示驾驶员 而是使用SET FMTONLY进行元数据检索。

如果我执行mConnection.Execute('set fmtonly on'),则一切正常。

在上面的探查器屏幕截图中,我可以看到,Generation-1驱动程序实际上经常执行这些命令。


编辑: 我注意到set fmtonly on对我不起作用。由于某些原因,各种事情都无法正常工作,例如所有结果集都是空的。

但是我发现有关最初问题的一件非常有趣的事情! 如果在同一函数调用中再次调用CommandTimeout,则只会使其崩溃!如果我此后立即调用CommandTimeout,但在一个新的函数调用中,则一切正常!

这里是一个例子:

procedure CausesBug;
begin
  command.CommandTimeout := 100;
  command.ParamCheck := false;
  command.CommandText := 'alter table TESTTABLE add TESTCOLUMN'+inttostr(Random(10000))+' int;';
  command.Execute;

  command.CommandTimeout := 100; // Crash!
  // This calls: exec [sys].sp_describe_first_result_set N'alter table TESTTABLE add TESTCOLUMN2729 int;',NULL,1
end;

procedure CausesNoBug;
  procedure Test1;
  begin
    command.CommandTimeout := 100;
    command.ParamCheck := false;
    command.CommandText := 'alter table TESTTABLE add TESTCOLUMN'+inttostr(Random(10000))+' int;';
    command.Execute;
  end;
begin
  Test1;
  Test1; // Although CommandTimeout is the first command,everything works!
         // "sp_describe_first_result_set" is NOT called!
  Test1;
  Test1;
end;

还有:

procedure AlsoCausesNoBug;
  procedure Test1;
  begin
    command.ParamCheck := false;
    command.CommandText := 'alter table TESTTABLE add TESTCOLUMN'+inttostr(Random(10000))+' int;';
    command.Execute;
  end;
begin
  command.CommandTimeout := 100;
  Test1;
  command.CommandTimeout := 100;
end;

注意:我还尝试禁用编译器优化,以确保那里没有问题。