如何使用新的 SQL Server Native Client 将 datetime 迁移到 datetime2

How to migrate datetime to datetime2 with new SQL Server Native Client

我们目前正在将数据库从 datetime 迁移到 datetime2,包括 SQL Server Native Client v11 (SQL Server 2012)。数据库更新很容易完成,但是随着我们要使用的新 SQL Server Native Client 11 出现了问题。

一般来说,我们有 OLE DB 消费者,其中 "COLUMN_ENTRY*" 访问器用于我们的 CRUD 操作。对于 datetime2 列,成员的类型为 DBTIMESTAMP。使用 SQLNCLI 提供程序,DBTIMESTAMP 的小数部分被默默地 t运行 调整为支持的值。对于 SQLNCLI11,插入的分数过于精确会导致此错误:

DB_E_ERRORSOCCURRED Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

根据 this link, this error is returned if too much data is being inserted into a field. From this I assumed that the fractional part of the DBDATETIME member is too precise to be inserted. According to this link,新的 Native Client 版本(10 和 11)不满足 运行 要求,但会出现错误。拿这个简化的例子来说说我们想要达到的效果:

class CMyTableStruct
{
public: 
    CMyTableStruct();
    LONG m_lID;
    DBTIMESTAMP m_dtLogTime;
};
class CMyTableStruct_InsertAccessor : public CMyTableStruct
{
public:
    BEGIN_PARAM_MAP(CMyTableStruct_InsertAccessor)
        COLUMN_ENTRY(1,  m_dtLogTime)
    END_PARAM_MAP()  
};

在代码的某些部分,我将时间戳初始化为 2015-08-10 07:47:49.986149999 并且插入失败。如果我将分数重置为 0,则插入有效; 0 旁边的任何值都失败。我尝试使用具有各种值的 COLUMN_ENTRY_PS 提供日期时间精度和比例,但插入总是失败。

我们如何强制 Native Client 简单地接受值并 运行cate 它?很明显,我们无法手动将所有值 运行 分类为支持的数据库精度。我找不到任何关于如何将 datetime2 与新的 Native Client 一起使用的适当文档。我们是否缺少任何正确处理 datetime2 的转换或设置?

这是测试设置:

使用 SQLNCLI 提供程序可以使用相同的代码。

[EDIT#1]:我使用 AtlTraceErrorRecords 转储了更多错误信息,这证实了与链接的 Microsoft Connect 报告中相同的错误:

Row #: 0 Source: "Microsoft SQL Server Native Client 11.0" Description: "The fractional part of the provided time value overflows the scale of the corresponding SQL Server parameter or column. Increase bScale in DBPARAMBINDINFO or column scale to correct this error." Help File: "(null)" Help Context: 0 GUID: {0C733A63-2A1C-11CE-ADE5-00AA0044773D}

[EDIT#2]:根据我的进一步研究,这似乎是新 Native Client 定义和接受的行为。请参阅 Stricter SQL_C_TYPE _TIMESTAMP and DBTYPE_DBTIMESTAMP parameter validation 以供参考。但是,如果没有任何有关如何正确使用 datetime2 的文档,Microsoft 将无法认真对待此更改。在将它们插入数据库之前,他们真的需要开发人员自己对 DBTIMESTAMP 进行舍入吗?在某些情况下,由于某些浮点精度错误,四舍五入甚至不起作用。以上面的示例时间戳为例,您将看到典型的 .9999 精度错误。有人应该如何让这种奇怪的行为起作用?使用此错误消息,如果不使用 now() 的 SQL 函数,您甚至无法将当前时间戳保存到数据库中,因为您通常会在这些溢出中 运行 。

我做了一些测试并完全重写了我的答案。

我使用 SQL Server 2008 和 Native Client 10。它已经支持 datetime2(7),所以我的测试是适用的。

日期时间

我有一个具有 table 值参数的存储过程。参数的 table 的列之一具有 datetime 类型。我之前没有使用非零分之一秒,但现在尝试了。我花了一段时间才弄清楚需要什么。

为了在 DBTIMESTAMP.fraction 字段中使用非零值,我必须同时配置列描述和参数描述:将 DBCOLUMNDESC.bScale 设置为 3,将 DBBINDING.bScale 设置为 3。两者bPrecision 保留为 0。这 MSDN doc 帮助我意识到我还必须设置 DBCOLUMNDESC.bScale

The SQL Server Native Client OLE DB provider inspects the DBCOLUMDESC bScale member to determine the fractional seconds precision.

一旦 Scale 设置为 3,我可以将 DBTIMESTAMP.fraction 的值设置为 555000000 并且它作为 .557 成功插入到数据库中,即服务器将该值进一步四舍五入到毫秒的 1/3(datetime 类型的精度)。 但是,当我尝试将 DBTIMESTAMP.fraction 的值设置为 552100000 时,我得到了与您得到的相同的错误。因此,服务器希望程序员自己对值进行舍入。

一种截断分数的简单方法如下所示:

DBTIMESTAMP dt;
// set to any value from 0 to 999,999,999 (billionth of a second)
dt.fraction = 555123456; 

// truncate fractions to milliseconds before inserting into the database
dt.fraction /= 1000000;
dt.fraction *= 1000000;

datetime2(7)

我将 table 值参数中的列类型更改为 datetime2(7) 并做了更多测试。

我将 Scale 设置为 7,并且可以成功地将值 555123400 插入到数据库 DBTIMESTAMP.fraction 中,但是当我尝试插入值 555123478 时,我得到了同样的错误。因此,对于 datetime2(7) 服务器还希望程序员自己对值进行舍入。

// truncate fractions to 100 nanoseconds precision
dt.fraction /= 100;
dt.fraction *= 100;

应该够了。

将 C# 或 VB 与 OleDb 库一起使用时,请设置 OleDbParameter.Scale 值以避免出现此错误消息。

使用 Provider=SQLNCLI11 测试。