Home > Software engineering >  Vc SQL database connection is always appear such mistakes, more than a month, strives for the great
Vc SQL database connection is always appear such mistakes, more than a month, strives for the great

Time:09-15

Using VC6 0 connection SQL SERVER2008, according to the book still wrote make a coding, always appear such mistakes
First - chance exception in HrSys. Exe: 0 xc0000005: Access Violation.
The single step debugging here m_pRecordset & gt; Open (bstrSQL, m_pConnection GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adCmdText);
Found the problem pointed V_DISPATCH (this) - & gt; AddRef ();
The inline _variant_t: : _variant_t (pSrc IDispatch * and bool fAddRef) throw ()
{
=VT_DISPATCH V_VT (this);
=pSrc V_DISPATCH (this);
//Need the AddRef () as VariantClear () calls the Release (), unless fAddRef
//false are we 'r e taking ownership
//
If (fAddRef) {
V_DISPATCH (this) - & gt; AddRef ();
}
}
Great god, please advise what's the problem, how to solve?
This is part of important code:
1, void CLoginDlg: : OnOK ()
{
The UpdateData (TRUE);
If (m_UserName=="")
{
MessageBox (" please enter the user name ");
return;
}
If (m_Pwd=="")
{
MessageBox (" please enter the password ");
return;
}
CUsers user;
User. GetData (m_UserName);
If (user. GetPwd ()!=m_Pwd)
{
MessageBox (" user information is not correct, unable to login!" );
return;
}
CDialog: : OnOK ();
}
2, the BOOL CHrSysDlg: : OnInitDialog ()
{
CDialog: : OnInitDialog ();
M_adodc. SetRecordSource (" SELECT EmpId, UserName As the user name, UserPwd As password FROM the Users ");
M_adodc. Refresh ();
_variant_t vIndex;
VIndex=long (0);
M_datagrid. GetColumns (). The GetItem (vIndex). SetWidth (0);
VIndex=long (1);
M_datagrid. GetColumns (). The GetItem (vIndex). SetWidth (180);
VIndex=long (2);
M_datagrid. GetColumns (). The GetItem (vIndex). SetWidth (140);
CLoginDlg DLG.
If (DLG. DoModal ()!=IDOK)
OnOK ();
The 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 & amp; 0==IDM_ABOUTBOX xfff0));
ASSERT (IDM_ABOUTBOX & lt; 0 xf000);
CMenu * pSysMenu=GetSystemMenu (FALSE);
If (pSysMenu!=NULL)
{
Cstrings strAboutMenu;
StrAboutMenu. LoadString (IDS_ABOUTBOX);
if (! StrAboutMenu. IsEmpty ())
{
PSysMenu - & gt; The AppendMenu (MF_SEPARATOR);
PSysMenu - & gt; 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 the big icon
SetIcon (m_hIcon, FALSE);//Set the small icon
//TODO: Add extra initialization here
return TRUE;//return TRUE unless you set the focus to a control
}
3,
Void CUsers: : GetData (cstrings cUserName)
{
ADOConn m_AdoConn;
M_AdoConn. OnInitADOConn ();
_bstr_t vSQL;
VSQL="SELECT * FROM the Users WHERE the UserName=" + cUserName;
_RecordsetPtr m_pRecordset;
M_pRecordset=m_AdoConn. GetRecordSet (vSQL);
If (m_pRecordset - & gt; AdoEOF==1)
CUsers ();
The else
{
The UserName=(LPCTSTR) (_bstr_t) m_pRecordset - & gt; GetCollect (" UserName ");
EmpId=atoi ((LPCTSTR) (_bstr_t) m_pRecordset - & gt; GetCollect (" EmpId "));
UserPwd=(LPCTSTR) (_bstr_t) m_pRecordset - & gt; GetCollect (" UserPwd ");
UserType=atoi ((LPCTSTR) (_bstr_t) m_pRecordset - & gt; GetCollect (" UserType "));
}
M_AdoConn. ExitConnect ();
}
4, void ADOConn: : OnInitADOConn ()
{
: : CoInitialize (NULL);
Try
{
M_pConnection. CreateInstance (" ADODB. You ");
_bstr_t strConnect="Provider=SQLOLEDB; Server=MSSQLSERVER1; The Database=the personnel salary management system; Uid=sa; The PWD=sa; ";
M_pConnection - & gt; Open (strConnect, ""," ", adModeUnknown);
}
Catch _com_error (e)
{
AfxMessageBox (e.D escription ());
}
}
5,
_RecordsetPtr & amp; ADOConn: : GetRecordSet (_bstr_t bstrSQL)
{
Try
{
If (m_pConnection==NULL)
OnInitADOConn ();
M_pRecordset. CreateInstance (__uuidof (you));
M_pRecordset - & gt; Open (bstrSQL, m_pConnection GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adCmdText);
}
Catch _com_error (e)
{
AfxMessageBox (e.D escription ());
}
Return m_pRecordset;
}

CodePudding user response:

The first step in//connection
CoInitialize (NULL);
_ConnectionPtr m_pConnection;
_CommandPtr pCommand;

M_pConnection. CreateInstance (__uuidof (Connection));

M_pConnection - & gt; Open (_bstr_t (connString), ""," ", adModeUnknown);

PCommand. CreateInstance (__uuidof (Command));
PCommand - & gt; ActiveConnection=m_pConnection;


//the second step is to open the
_RecordsetPtr pRecordset;
PCommand - & gt; CommandText=(_bstr_t) str_Query;
PRecordset. CreateInstance (__uuidof (you));
PRecordset - & gt; CursorLocation=adUseClient;
PRecordset - & gt; Open ((IDispatch *) pCommand,
VtMissing,
AdOpenStatic,
AdLockOptimistic,
AdCmdUnknown);



//the third insert data
PRecordset - & gt; AddNew ();
PRecordset - & gt; PutCollect (field_name (_variant_t), (_variant_t) fieldvalue)
.
.
PRecordset - & gt; The Update ();
PRecordset - & gt; Close ();


//the fourth closing
M_pConnection - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related