VS2005 写的测试程序 ADO 连接oracle 10 执行SQL语句卡住
void COracleTestDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
if ( CoInitialize( NULL ) != S_OK )
{
AfxMessageBox( _T( "初始化Com库失败!" ) );
return;
}
_ConnectionPtr a_pConnection;
_RecordsetPtr a_pRecordset;
_CommandPtr a_pCommand;
try
{
// 初始化Connection 指针
a_pConnection.CreateInstance(__uuidof(Connection));
// 初始化Recordset 指针
a_pRecordset.CreateInstance(__uuidof(Recordset));
//m_pRecordset.CreateInstance("ADODB.Recordset");
// 初始化Command 指针
a_pCommand.CreateInstance("ADODB.Command");
// 连接数据库
CString strCon = "Provider=OraOLEDB.Oracle.1; Password=admin; User ID=system; Data Source=10.10.0.172/test111; Persist Security Info=True";
a_pConnection->Open(strCon.AllocSysString(), "", "", adModeUnknown);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format(_T("数据库连接失败!\r\n错误信息:%s"), e.ErrorMessage());
AfxMessageBox(errormessage);
//CoUninitialize();
return ;
}
///////////////////////////////////////////////
try
{
CString strSql = "delete from mac_info_01";
a_pCommand.CreateInstance("ADODB.Command");
_variant_t vNULL;
vNULL.vt = VT_ERROR;
vNULL.scode = DISP_E_PARAMNOTFOUND;//定义为无参数
a_pCommand->ActiveConnection = a_pConnection;
a_pCommand->CommandText = strSql.AllocSysString();//_bstr_t(strSql);AllocSysString
a_pCommand->CommandType = adCmdText;
a_pCommand->Execute(&vNULL, &vNULL, adCmdText); // 执行到这块就无响应
return;
}
catch(_com_error e)
{
e.Description();
AfxMessageBox(e.ErrorMessage());
return;
}
}
[解决办法]
可以用ADO助手试试你的数据库连接字符串和SQL语句。
[解决办法]
试一下吧:
void COracleTestDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
if ( CoInitialize( NULL ) != S_OK )
{
AfxMessageBox( _T( "初始化Com库失败!" ) );
return;
}
_ConnectionPtr a_pConnection;
_RecordsetPtr a_pRecordset;
_variant_t vNull;
try
{
// 初始化Connection 指针
a_pConnection.CreateInstance(__uuidof(Connection));
// 初始化Recordset 指针
a_pRecordset.CreateInstance(__uuidof(Recordset));
// 连接数据库
CString strCon = "Provider=OraOLEDB.Oracle.1; Password=admin; User ID=system; Data Source=10.10.0.172/test111; Persist Security Info=True";
a_pConnection->Open(strCon.AllocSysString(), "", "", adModeUnknown);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format(_T("数据库连接失败!\r\n错误信息:%s"), e.ErrorMessage());
AfxMessageBox(errormessage);
//CoUninitialize();
return ;
}
///////////////////////////////////////////////
try
{
CString strSql = _T("delete from mac_info_01");
_bstr_t strCmd = strSql;
a_pRecordset = a_pConnection->Execute(strCmd,&vNull,adCmdText);
//a_pCommand->Execute(&vNULL, &vNULL, adCmdText); // 执行到这块就无响应
return;
}
catch(_com_error e)
{
e.Description();
AfxMessageBox(e.ErrorMessage());
return;
}
}
[解决办法]
if ( CoInitialize( NULL ) != S_OK )
这个移到外面去,不要每次按键都进行初始化