Home > Software engineering >  To validate the MFC using ADO to connect Access realize MFC list and database synchronization delete
To validate the MFC using ADO to connect Access realize MFC list and database synchronization delete

Time:09-30

Are learning MFC, to make a simple inventory information system, using ADO to connect the Access, increase and change of these functions are realized the function of list and database synchronization update, but the implementation delete tried several ways are not, when I interface has a "save" button, want to click on the save button, the increase in a list control deletion operation real-time updates to the database, please list when deleting a row and then update to the database at the same time method (delete an operation I) in the list in the list, I hope you tell experts, the following code
Void CUserInfoDlg: : OnBnClickedSave ()
{
Int nCount=m_list. GetItemCount ();
Cstrings sNumb, sName, sSex, sAge, sDept, sLevel, sPhone, sEmail, sFlag, sID.
Cstrings sSql;
for(int i=0; I{
SNumb=m_list. GetItemText (I, 0).
SName=m_list. GetItemText (I, 1);
SSex=m_list. GetItemText (I, 2);
SAge=m_list. GetItemText (I, 3);
SDept=m_list. GetItemText (I, 4);
SLevel=m_list. GetItemText (I, 5);
SPhone=m_list. GetItemText (I, 6);
SEmail=m_list. GetItemText (I, 7);
SFlag=m_list. GetItemText (I, 8).
SID=m_list. GetItemText (I, 9);


If (sFlag=="N")//new user
{
SSql. Format (" Insert into the UserInfo (Code, UserName, \
Sex, Age, Branch, RightLevel, Phone, EMail) \
Values (' % s', '% s',' % s', % d, '% s', % d,' % s', '% s'). "
SNumb, sName sSex, atoi (sAge), sDept, atoi (sLevel), sPhone, sEmail);
AfxMessageBox (sSql);
Try
{
TheApp. M_pConn - & gt; Execute (_bstr_t (sSql), NULL, adCmdText);
}
The catch (_com_error & amp; E)
{
AfxMessageBox (e.D escription ());
}
}
Else if (sFlag=="M")//Modify user
{
AfxMessageBox (sID);

SSql. Format (" Update the UserInfo Set Code='% s', the UserName=' % s', \
='% s' Sex, the Age=% d, Branch=' % s', RightLevel=% d, \
='% s' Phone, EMail=' % s' where id=% d ", \
SNumb, sName sSex, atoi (sAge), sDept, atoi (sLevel), sPhone, sEmail, atoi (sID));
AfxMessageBox (sSql);
Try
{
TheApp. M_pConn - & gt; Execute (_bstr_t (sSql), NULL, adCmdText);
}
The catch (_com_error & amp; E)
{
AfxMessageBox (e.D escription ());
}
}
}
}

CodePudding user response:

I find the method of the Internet, I can't, the following
Else if (sFlag=="D")
{
Try
{
SSql. The Format (_T (" DELETE from the UserInfo where id=% d "));
_variant_t RecordsAffected;
TheApp. M_pConn - & gt; Execute (_bstr_t (sSql), & amp; RecordsAffected adCmdText);
AfxMessageBox (_T (" deleted successful!" ));
}
Catch _com_error * (e)
{
AfxMessageBox (e - & gt; ErrorMessage ());
}

& RecordsAffected this parameter should be a representative to delete rows, not too understand this parameter

CodePudding user response:

Use a SQL statement to delete the database corresponding to the yao, wrong just look at your SQL statement is correct

CodePudding user response:

refer to the second floor SWWLLX response:
with a SQL statement to delete the database corresponding to the yao, make a mistake is to see if your SQL statements right


Yes, 1/f, I posted the method I tried compiler running under no problem, but there is no delete function, m_pConnection - & gt; Execute ((_bstr_t) STR, & amp; RecordsAffected adCmdText);
Could you tell me the inside & amp; RecordsAffected this parameter should be changed in my program?

CodePudding user response:

SSql. The Format (_T (" DELETE * the from the UserInfo where id=% d "));

CodePudding user response:

SSql. The Format (_T (" DELETE from the UserInfo where id=% d "), your id);

CodePudding user response:

sSql. The Format (_T (" DELETE * from the UserInfo where id=% d "), and your id);
  • Related