First, the following header file # importC: ProgramFilesCommonFilessystemadomsadox. dll is included in StdAfx. h. It is required to create a database # importC
First in StdAfx. h contains the following header file # import "C: \ Program Files \ Common Files \ system \ ado \ msadox. dll "// required for creating a database # import" C: \ Program Files \ Common Files \ System \ ADO \ msado15.dll "named_guids rename (" EOF "," adoEOF "), rename ("BOF", "adoBOF") msado
First, the following header file is included in StdAfx. h:
# Import "C: \ Program Files \ Common Files \ system \ ado \ msadox. dll" // required to create a database
# Import "C: \ Program Files \ Common Files \ System \ ADO \ msado15.dll" named_guids rename ("EOF", "adoEOF"), rename ("BOF", "adoBOF ")
Msadox. dll msado15.dll is loaded Based on the location on your computer. Note: The two # import locations cannot be changed. An error occurred while switching the link.
1. Create a database
// Create a database
Void CTestDlg: OnCreateAdoMdb ()
{
HRESULT hr = S_ OK; CString filename = "c: \ test. mdb ";
// Set ActiveConnection of Catalog to this string
CString strcnn (_ T ("Provider = Microsoft. JET. OLEDB.4.0; Data source =" + filename ));
Try
{
ADOX: _ CatalogPtr m_pCatalog = NULL;
Hr = m_pCatalog.CreateInstance (_ uuidof (ADOX: Catalog ));
If (FAILED (hr ))
{
_ Com_issue_error (hr );
}
Else
{
M_pCatalog-> Create (_ bstr_t (strcnn ));
// Create MDB
}
AfxMessageBox (_ T ("OK "));
}
Catch (_ com_error & e)
{
// Configure y the user of errors if any.
AfxMessageBox (_ T ("error "));
}
}
2. Open the database
BOOL CTestApp: InitInstance ()
{
AfxEnableControlContainer ();
CoInitialize (NULL );
.......
Return TRUE;
}
3. Create a new table
Void CTestDlg: OnMainCreate ()
{
Try
{
_ ConnectionPtr pConn; pConn. CreateInstance (_ uuidof (Connection ));
_ RecordsetPtr pRs; pRs. CreateInstance (_ uuidof (Recordset ));
_ CommandPtr Cmd; Cmd. CreateInstance (_ uuidof (Command ));
Try
{
CString dd; CString file = "c :\\ NXYH. mdb ";
// CString dd; CString file = "NXYH. mdb ";
Dd. Format ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = % s", file );
PConn-> Open (_ bstr_t) dd, "", "", adModeUnknown); // Open the local Access Library Demo. mdb
}
Catch (_ com_error e)
{
AfxMessageBox ("database connection failed. Check whether the database NXYH. mdb is in the current path! ");
}
// If this table does not exist, you can create it.
Try
{
_ Variant_t RecordsAffected; CString command1, command2, myfilename = "yours ";
Command1.Format ("create table % s (id integer, username TEXT (5), old INTEGER, birthday
DATETIME) ", myfilename );
PConn-> Execute (_ bstr_t (command1), & RecordsAffected, ad1_text );
Command2.Format ("insert into % s (ID, username, old, birthday) VALUES
(1, 'washton', 26, '2014/1/1') ", myfilename );
PConn-> Execute (_ bstr_t (command2), & RecordsAffected, ad1_text );
AfxMessageBox ("created .");
}
Catch (...)
{
AfxMessageBox ("table have already created .");
}
}
Catch (...)
{
}
}
4. delete records
Void CTestDlg: OnMainDel ()
{
Try
{
_ ConnectionPtr pConn;
_ RecordsetPtr pRs;
PConn. CreateInstance (_ uuidof (Connection ));
PRs. CreateInstance (_ uuidof (Recordset ));
Try
{
CString dd; CString file = "c :\\ NXYH. mdb ";
Dd. Format ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = % s", file );
PConn-> Open (_ bstr_t) dd, "", "", adModeUnknown); // Open the local Access Library Demo. mdb
}
Catch (_ com_error e)
{
AfxMessageBox ("database connection failed. Check whether the database NXYH. mdb is in the current path! ");
}
Try
{
PRs-> Open ("SELECT * FROM coordinate", // query all fields in the DemoTable table
PConn. GetInterfacePtr (), // obtain the IDispatch pointer of the database connected to the database
AdOpenDynamic,
AdLockOptimistic,
AdCmdText );
}
Catch (_ com_error * e)
{
AfxMessageBox (e-> ErrorMessage ());
}
_ Variant_t var;
While (! PRs-> adoEOF)
{
PRs-> MoveFirst ();
PRs-> Delete (adAffectCurrent); // Delete the current record
PRs-> MoveNext ();
}
MessageBox ("delete -- over"); pRs-> Update ();
PRs-> Close (); pConn-> Close (); pRs = NULL; pConn = NULL;
}
Catch (_ com_error & e)
{
Printf ("Error: \ n ");
Printf ("Code = % 08lx \ n", e. Error ());
Printf ("Meaning = % s \ n", e. ErrorMessage ());
Printf ("Source = % s \ n", (LPCSTR) e. Source ());
}
}
5. Add records
Void CTestDlg: OnMainAdd ()
{
Try
{
_ ConnectionPtr pConn;
_ RecordsetPtr pRs;
PConn. CreateInstance (_ uuidof (Connection ));
PRs. CreateInstance (_ uuidof (Recordset ));
Try
{
// Open the local Access Library Demo. mdb
PConn-> Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = NXYH. mdb", "", "", adModeUnknown );
}
Catch (_ com_error e)
{
AfxMessageBox ("database connection failed. Check whether the database NXYH. mdb is in the current path! ");
}
Try
{
PRs-> Open ("SELECT * FROM coordinate", // query all fields in the DemoTable table
PConn. GetInterfacePtr (), // obtain the IDispatch pointer of the database connected to the database
AdOpenDynamic,
AdLockOptimistic,
AdCmdText );
}
Catch (_ com_error * e)
{
AfxMessageBox (e-> ErrorMessage ());
}
// Add new elements
XX = 123.345; YY = 222.434; HH = 1445;
For (int I = 0; I <300; I ++)
{
M_Name.Format ("D % d", I + 1 );
PRs-> AddNew ();
// PRs-> PutCollect ("ID", _ variant_t (long) (I + 10 )));
PRs-> PutCollect ("Name", _ variant_t (m_Name ));
PRs-> PutCollect ("X", _ variant_t (double) (XX )));
PRs-> PutCollect ("Y", _ variant_t (double) (YY )));
PRs-> PutCollect ("H", _ variant_t (double) (HH )));
}
PRs-> Update (); MessageBox ("add -- over ");
PRs-> Close (); pConn-> Close (); pRs = NULL; pConn = NULL;
}
Catch (_ com_error & e)
{
Printf ("Error: \ n ");
Printf ("Code = % 08lx \ n", e. Error ());
Printf ("Meaning = % s \ n", e. ErrorMessage ());
Printf ("Source = % s \ n", (LPCSTR) e. Source ());
}
}
6. Modify records
Void CTestDlg: OnMainChange ()
{
Try
{
_ ConnectionPtr pConn;
_ RecordsetPtr pRs;
PConn. CreateInstance (_ uuidof (Connection ));
PRs. CreateInstance (_ uuidof (Recordset ));
Try
{
// Open the local Access Library Demo. mdb
PConn-> Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = NXYH. mdb", "", "", adModeUnknown );
}
Catch (_ com_error e)
{
AfxMessageBox ("database connection failed. Check whether the database NXYH. mdb is in the current path! ");
}
Try
{
PRs-> Open ("SELECT * FROM coordinate", // query all fields in the DemoTable table
PConn. GetInterfacePtr (), // obtain the IDispatch pointer of the database connected to the database
AdOpenDynamic,
AdLockOptimistic,
AdCmdText );
}
Catch (_ com_error * e)
{
AfxMessageBox (e-> ErrorMessage ());
}
// Modify data
_ Variant_t var;
While (! PRs-> adoEOF)
{
// Var = pRs-> GetCollect ("Name ");
// If (var. vt! = VT_NULL) m_Name = (LPCSTR) _ bstr_t (var );
// MessageBox (m_Name); // m_Name = m_Name.Left (2) + "*";
// M_Name + = "main ";
Var = pRs-> GetCollect ("X ");
If (var. vt! = VT_NULL) m_X = (LPCSTR) _ bstr_t (var );
Double xx= 100.789; // XX + = atof (m_X );
PRs-> PutCollect ("X", _ variant_t (double) (XX )));
// PRs-> PutCollect ("Name", _ variant_t (m_Name ));
PRs-> MoveNext ();
}
MessageBox ("change -- over ");
PRs-> Update ();
PRs-> Close (); pConn-> Close (); pRs = NULL; pConn = NULL;
}
Catch (_ com_error & e)
{
Printf ("Error: \ n ");
Printf ("Code = % 08lx \ n", e. Error ());
Printf ("Meaning = % s \ n", e. ErrorMessage ());
Printf ("Source = % s \ n", (LPCSTR) e. Source ());
}
}