vc连接sql数据库总是出现这样的错误,困扰一个多月了,求大神指点!

时尚猎人 2019-06-16 03:35:15
使用VC6。0连接SQL SERVER2008,按照书本依样画葫芦写了编一段编码,总是出现这样的错误 First-chance exception in HrSys.exe: 0xC0000005: Access Violation. 经单步调试到这儿m_pRecordset>Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); 发现问题指向这里 V_DISPATCH(this)->AddRef(); inline _variant_t::_variant_t(IDispatch* pSrc, bool fAddRef) throw() { V_VT(this) = VT_DISPATCH; V_DISPATCH(this) = pSrc; // Need the AddRef() as VariantClear() calls Release(), unless fAddRef // false indicates we're taking ownership // if (fAddRef) { V_DISPATCH(this)->AddRef(); } } 请大神指点这是什么问题,如何解决? 这是部分重要编码: 1、void CLoginDlg::OnOK() { UpdateData(TRUE); if(m_UserName=="") { MessageBox("请输入用户名"); return; } if(m_Pwd=="") { MessageBox("请输入密码"); return; } CUsers user; user.GetData(m_UserName); if(user.GetPwd()!=m_Pwd) { MessageBox("用户信息不正确,无法登录!"); return; } CDialog::OnOK(); } 2、BOOL CHrSysDlg::OnInitDialog() { CDialog::OnInitDialog(); m_adodc.SetRecordSource("SELECT EmpId,UserName As 用户名,UserPwd As 密码 FROM Users"); m_adodc.Refresh(); _variant_t vIndex; vIndex=long(0); m_datagrid.GetColumns ().GetItem(vIndex).SetWidth (0); vIndex=long(1); m_datagrid.GetColumns ().GetItem(vIndex).SetWidth (180); vIndex=long(2); m_datagrid.GetColumns ().GetItem(vIndex).SetWidth (140); CLoginDlg dlg; if(dlg.DoModal()!=IDOK) OnOK(); else curUser.GetData(dlg.m_UserName); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE);// Set big icon SetIcon(m_hIcon, FALSE);// Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } 3、 void CUsers::GetData(CString cUserName) { ADOConn m_AdoConn; m_AdoConn.OnInitADOConn(); _bstr_t vSQL; vSQL="SELECT *FROM Users WHERE UserName="+cUserName; _RecordsetPtr m_pRecordset; m_pRecordset=m_AdoConn.GetRecordSet (vSQL); if(m_pRecordset->adoEOF==1) CUsers(); else { UserName=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("UserName"); EmpId=atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("EmpId")); UserPwd=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("UserPwd"); UserType=atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("UserType")); } m_AdoConn.ExitConnect(); } 4、void ADOConn::OnInitADOConn() { ::CoInitialize(NULL); try { m_pConnection.CreateInstance("ADODB.Recordset"); _bstr_t strConnect="Provider=SQLOLEDB;Server=MSSQLSERVER1;Database=人事工资管理系统;uid=sa;pwd=sa;"; m_pConnection->Open(strConnect,"","",adModeUnknown); } catch(_com_error e) { AfxMessageBox(e.Description ()); } } 5、 _RecordsetPtr& ADOConn::GetRecordSet (_bstr_t bstrSQL) { try { if(m_pConnection==NULL) OnInitADOConn(); m_pRecordset.CreateInstance(__uuidof(Recordset)); m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); } catch(_com_error e) { AfxMessageBox(e.Description()); } return m_pRecordset; }
...全文
277 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sandrer 2019-09-06
  • 打赏
  • 举报
回复
应该是 m_pRecordset 没有被初始化, 你单步调试的时候看看它里面的对象指针是不是为 0x00000000 或 0xcccccccc 或其它无效地址
yangkunhenry 2019-09-05
  • 打赏
  • 举报
回复
https://blog.csdn.net/yangkunhenry/article/details/100526024 做如下设置即可解决: 1、菜单Tools->Options中的Debug,Disassembly window下的Source annotation取消 2、保存设置,然后即可单步调试 3、再将Tools->Options中的Debug,Disassembly window下的Source annotation勾上(这步可做可不做) ———————————————— 版权声明:本文为CSDN博主「yangkunhenry」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/yangkunhenry/article/details/100526024
zgl7903 2019-07-01
  • 打赏
  • 举报
回复
C0000005 访问非法地址, 每一步的指针状态都应该检测是否有效
时尚猎人 2019-07-01
  • 打赏
  • 举报
回复
书本上有错误,导致无法运行成功,校对不严的书本害人不浅
rongewu2013 2019-06-27
  • 打赏
  • 举报
回复
ODBC CDatabase
ITER2012 2019-06-23
  • 打赏
  • 举报
回复
//第一步连接 CoInitialize(NULL); _ConnectionPtr m_pConnection; _CommandPtr pCommand; m_pConnection.CreateInstance(__uuidof(Connection)); m_pConnection->Open(_bstr_t(connString),"","",adModeUnknown); pCommand.CreateInstance (__uuidof (Command)); pCommand->ActiveConnection = m_pConnection; //第二步打开 _RecordsetPtr pRecordset; pCommand->CommandText = (_bstr_t) str_Query; pRecordset.CreateInstance (__uuidof (Recordset)); pRecordset->CursorLocation = adUseClient; pRecordset->Open((IDispatch *) pCommand, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnknown); //第三步插入数据 pRecordset->AddNew(); pRecordset->PutCollect((_variant_t)field_name, (_variant_t)fieldvalue) ... ... pRecordset->Update(); pRecordset->Close(); //第四部关闭 m_pConnection->Close();

4,011

社区成员

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

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