About the MFC ODBC classes are unable to update the CRecordset dataset

xiaoxiao2021-03-06  109

Incorrectly editing, add, delete the open data set with the MFC database class CDatabase and CRecordset class declaration. Character, let me feel such a problem, let me spend a lot of spirit, I don't know why the database can be opened normally, and the data set is also normal open. However, there is no way to enter the editing state, always pop up the "Recording Read Read" prompt. -------------------------------------------------- -------------------------------------------------- -------------------------- CDATABASE DB; DB.OPENEX (_t "dsn = stratab"); CRecordset M_Set (& DB); m_set.open (AFX_DB_USE_DEFAULT_TYPE, _T "Select * from stratab"); if (! M_set.isopen ()) AFXMESSAGEBOX ("Data set is not open!"); M_set.edit (); ----------- -------------------------------------------------- -------------------------------------------------- ----------------- Check your own procedure, find that whether the declared CDATABASE object is still used by CRecordset's record set, which is the default parameter, My memory cdatabase :: OpenEx () and CRecordset :: Open () The default parameters of the two member functions open record sets and database objects are not read-only. What is the problem? BOOL CDatabase :: OpenEx (LPCTSTR lpszConnectString, DWORD dwOptions) virtual BOOL Open (UINT nOpenType = AFX_DB_USE_DEFAULT_TYPE, LPCTSTR lpszSQL = NULL, DWORD dwOptions = none); I open dataset in my program Open () and at the lower breakpoint Track in, found that the crecordset's m_bupdatable flag initially has always been true for true, you can update the status.

Subsequently, in the following stroke, the next dazzling function is finally found, the value of m_bupdatable is changed there, all the problems are from there! -------------------------------------------------- -------------------------------------------------- ------------------------------------------ BUILDSQL (LPSZSQL); IF ! (m_bUpdatable || m_bAppendable) && IsRecordsetUpdatable ()) if (IsSQLUpdatable (m_strSQL!)) return IsSelectQueryUpdatable (lpszSQL); lpchTokenFrom = FindSQLToken (strSQL, _afxFrom); // AFX_STATIC_DATA const TCHAR _afxFrom [] = _T ( "FROM") LPSZFOUNDTOKEN = _TCSSTR (LPSZFOUNDTOKEN NTOKENOFFSET, LPSZSQLTOKEN); -------------------------------------- -------------------------------------------------- -------------------------------------------------- -------- The above line is a function that tracks in, the blue word is a function name. These functions are analyzing and performing corresponding settings to open the data set. Note What is the value of the fifth line _AFXFROM! This is a constant defined by the MFC, and its order is commented there. The problem is here, the MFC defines whether this constant is used to analyze if the SQL statement contains from the FROM keyword and the table name. If there is this keyword, the data set is opened in an updated state. However, it is clear that the SQL statement that opens the data set in my program is this from keyword and table name. Then I should get an updateable data set, why can't I update? Take a closer look at this _AFXFROM constant, its value is _t ("from") from From, there is a space, so the truth is white. The problem is that my SQL statement is not given a field name but directly uses the * sign to return all the fields, and this * tightened the from keyword, so in the last line MFC The TCSSTR () function looks up in the SQL statement, the result is that the two heads of this two have one space that have a space can never find the MFC definition. _TCSSTR () returns an empty pointer, then these functions returns false so that the value of m_bupdatable is false, and the data set I open is not updated. Why did this have not appeared before? That's because I didn't return only a particular field, and the FROM in the constructed SQL statement is also a space before and after. That is, the direct CRecordset :: Open () a parameter does not have this open () function. This default SQL statement constructed by the MFC is definitely in line with his own requirements. In fact, the * SQL statement written by the * is also a habit of many people. So, I believe that there are many friends who have encountered such problems. In addition, there is a Group By clause in the SQL statement or a multi-table query that is not updated and deleted. But MFC has no specific error report, but also discovered in tracking. It is also good to have source code, or I don't know what to hurt.

转载请注明原文地址:https://www.9cbs.com/read-102358.html

New Post(0)