VC操作MSSQL,为什么我的事务不能回滚?VC版没人理,到这里求救了
我想做一个事务,代码如下
CString strOpen;
strOpen.Format(("Provider=SQLOLEDB; Data Source= %s; User ID=%s; PWD=%s"),m_strIPAddress,m_strServerName,m_strServerPass);
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((const char *)strOpen,"","",-1);
}
catch(...)
{
HRESULT rusult = LogAdoErrorImport(m_pConnection);
AfxMessageBox("数据库初始化错误!");
if(m_pConnection->GetState() != 0)
m_pConnection->Close();
}
try
{
int n = m_pConnection->BeginTrans();
CString strSQL;
strSQL.Format("Create Database Test1");//创建一个数据库
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
m_pConnection->Execute(_bstr_t(strSQL),&vtOptional,-1);
strSQL.Format("Insert into UserInfo Values(123)");//向一个不存在的表插入一条数据
m_pConnection->Execute(_bstr_t(strSQL),&vtOptional,-1);
m_pConnection->CommitTrans();
}
catch(_com_error e)
{
//AfxMessageBox("数据库初始化错误,事务将回滚!");
TRACE(_T("Warning: SetMode 发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.Description(), __FILE__, __LINE__);
HRESULT result = m_pConnection->RollbackTrans();
}
catch(...)
{
AfxMessageBox("数据库初始化错误,事务将回滚!");
TRACE(_T("未知错误,事务将回滚!"));
m_pConnection->RollbackTrans();
return;
}
我首先建了一个数据库,然后像数据库中不存在的一个表中添加一条数据。按道理应该是应该回滚的,但是我现在发现当第一条创建数据库命令Execute的时候,数据库中就有开始创建这个库了,执行回滚得时候这个库也没有删除,请问这是什么原因呢?