ado 读取数据库open()崩溃 本帖最后由 VisualEleven 于 2013-10-10 09:33:39 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 m_pConnection,这个值无效吧,你设断点试试 _ConnectionPtr m_pConnection;_RecordsetPtr m_pRecordset;try{m_pRecordset.CreateInstance(___uuidof("ADODB.Recordset"));m_pRecordset->Open(_bstr_t("SELECT * from db_1"),(IUnknown*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);//在这就崩溃了,调试报错连接无法执行此操作,在此上下文中它可能已被关闭或无效0x800a0e7d}catch(_com_error& e) { MessageBox(e.Description(), e.ErrorMessage()); } m_pRecordset创建成功了吗?看这些似乎是create就失败了 有可能没有初始化OLE,你在程序初始化函数里加上:AfxInitOle();好象是这么写的 你m_pConnection打开数据库了吗?没看到你的数据打开啊? 大神 上面的问题解决了 但是CString var;var = m_pRecordset->GetCollect("xinghao");//xinghao是数据库字段,程序崩溃报错无法检查的错误m_Edit1 = var; //m_Edit是字符变量 这个怎么解决呀?我的editcontrol控件设置的是cstring类型的, 而且___uuidof 总报无法找到错误 #import "C:\Program Files\Common Files\System\ado\msado15.dll" rename("EOF", "EndOfFile")这个导入了吗? 将第二句改成var=(char*)(_bstr_t)m_pRecordset->GetCollect("xinghao");这之间存在着变量的转换问题,直接读取出来的记录并不是CString类型 还是没有解决 m_pRecordset->Open("SELECT * from db_1",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText 这句到底哪里错了 晕死,我第一步就是导入com组件,并初始化,连接字符串也没看出哪里有问题 var = m_pRecordset->GetCollect(_bstr_t("xinghao")1);注意com组件一般只接受BSTR及其VARIANT数据类型 晕死,我第一步就是导入com组件,并初始化,连接字符串也没看出哪里有问题老兄,我不是给你正确的写法了,干嘛不试试1?大哥 在open那步就崩溃了 根本走不到你这步啊 m_pRecordset->Open("SELECT * from db_1",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);//这里崩溃吧因为你的m_pConnection就没有创建和openm_pCnnection.createinstance(....)m_pConnection->open(....) m_pRecordset->Open(_bstr_t("SELECT * from db_1"),(IUnknown*)m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); 求大神 解答 这句话怎么会崩溃呢 m_pConnection能被使用之前必须这样(将你的定义去掉)_ConnectionPtr m_pConnection(__uuidof(Connection));m_pConnection->Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Data\Northwind\NORTHWINDA.MDB;Persist Security Info=False"),_bstr_t(""),_bstr_t(""),-1);红色部分换成你自己的数据库,如果不是access,那么你自己写连接串 不太可能,程序开始的时候,有没有调用CoInitilize?把完整代码发上来看看 我在ado类中调用 CoInitilize 了 然后 ado lei; lei.OnInitilize();所以应该初始化了呀完整代码新建ado类代码BOOL CADO::OnInitADOConn(void){ HRESULT hr; //创建对象 try { hr = m_pConnection.CreateInstance("ADODB.Connection"); if (SUCCEEDED(hr)) //创建成功 { hr = m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Database3.accdb;","","",adModeUnknown); AfxMessageBox(_T("连接数据库成功!")); } } catch(_com_error& e) { MessageBox(e.Description(), e.ErrorMessage());///显示错误信息 return FALSE; } return TRUE;}_RecordsetPtr& CADO::OpenRecordset(CString sql){ //TODO: insert return statement here ASSERT(!sql.IsEmpty()); try { m_pRecordset.CreateInstance("ADODB.Recordset"); if (m_pRecordset == NULL) { AfxMessageBox(_T("RecordSet 对象创建失败! 请确认是否初始化了COM环境.")); } m_pRecordset->CursorLocation = adUseClient; m_pRecordset->Open(_variant_t(sql),_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText); } catch(_com_error e) { CString errorMsg; errorMsg.Format(_T("连接数据库失败!\r\n错误信息:%s"),e.ErrorMessage()); AfxMessageBox(errorMsg); //显示错误信息 } return m_pRecordset;}void CADO::ExitConnection(void){ if(m_pRecordset->GetState() == adStateOpen) { m_pRecordset->Close(); m_pRecordset = NULL; } if (m_pConnection->State) { m_pConnection->Close(); m_pConnection = NULL; }}_RecordsetPtr& CADO::GetRecordset(void){ //TODO: insert return statement here return m_pRecordset;}DLG.cpp代码lei.OnInitADOConn(); _ConnectionPtr m_pConnection(__uuidof(Connection)); _RecordsetPtr m_pRecordset; m_pRecordset.CreateInstance("ADODB.Recordset"); try{ m_pRecordset->Open(_bstr_t("SELECT * from db_1"),(IUnknown*)m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); if (m_pRecordset == NULL) { MessageBox(_T("RecordSet 对象创建失败! 请确认是否初始化了COM环境.")); } } catch(_com_error& e) { MessageBox(e.Description(), e.ErrorMessage()); } _variant_t var; m_Edit1 = (char *)(_bstr_t)m_pRecordset->GetCollect("xinghao"); UpdateData(FALSE); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE} ado lei这个是什么?不懂 ((IDispatch *)m_pConnection,这个似乎有问题,应该是(IUnknown *)m_pConnection 既然你创建了全局的连接对象,在函数内就不应该再定义lei.OnInitADOConn();_ConnectionPtr m_pConnection(__uuidof(Connection)); 不再定义lei 实例 的话 怎么调用初始化方法呢 ???不再定义lei 实例 的话 怎么调用初始化方法呢 m_pRecordset->Open( "SELECT * from db_1",_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);这句话有什么问题 为什么打开失败 我是菜鸟,想请高手推荐几本学习VC++的收!!! 如何删除其它工程的资源? 如何在对话框上贴一个web方式的连接 dll中使用对话框类的问题 请大家进来说说MSN拖动联系人出现的背景是怎么做出来的?进来就有分啊! 添加菜单的问题 如何启动屏幕保护(在线等待) 会者不难 Cpoint怎么用? 关于debug的问题 在工具栏上创建checkbox,如何响应该控件 为什么这个Cmenu的按钮不能点
_RecordsetPtr m_pRecordset;
try{
m_pRecordset.CreateInstance(___uuidof("ADODB.Recordset"));m_pRecordset->Open(_bstr_t("SELECT * from db_1"),(IUnknown*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);//在这就崩溃了,调试报错连接无法执行此操作,在此上下文中它可能已被关闭或无效0x800a0e7d}
catch(_com_error& e) { MessageBox(e.Description(), e.ErrorMessage()); }
CString var;
var = m_pRecordset->GetCollect("xinghao");//xinghao是数据库字段,程序崩溃报错无法检查的错误
m_Edit1 = var; //m_Edit是字符变量
这个怎么解决呀?我的editcontrol控件设置的是cstring类型的,
这个导入了吗?
这之间存在着变量的转换问题,直接读取出来的记录并不是CString类型
m_pRecordset->Open("SELECT * from db_1",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText 这句到底哪里错了
老兄,我不是给你正确的写法了,干嘛不试试1?
大哥 在open那步就崩溃了 根本走不到你这步啊
因为你的m_pConnection就没有创建和openm_pCnnection.createinstance(....)
m_pConnection->open(....)
m_pConnection->Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Data\Northwind\NORTHWINDA.MDB;Persist Security Info=False"),_bstr_t(""),_bstr_t(""),-1);红色部分换成你自己的数据库,如果不是access,那么你自己写连接串
把完整代码发上来看看
完整代码
新建ado类代码
BOOL CADO::OnInitADOConn(void)
{
HRESULT hr; //创建对象
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");
if (SUCCEEDED(hr)) //创建成功
{
hr = m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Database3.accdb;","","",adModeUnknown);
AfxMessageBox(_T("连接数据库成功!"));
}
}
catch(_com_error& e)
{
MessageBox(e.Description(), e.ErrorMessage());///显示错误信息
return FALSE;
}
return TRUE;
}
_RecordsetPtr& CADO::OpenRecordset(CString sql)
{
//TODO: insert return statement here
ASSERT(!sql.IsEmpty());
try
{
m_pRecordset.CreateInstance("ADODB.Recordset"); if (m_pRecordset == NULL)
{
AfxMessageBox(_T("RecordSet 对象创建失败! 请确认是否初始化了COM环境."));
}
m_pRecordset->CursorLocation = adUseClient;
m_pRecordset->Open(_variant_t(sql),_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
CString errorMsg;
errorMsg.Format(_T("连接数据库失败!\r\n错误信息:%s"),e.ErrorMessage());
AfxMessageBox(errorMsg); //显示错误信息
}
return m_pRecordset;
}void CADO::ExitConnection(void)
{
if(m_pRecordset->GetState() == adStateOpen)
{
m_pRecordset->Close();
m_pRecordset = NULL;
}
if (m_pConnection->State)
{
m_pConnection->Close();
m_pConnection = NULL;
}
}
_RecordsetPtr& CADO::GetRecordset(void)
{
//TODO: insert return statement here
return m_pRecordset;
}
DLG.cpp代码
lei.OnInitADOConn();
_ConnectionPtr m_pConnection(__uuidof(Connection)); _RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset");
try{
m_pRecordset->Open(_bstr_t("SELECT * from db_1"),(IUnknown*)m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
if (m_pRecordset == NULL)
{
MessageBox(_T("RecordSet 对象创建失败! 请确认是否初始化了COM环境."));
} }
catch(_com_error& e) { MessageBox(e.Description(), e.ErrorMessage()); }
_variant_t var;
m_Edit1 = (char *)(_bstr_t)m_pRecordset->GetCollect("xinghao");
UpdateData(FALSE);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
这个似乎有问题,应该是
(IUnknown *)m_pConnection
lei.OnInitADOConn();_ConnectionPtr m_pConnection(__uuidof(Connection));