VC6.0用m_pSet->AddNew()是好好的,在VS2012却出现系统崩溃,弹出来:
在调试窗口中提示:
Warning: Driver does not support requested concurrency.
Warning: Setting recordset read only.
Debug Assertion Failed!
Program: D:\MFCDemos\í?è′VS2012°?\StudSco1\.\Debug\StudSco.exe
File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dbcore.cpp
Line: 64
其中关键的一行信息:“记录集为只读”。
造成这一问题的原因在于打开数据记录集的方式,在VC6.0中可以直接选择默认方式,即:
m_pSet->Open(AFX_DB_USE_DEFAULT_TYPE,strSQL)
但在VS2010以上版本中(笔者使用的是VS2012,其他版本未测试),应该将默认方式改为动态记录集方式,即:
m_pSet->Open(CRecordset::dynaset,strSQL)
究其根源,在于CRecordset的Open函数的用法根据参数的不同有不同的结果,具体用法参看下文
CRecordset::Open
virtual BOOL Open( UINT nOpenType = AFX_DB_USE_DEFAULT_TYPE, LPCTSTR lpszSQL = NULL, DWORD dwOptions = none );
throw( CDBException, CMemoryException );
Return Value
Nonzero if the CRecordset object was successfully opened; otherwise 0 if CDatabase::Open (if called) returns 0.
Parameters
nOpenType
Accept the default value, AFX_DB_USE_DEFAULT_TYPE, or use one of the following values from the enum OpenType:
CRecordset::dynaset A recordset with bi-directional scrolling. The membership and ordering of the records are determined when the recordset is opened, but changes made by other users to the data values are visible following a fetch operation. Dynasets are also known as keyset-driven recordsets.
CRecordset::snapshot A static recordset with bi-directional scrolling. The membership and ordering of the records are determined when the recordset is opened; the data values are determined when the records are fetched. Changes made by other users are not visible until the recordset is closed and then reopened.
CRecordset::dynamic A recordset with bi-directional scrolling. Changes made by other users to the membership, ordering, and data values are visible following a fetch operation. Note that many ODBC drivers do not support this type of recordset.
CRecordset::forwardOnly A read-only recordset with only forward scrolling.
For CRecordset, the default value is CRecordset::snapshot. The default-value mechanism allows the Visual C++ wizards to interact with both ODBC CRecordset and DAO CDaoRecordset, which have different defaults.
For more information about these recordset types, see the articleRecordset (ODBC) in Visual C++ Programmer’s Guide. For related information, see the article ”Using Block and Scrollable Cursors" in the ODBC SDK Programmer's Reference.
Caution If the requested type is not supported, the framework throws an exception.