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;
}