关于OLE DB实现分布式事务的问题。

rockrabbit 2004-06-14 06:52:39
在编写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* pTransactionJoin= NULL;
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].dwOptions=DBPROPOPTIONS_REQUIRED;
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();
};

哪位如果实现过分布式事务,最好请给一点代码看看。感谢!
...全文
98 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
rockrabbit 2004-06-16
  • 打赏
  • 举报
回复
自己解决了。谢谢各位捧场:)
njtlxm 2004-06-16
  • 打赏
  • 举报
回复
很惭愧地问一下:分布式事务是什么?
laker_tmj 2004-06-16
  • 打赏
  • 举报
回复
up
Kudeet 2004-06-15
  • 打赏
  • 举报
回复
http://www.vckbase.com/tools/findtools.asp?keyword=OLE+DB
rockrabbit 2004-06-15
  • 打赏
  • 举报
回复
那个例子和这个不太一样。
Kudeet 2004-06-15
  • 打赏
  • 举报
回复
呵呵!人气不足啊!顶起来!

4,017

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧