我们有一个用 Delphi 2007 编写的遗留应用程序,并且仍在使用 BDE(是的,它需要切换到 ADO,但有超过 500K 行,这是一项艰巨的工作)。它使用 SQL SERVER ODBC 连接连接到 SQL Server 2008 DB。我正在尝试切换到 SQL Server Native Client 10.0,但遇到了一个有趣的问题。当尝试向包含日期时间字段的表中插入记录时,我们收到以下错误:
Project DateTimeParamTest.exe raised exception class EDBEngineError with message 'General SQL error.
[Microsoft][SQL Server Native Client 10.0]Datetime field overflow. Fractional second precision exceeds the scale specified in
the parameter binding.'.
在进行一些研究时,我看到了有关使用 TParameter 对象的 NumericScale、Precision 和 Size 参数的评论。一个TADOQuery会自动将参数分别设置为3、23、16,插入没有问题。如果我在 TQuery 对象上将参数设置为相同,则会收到与上述相同的错误。
有没有人有这方面的经验并且知道一个简单的解决方法?我为任何想尝试的人创建了以下示例代码。您只需要更改连接和 SQL 代码。
DateTimeParamTest_Main.dfm:
object Form10: TForm10
Left = 0
Top = 0
Caption = 'Form10'
ClientHeight = 111
ClientWidth = 181
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button2: TButton
Left = 20
Top = 16
Width = 75
Height = 25
Caption = 'BDE'
TabOrder = 0
OnClick = Button2Click
end
object dbPMbde: TDatabase
AliasName = 'PMTest'
DatabaseName = 'DB'
LoginPrompt = False
SessionName = 'Default'
Left = 20
Top = 52
end
object qryBDE: TQuery
DatabaseName = 'DB'
SQL.Strings = (
'INSERT INTO TRAN_DETAIL (ID, STARTDATE, ENDDATE)'
'VALUES (:ID, :STARTDATE, :ENDDATE);')
Left = 88
Top = 52
ParamData = <
item
DataType = ftInteger
Name = 'ID'
ParamType = ptInput
end
item
DataType = ftDateTime
Precision = 23
NumericScale = 3
Name = 'STARTDATE'
ParamType = ptInput
Size = 16
end
item
DataType = ftDateTime
Precision = 23
NumericScale = 3
Name = 'ENDDATE'
ParamType = ptInput
Size = 16
end>
end
end
DateTimeParamTest_Main.pas:
unit DateTimeParamTest_Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBTables;
type
TForm10 = class(TForm)
Button2: TButton;
dbPMbde: TDatabase;
qryBDE: TQuery;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form10: TForm10;
implementation
{$R *.dfm}
procedure TForm10.Button2Click(Sender: TObject);
begin
dbPMbde.Open;
with qryBDE do
begin
parambyname('ID').Value := 99999999;
parambyname('StartDate').Value := now;
parambyname('EndDate').Value := now;
execsql;
end;
dbPMbde.Close;
end;
end.