使用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;
}
uj5u.com熱心網友回復:
//第一步連接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();
uj5u.com熱心網友回復:
ODBC CDatabaseuj5u.com熱心網友回復:
書本上有錯誤,導致無法運行成功,校對不嚴的書本害人不淺uj5u.com熱心網友回復:
C0000005 訪問非法地址, 每一步的指標狀態都應該檢測是否有效uj5u.com熱心網友回復:
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
uj5u.com熱心網友回復:
應該是 m_pRecordset 沒有被初始化, 你單步除錯的時候看看它里面的物件指標是不是為 0x00000000 或 0xcccccccc 或其它無效地址轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/37101.html
標籤:數據庫
