您好我寫了一個代碼來讀取數據庫及其工作。但是當它嘗試使用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