描述:
在编写OLE DB实现分布式事务的代码中,使用了
DtcGetTransactionManager函数。
在VC++编辑时,可以看到函数的参数提示。
可是,编译的时候却提示该函数是未定义的表识符。请问,是什么原因?
是不是漏掉了什么#include库?
新手,初学用SDK编程,见笑见笑!
代码如下:
//*********************************************************
//使用OLE DB API控制分布式事务的例子
//运行本示例要求用户机器上安装有MSDAC SDK
//此示例假定本机已经有数据源MyOrder
//,该数据源连接到SQL Server数据库Northwind
//*********************************************************
#define UNICODE
#define _UNICODE
#define DBINITCONSTANTS
#define INITGUID
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <stddef.h>
#include <iostream.h>
#include <oledb.h>
#include <oledberr.h>
#include <msdaguid.h>
#include <msdasql.h>
void main()
{
IDBInitialize *pIDBInitialize = NULL;
IDBCreateSession* pCreateSession = NULL;
IDBCreateCommand* pCreateCommand = NULL;
IRowset* pRowset = NULL;
ICommandText* pCommandText = NULL;
ITransaction* pTransaction = NULL;
ITransactionJoin* pTransacti />
ITransactionDispenser* pTransactiondispenser = NULL;
IDBProperties* pIDBProperties;
DBPROP InitProperties[4];
DBPROPSET rgInitPropSet[1];
int i;
LONG cNumRows;
bool bHasErr = FALSE;
//要执行的命令
LPCTSTR wCmdString=OLESTR("SELECT EmployeeID,Title FROM EMPLOYEES");
LPCTSTR wSQL=OLESTR("Update Employees Set Title='AD Vice President' WHERE
EmployeeID=6");
CoInitialize(NULL);
CoCreateInstance(CLSID_MSDASQL,NULL,CLSCTX_INPROC_SERVER,
IID_IDBInitialize,(void**) &pIDBInitialize);
for(i=0;i<4;i++)
{
VariantInit(&InitProperties[i].vValue);
InitProperties[i].dwOpti />
InitProperties[i].colid=DB_NULLID;
}
//level of Prompt
InitProperties[0].dwPropertyID=DBPROP_INIT_PROMPT;
InitProperties[0].vValue.vt=VT_I2;
InitProperties[0].vValue.iVal=DBPROMPT_NOPROMPT;
//UserName
InitProperties[1].dwPropertyID=DBPROP_AUTH_USERID;
InitProperties[1].vValue.vt=VT_BSTR;
InitProperties[1].vValue.bstrVal=SysAllocString((LPOLESTR)L"sa");
//password
InitProperties[2].dwPropertyID=DBPROP_AUTH_PASSWORD;
InitProperties[2].vValue.vt=VT_BSTR;
InitProperties[2].vValue.bstrVal=SysAllocString((LPOLESTR)L"364018");
//数据源名称
InitProperties[3].dwPropertyID=DBPROP_INIT_DATASOURCE;
InitProperties[3].vValue.vt=VT_BSTR;
InitProperties[3].vValue.bstrVal=SysAllocString((LPOLESTR)L"MyOrder");
rgInitPropSet[0].guidPropertySet=DBPROPSET_DBINIT;
rgInitPropSet[0].cProperties=4;
rgInitPropSet[0].rgProperties=InitProperties;
//设置初始化属性
pIDBInitialize->QueryInterface(IID_IDBProperties,
(void**)&pIDBProperties);
pIDBProperties->SetProperties(1,rgInitPropSet);
pIDBProperties->Release();
//连接到数据源
HRESULT hr=pIDBInitialize->Initialize();
pIDBInitialize->QueryInterface(IID_IDBCreateSession,(void
**)&pCreateSession);
pCreateSession->CreateSession(NULL,IID_IDBCreateCommand,(IUnknown**)&pCreate
Command);
hr=DtcGetTransactionManager(NULL,NULL,IID_ITransactionDispenser,0,0,NULL
,(void**)&pTransactionDispenser);//====>>>这一句编译出错了
//从Command得到ITransaction
pCreateCommand->QueryInterface(IID_ITransactionLocal,(void**)&pTransaction);
//启动本地事务
hr=((ITransactionLocal*)pTransaction)->StartTransaction(
ISOLATIONLEVEL_REPEATABLEREAD,0,NULL,NULL);
if SUCCEEDED(hr)
{
::printf("启动事务成功!");
}
pCreateCommand->CreateCommand(NULL,IID_ICommandText,(IUnknown
**)&pCommandText);
//执行UPDATE命令
pCommandText->SetCommandText(DBGUID_DBSQL,wSQL);
hr=pCommandText->Execute(NULL,IID_IRowset,NULL,&cNumRows,
(IUnknown**)&pRowset);
if FAILED(hr)
bHasErr=TRUE;
else
printf("\n执行SQL:%S",wSQL);
//执行SELECT命令
pCommandText->SetCommandText(DBGUID_DBSQL,wCmdString);
hr=pCommandText->Execute(NULL,IID_IRowset,NULL,&cNumRows,
(IUnknown**)&pRowset);
if FAILED(hr)
bHasErr=TRUE;
else
printf("\n执行SQL:%S",wCmdString);
if(bHasErr)
{
pTransaction->Abort(NULL,FALSE,FALSE);
printf("\n事务回滚!\n");
}
else
{
pTransaction->Commit(FALSE,XACTTC_SYNC,0);
printf("\n事务提交!\n");
};
//清理内存
pTransaction->Release();
pRowset->Release();
pCommandText->Release();
pCreateCommand->Release();
pCreateSession->Release();
pIDBInitialize->Uninitialize();
pIDBInitialize->Release();
CoUninitialize();
};
哪位如果实现过分布式事务,最好请给一点代码看看。感谢!
解决方案1:
用ADO吧。
ADO有 BeginTrans
CommitTrans
http://www.vckbase.com/tools/findtools.asp?keyword=OLE+DB
解决方案3:COM+ IS THE FIRST CHOICE