我正在对SQL Server数据库使用OLE DB批量复制操作,但是在将数据加载到bit列时遇到了麻烦-它们总是填充有true!

我根据下面的代码段从simple reproduction program创建了Microsoft's sample code。我的程序包括一个用于创建目标表的SQL脚本。我必须下载并安装SQL Server OLE DB driver的x64版本来构建它。

// Set up custom bindings.
    oneBinding.dwPart = DBPART_VALUE | DBPART_LENGTH | DBPART_STATUS;
    oneBinding.iOrdinal = 1;
    oneBinding.pTypeInfo = NULL;
    oneBinding.obValue = ulOffset + offsetof(COLUMNDATA, bData);
    oneBinding.obLength = ulOffset + offsetof(COLUMNDATA, dwLength);
    oneBinding.obStatus = ulOffset + offsetof(COLUMNDATA, dwStatus);
    oneBinding.cbMaxLen = 1;   // Size of varchar column.
    oneBinding.pTypeInfo = NULL;
    oneBinding.pObject = NULL;
    oneBinding.pBindExt = NULL;
    oneBinding.dwFlags = 0;
    oneBinding.eParamIO = DBPARAMIO_NOTPARAM;
    oneBinding.dwMemOwner = DBMEMOWNER_CLIENTOWNED;
    oneBinding.bPrecision = 0;
    oneBinding.bScale = 0;
    oneBinding.wType = DBTYPE_BOOL;
    ulOffset = oneBinding.cbMaxLen + offsetof(COLUMNDATA, bData);
    ulOffset = ROUND_UP(ulOffset, COLUMN_ALIGNVAL);

    if (FAILED(hr =
        pIFastLoad->QueryInterface(IID_IAccessor, (void**)&pIAccessor)))
        return hr;

    if (FAILED(hr = pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA,
        1,
        &oneBinding,
        ulOffset,
        &hAccessor,
        &oneStatus)))
        return hr;

    // Set up memory buffer.
    pData = new BYTE[40];
    if (!(pData /* = new BYTE[40]*/)) {
        hr = E_FAIL;
        goto cleanup;
    }

    pcolData = (COLUMNDATA*)pData;
    pcolData->dwLength = 1;
    pcolData->dwStatus = 0;

    for (i = 0; i < 10; i++)
    {
        if (i % 2 == 0)
        {
            pcolData->bData[0] = 0x00;
        }
        else
        {
            pcolData->bData[0] = 0xFF;
        }


        if (FAILED(hr = pIFastLoad->InsertRow(hAccessor, pData)))
            goto cleanup;
    }

我很可能将错误的值放入缓冲区,或者将某些其他常数值错误。我确实找到了an article,它描述了各种数据类型转换的安全性,并且看起来像字节到 bool(boolean) 是安全的...但是,如果只是字节数组,缓冲区如何知道我要放入哪种数据呢?

最佳答案

弄清楚了这一点,我没有正确地将演示从加载字符串切换为固定宽度值。对于字符串,数据blob获取指向该值的1宽度的指针,而固定宽度的值获取实际的数据。

所以我的COLUMNDATA结构现在看起来像这样:

// How to lay out each column in memory.
struct COLUMNDATA {
    DBLENGTH dwLength;   // Length of data (not space allocated).
    DWORD dwStatus;   // Status of column.
    VARIANT_BOOL bData; // Value, or if a string, a pointer to the value.
};

在这里找到相关的长度:
pcolData = (COLUMNDATA*)pData;
pcolData->dwLength = sizeof(VARIANT_BOOL); // using a short.. make it two
pcolData->dwStatus = DBSTATUS_S_OK; // Indicates that the data value is to be used, not null

循环的小值设置如下所示:
for (i = 0; i < 10; i++)
{
    if (i % 2 == 0)
    {
        pcolData->bData = VARIANT_TRUE;
    }
    else
    {
        pcolData->bData = VARIANT_FALSE;
    }

    if (FAILED(hr = pIFastLoad->InsertRow(hAccessor, pData)))
        goto cleanup;
}

我已经使用工作代码更新了repository。在阅读了obValue属性的documentation之后,我得到了进行此更改的启发。

关于c++ - OLE DB批量复制操作始终将True装入位列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59431230/

10-10 16:30