您好我写了一个代码来读取数据库及其工作。但是当它尝试使用AddNew()写入数据库时。但它给出了调试断言错误。这里是我正在尝试的代码在使用ODBC在MFC中创建的基于对话框的应用程序中写入数据库
void CReadDBDlg::OnRead()
{
// TODO: Add your control notification handler code here
CDatabase database;
CString SqlString;
CString sCatID, sCategory;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "d:\\Test.mdb";
// You must change above path if it's different
int iRec = 0;
// Build ODBC connection string
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database
database.OpenEx(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset(&database);
// Build the SQL statement
SqlString = "SELECT CatID, Category "
"FROM Categories";
// Execute the query
recset.Open(CRecordset::snapshot,SqlString,CRecordset::appendOnly);
// Loop through each record
// Copy each column into a variable
recset.GetFieldValue("CatID",sCatID);
recset.GetFieldValue("Category",sCategory);
// goto next record
recset.MoveLast();
recset.GetFieldValue("CatID",sCatID);
//recset.AddNew();
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// If a database exception occured, show error msg
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
我们正在使用的数据库是MS接取 – user1285170 2012-03-22 06:53:44
和代码写在Visual Studio 2008中 – user1285170 2012-03-22 06:54:06