ADO技术管控C语言与Oracle进行数据交互(c oracle ado)

ADO技术管控C语言与Oracle进行数据交互

ADO(ActiveX Data Objects)是一种基于COM对象的数据访问技术,可用于通过语言无关的方式访问各种数据源。在C语言中,通过使用ADO技术,可以实现与Oracle数据库的数据交互,实现数据的查询、插入、更新和删除等操作。

要实现ADO技术与Oracle数据库的数据交互,需要在系统中安装Oracle驱动程序,例如Oracle的ODBC驱动程序。然后,需要在代码中引入头文件,并初始化ADO环境。以下是一个简单的示例代码:

#include

#include

#include

#include

#include

#include

#include

int mn()

{

WCHAR *wconstr=L”Provider=MSDAORA;Data Source=ORA9I;Password=tiger;User ID=scott”;

CoInitialize(NULL);

IDBInitialize *pIDBInitialize=NULL;

IDBCreateSession *pIDBCreateSession=NULL;

ISessionProperties *pISessionProperties=NULL;

IDBProperties *pIDBProperties=NULL;

CoCreateInstance(&CLSID_OLEDB_CONVERSIONLIBRARY, NULL, CLSCTX_INPROC_SERVER, &IID_IConvertType, (void**)&pIConvertType);

CoCreateInstance(&CLSID_OLEDB_ENUMERATOR, NULL, CLSCTX_INPROC_SERVER, &IID_IEnumGUID, (void**)&pIEnumGUID);

CoCreateInstance(&CLSID_OLEDB_ROW, NULL, CLSCTX_INPROC_SERVER, &IID_IRow, (void**)&pIRow);

CoCreateInstance(&CLSID_MSDAORA, NULL, CLSCTX_INPROC_SERVER, &IID_IDBInitialize, (void**)&pIDBInitialize);

pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void**)&pIDBCreateSession);

pIDBCreateSession->CreateSession(NULL, &IID_ISessionProperties, (IUnknown**)&pISessionProperties);

pISessionProperties->SetProperties(1, &DBPROP_AUTH_USERID, &DBPROPSET_DBINIT,

0, NULL, sizeof(WCHAR*), (void**)wconstr);

pIDBInitialize->QueryInterface(IID_IDBProperties, (void**)&pIDBProperties);

pIDBProperties->SetProperties(1, &DBPROP_USESESSIONPOOLING, &DBPROPSET_DBINIT,

0, NULL, sizeof(VARIANT_BOOL), (void*)VARIANT_FALSE);

pIDBProperties->Release();

pIDBCreateSession->Release();

pISessionProperties->Release();

pIDBInitialize->Release();

CoUninitialize();

return 0;

}

以上代码中,通过调用CoInitialize函数初始化COM库,然后调用一系列的CoCreateInstance、QueryInterface和Release函数进行初始化,最后调用CoUninitialize函数释放COM库资源。

接下来,需要创建一个ADO连接,并通过连接对象执行SQL语句进行数据的操作。以下是一个示例代码:

void test_command(IDataStore* pIDS, WCHAR* sql)

{

ICommand *pICommand=NULL;

IDBAccess *pIDBAccess=NULL;

ICommandText *pICommandText=NULL;

IAccessor *pIAccessor=NULL;

HACCESSOR hAccessor=0;

DBBINDING dbBinding;

DWORD dwBinding=1;

WCHAR buf[1024]={0};

WCHAR *pBuf=(WCHAR*)CoTaskMemAlloc(1024*sizeof(WCHAR));

pIDS->BeginTrans();

pIDS->QueryInterface(IID_ICommand, (void**)&pICommand);

pICommand->QueryInterface(IID_IColumnsInfo, (void**)&pIColumnsInfo);

pIColumnsInfo->GetColumnInfo(&dwColumns, &pColumnInfo, &pStringsBuffer);

pICommand->QueryInterface(IID_ICommandText, (void**)&pICommandText);

pICommandText->SetCommandText(DBGUID_DBSQL, sql);

pICommandText->Execute(NULL, &IID_IAccessor, NULL, &rowcount, (void**)&pIAccessor);

dbBinding.iOrdinal=1;

dbBinding.dwPart=DBPART_VALUE | DBPART_LENGTH | DBPART_STATUS;

dbBinding.eParamIO=DBPARAMIO_OUTPUT;

dbBinding.dwMemOwner=DBMEMOWNER_PROVIDEROWNED;

dbBinding.pTypeInfo=NULL;

dbBinding.pObject=NULL;

dbBinding.pData=&buf[0];

dbBinding.cbMaxLen=SIZE_WCHAR;

dbBinding.cbMaxLen=SIZE_WCHAR * 256;

dbBinding.cbMaxLen=255;

dbBinding.cbMaxLen=256;

dbBinding.cbMaxLen=0;

pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA, 1, &dbBinding, 0, &hAccessor, NULL);

while (S_OK == pIAccessor->FetchData(hAccessor, &rowcount, NULL))

{

memset(pBuf, 0, 1024*sizeof(WCHAR));

memcpy(pBuf, pColumnData, pColumnInfo[0].ulColumnSize);

wprintf(L”%s\n”, pBuf);

}

pIAccessor->ReleaseAccessor(hAccessor, NULL);

pIAccessor->Release();

pICommandText->Release();

pICommand->Release();

pIDS->CommitTrans();

CoTaskMemFree(pBuf);

}

以上代码中,通过调用IDataStore对象的BeginTrans方法开启一个数据库事务,通过创建ADO连接对象,并调用QueryInterface方法获取ICommand对象和ICommandText对象,通过调用ICommandText的SetCommandText方法设置SQL语句,最后通过调用ICommandText的Execute方法执行SQL语句,获取IAccessor对象。通过IAccessor对象创建数据访问器,并通过调用IAccessor对象的FetchData方法获取数据,并将数据输出到控制台上。

综上所述,通过ADO技术,可以实现C语言与Oracle数据库进行数据交互的功能。需要注意的是,需要在代码中引入头文件,并进行必要的初始化,包括COM库的初始化和ADO连接的创建。同时,需要根据具体的业务需求,编写相应的SQL语句,并处理异常情况,以保证程序的稳定性和安全性。


数据运维技术 » ADO技术管控C语言与Oracle进行数据交互(c oracle ado)