用c++写一个通过ado更新blob字段的代码段
时间: 2024-05-09 22:16:44 浏览: 10
好的,以下是用ADO更新BLOB字段的C代码:
```
#include <windows.h>
#include <oledb.h>
#include <stdio.h>
void update_blob_field()
{
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
// Connection string for your database
char connStr[256] = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\myDatabase.mdb;Persist Security Info=False;";
// Initialize ADO objects
IDBInitialize *pInitialize = NULL;
IDBCreateSession *pCreateSession = NULL;
IDBCreateCommand *pCreateCommand = NULL;
hr = CoCreateInstance(CLSID_MSDASQL, NULL, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (void**)&pInitialize);
if (SUCCEEDED(hr))
{
// Set DB initialization properties
hr = pInitialize->Initialize();
if (SUCCEEDED(hr))
{
// Create session
hr = pInitialize->QueryInterface(IID_IDBCreateSession, (void**)&pCreateSession);
if (SUCCEEDED(hr))
{
DBPROP rgProps[1];
rgProps[0].dwPropertyID = DBPROP_AUTH_USERID;
rgProps[0].dwOptions = DBPROPOPTIONS_REQUIRED;
rgProps[0].colid = DB_NULLID;
rgProps[0].vValue.vt = VT_BSTR;
rgProps[0].vValue.bstrVal = SysAllocString("myUsername");
// Connect to database
IDBSession *pSession = NULL;
hr = pCreateSession->CreateSession(NULL, IID_IDBSession, (IUnknown**)&pSession);
if (SUCCEEDED(hr))
{
hr = pSession->SetConnectionProperties(1, rgProps);
// Create command
pSession->QueryInterface(IID_IDBCreateCommand, (void**)&pCreateCommand);
// Issue update command for BLOB field
char updateSQL[256] = "UPDATE myTable SET myBlobField = ? WHERE myID = 1";
ICommandText *pCommandText = NULL;
hr = pCreateCommand->CreateCommand(NULL, IID_ICommandText, (IUnknown**)&pCommandText);
if (SUCCEEDED(hr))
{
hr = pCommandText->SetCommandText(DBGUID_DBSQL, updateSQL);
if (SUCCEEDED(hr))
{
// Set up parameter object for BLOB field
IColumnsInfo *pColumnsInfo = NULL;
pCommandText->QueryInterface(IID_IColumnsInfo, (void**)&pColumnsInfo);
DBCOLUMNINFO *pColumnInfo = (DBCOLUMNINFO*)malloc(sizeof(DBCOLUMNINFO));
pColumnsInfo->GetColumnInfo(1, pColumnInfo, NULL, NULL, NULL, NULL);
IAccessor *pIAccessor = NULL;
pSession->QueryInterface(IID_IAccessor, (void**)&pIAccessor);
HACCESSOR hAccessor;
pIAccessor->CreateAccessor(DBACCESSOR_PARAMETERDATA, 1, &pColumnInfo->columnid, sizeof(DBPARAMETER), &hAccessor, NULL);
DBPARAMETER param;
param.dwParameterID = pColumnInfo->columnid;
param.dwFlags = 0;
param.iid = IID_NULL;
param.pTypeInfo = NULL;
param.pObject = NULL;
param.pwszName = NULL;
param.pwszDataSourceType = NULL;
// Allocate a buffer for our BLOB data, and fill it with our desired data
BYTE *pBlobData = (BYTE*)malloc(100000);
// (In practice, you might want to read this data from a file or some other source)
for (int i = 0; i < 100000; i++)
{
pBlobData[i] = (BYTE)(i % 256);
}
// Set value for BLOB field
DBBINDING binding;
binding.iOrdinal = 1;
binding.dwPart = DBPART_VALUE | DBPART_STATUS | DBPART_LENGTH;
binding.eParamIO = DBPARAMIO_INPUT;
binding.dwMemOwner = DBMEMOWNER_CLIENTOWNED;
binding.dwFlags = 0;
binding.cbMaxLen = 100000;
binding.cbMaxLen = 100000;
binding.wType = DBTYPE_IUNKNOWN;
binding.obStatus = 0;
binding.obLength = 0;
binding.obValue = 0;
binding.pTypeInfo = NULL;
binding.pObject = NULL;
binding.pBindExt = NULL;
binding.pDataSourceType = NULL;
binding.pBindExt = NULL;
binding.pObject = NULL;
binding.dwReserved = 0;
DBPARAMBINDING paramBinding;
paramBinding.dwParamOrdinal = 1;
paramBinding.pwszDataSourceType = NULL;
paramBinding.dwFlags = 0;
paramBinding.pwszName = NULL;
paramBinding.iOrdinal = 1;
DB_UPARAMS cParams = 1;
DBPARAMINFO *rgParamInfo = NULL;
DBCOUNTITEM cParamSets = 1;
HROWSET hRowset;
IRowsetChange *pRowset = NULL;
IDBCreateCommand *pCommand = NULL;
DBROWCOUNT cRowsAffected = 0;
pCommandText->Execute(NULL, IID_IRowsetChange, NULL, &cRowsAffected, (IUnknown**)&pRowset);
pCommandText->GetParameterInfo(&cParams, &rgParamInfo);
rgParamInfo[0].cbMaxLen = 100000;
rgParamInfo[0].dwFlags = 0;
paramBinding.cbMaxLen = rgParamInfo[0].cbMaxLen;
binding.cbMaxLen = rgParamInfo[0].cbMaxLen;
IRowsetChange *pRowsetChange = NULL;
pCommandText->QueryInterface(IID_IRowsetChange, (void**)&pRowsetChange);
HROW hRow;
pRowset->InsertRow(NULL, hAccessor, pBlobData, &hRow);
IRowsetChange *pIRowsetChange = NULL;
pCommandText->QueryInterface(IID_IRowsetChange, (void**)&pIRowsetChange);
pIRowsetChange->SetData(hRow, hAccessor, pBlobData);
// Commit transaction
ITransactionLocal *pTransaction = NULL;
pSession->QueryInterface(IID_ITransactionLocal, (void**)&pTransaction);
DBPARAMS params;
params.cParamSets = cParamSets;
params.hAccessor = hAccessor;
params.pData = pBlobData;
params.rgParamIO = NULL;
params.cbParamIO = 0;
params.pVarData = NULL;
params.rghCursors = NULL;
params.rgRowStatus = NULL;
params.hChapter = 0;
params.cParamBindings = 1;
params.rgParamBindings = ¶mBinding;
hr = pTransaction->StartTransaction(ISOLATIONLEVEL_READCOMMITTED,ISOLEVEL_CHAOS,0,NULL);
hr = pRowsetChange->SetData(hRow, hAccessor, pBlobData);
pRowsetChange->Update(hRow, NULL, NULL, &cRowsAffected);
hr = pTransaction->Commit(FALSE, XACTTC_ASYNC_PHASEONE, 0);
if (!SUCCEEDED(hr)) {
printf("Could not commit transaction!");
}
// Clean up ADO objects
pCommand->Release();
pRowsetChange->Release();
pRowset->Release();
pIRowsetChange->Release();
pIAccessor->Release();
pCommandText->Release();
pColumnsInfo->Release();
CoFreeUnusedLibraries();
CoUninitialize();
free(pColumnInfo);
free(pBlobData);
}
}
}
}
}
}
}
}
```
请注意,这段代码在编写时并未测试。一些细节可能需要调整和完善,以确保它按您预期的方式运行。