Obtain the names of all tables in the ACCESS2000 Database
Obtain the names of all tables in the ACCESS2000 Database
Void OpenSchemaX (TCHAR * TableName)
{
HRESULT hr = S_ OK;
: CoInitialize (NULL); // initialize Com
IADORecordBinding * picRs = NULL;
_ RecordsetPtr pRstSchema ("ADODB. Recordset ");
_ ConnectionPtr pConnection ("ADODB. Connection ");
PConnection-> ConnectionString = TableName;
PConnection-> Provider = "Microsoft. Jet. OLEDB.4.0 ";
Try
{
PConnection-> Open (pConnection-> ConnectionString, "", "", adModeUnknown );
PRstSchema-> QueryInterface (
_ Uuidof (IADORecordBinding), (LPVOID *) & picRs );
PRstSchema = pConnection-> OpenSchema (adSchemaTables); // process the name of the enumerated table
While (! (PRstSchema-> EndOfFile ))
{
CString strTableType;
_ Bstr_t table_name = pRstSchema-> Fields->
GetItem ("TABLE_NAME")-> Value; // obtain the table name
_ Bstr_t table_type = pRstSchema-> Fields->
GetItem ("TABLE_TYPE")-> Value; // obtain the table Type
StrTableType. Format ("% s", (LPCSTR) table_type );
If (! Lstrcmp (strTableType, _ T ("TABLE ")))
{
M_strList.AddString (LPCSTR) table_name); // Add the table name.
}
PRstSchema-> MoveNext ();
}
// Clean up objects before exit.
PRstSchema-> Close ();
PConnection-> Close ();
}
Catch (_ com_error & e)
{
// Configure y the user of errors if any.
// Pass a connection pointer accessed from the Connection.
PrintProviderError (pConnection );
PrintComError (e );
}
CoUninitialize ();
}
Void PrintProviderError (_ ConnectionPtr pConnection)
{
ErrorPtr pErr = NULL;
If (pConnection-> Errors-> Count)> 0)
{
Long nCount = pConnection-> Errors-> Count;
// Collection ranges from 0 to nCount-1.
For (long I = 0; I <nCount; I ++)
{
PErr = pConnection-> Errors-> GetItem (I );
CString strError;
StrError. Format ("Error number: % x \ t % s", pErr-> Number, pErr-> Description );
AfxMessageBox (strError );
}
}
}
Void PrintComError (_ com_error & e)
{
_ Bstr_t bstrSource (e. Source ());
_ Bstr_t bstrDescription (e. Description ());
// Print COM errors.
CString strError;
StrError. Format ("Error number: Description = % s \ tCode meaning = % s", (LPCSTR) bstrDescription, e. ErrorMessage ());
AfxMessageBox (strError );
}
Call method:
CString strFileName;
TCHAR FileName [MAX_PATH];
TCHAR bigBuff [2048] = _ T (""); // maximum common dialog buffer size
TCHAR szFilter [] = _ T ("Text Files (*. mdb) | *. mdb | All Files (*. *) | *.*
");
CFileDialog dlg (TRUE, NULL, NULL,
OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, szFilter );
// Modify OPENFILENAME members directly to point to bigBuff
Dlg. m_ofn.lpstrFile = bigBuff;
Dlg. m_ofn.nMaxFile = sizeof (bigBuff );
If (IDOK = dlg. DoModal ())
{
StrFileName = dlg. GetPathName ();
Lstrcpy (FileName, strFileName );
OpenSchemaX (FileName );
} (Source: fengshangwang road College)