下面的也是一种使用ADO批量插入的数据的方法,不过没有尝试是否正确,在这里先记下来。

try
{
    m_pConnection.CreateInstance(__uuidof(Connection));
    m_pConnection->Open("DSN=SQLDEV;UID=cmacgowan;PWD=cmacgowan", "", "", -1);

    m_pRecordset.CreateInstance(__uuidof(Recordset));

    m_pRecordset->Open("SELECT * FROM dbo.DICastRawData",
        m_pConnection.GetInterfacePtr(),
        adOpenDynamic,
        adLockOptimistic,
        adCmdText);


    VARIANT vFieldList[25];
    VARIANT vValueList[25];

    int nFieldIndex = 0;
    int nValueIndex = 0;

    // Setup the fields
    vFieldList[nFieldIndex].vt = VT_BSTR;
    vFieldList[nFieldIndex].bstrVal = ::SysAllocString(L"RecordId");
    nFieldIndex++;

    vFieldList[nFieldIndex].vt = VT_BSTR;
    vFieldList[nFieldIndex].bstrVal = ::SysAllocString(L"StationID");
    nFieldIndex++;

    vFieldList[nFieldIndex].vt = VT_BSTR;
    vFieldList[nFieldIndex].bstrVal = ::SysAllocString(L"Frequency");
    nFieldIndex++;

    for (i = 0; i < 3; i++)
    {
        // Setup the data
        nValueIndex = 0;
        vValueList[nValueIndex].vt = VT_UI4;
        vValueList[nValueIndex].lVal = 1000 + nValueIndex;
        nValueIndex++;

        vValueList[nValueIndex].vt = VT_BSTR;
        vValueList[nValueIndex].bstrVal = ::SysAllocString(L"KSTP");
        nFieldIndex++;

        vValueList[nValueIndex].vt = VT_R8;
        vValueList[nValueIndex].dblVal = 102.003;
        nValueIndex++;

        // Add the record to the recordset
        m_pRecordset->AddNew(vFieldList, vValueList);

    }

    m_pRecordset->UpdateBatch(adAffectAll);
    m_pRecordset->Close();

}
catch (_com_error *e)
{
    CString Error = e->ErrorMessage();
    AfxMessageBox(e->ErrorMessage());
}

标签: MFC, C++, ADO, VC, 批量插入, 批量删除

添加新评论