Get the name of all tables in the Access2000 database (VC + ADO)

zhaozj2021-02-16  43

The principle is that the type of the table created by the user is generally TBALE by enumerating the type of table in the database. So by judging the type of tables in the current database, you can get the name of the table.

Initialization section

Add: STDAFX.H:

#include #include #include

#import "c: / program files / compo files / system / ado / msado15.dll" / no_namespace rename ("eof", "endoffile")

In the header file of the main dialog, map one:

Clistbox m_strlist;

Implementation section:

Add: in the constructor:

:: Coinitialize (NULL); // Initialization COM library

Implement code:

// Enumerate the name of the data table

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); // enumerated table of name handles while (! (PrStschema-> endoffile) {cstring stratablepe; _BSTR_T TABLE_NAME = prstschema-> fields-> GetItem ("Table_name") -> value; // Get the name of the table _BSTR_T TABLE_TYPE = PRSTSCHEMA-> Fields-> GetItem ("Table_Type") -> value; // Get the Table of Table StrtableType.Format ("% S", (LPCSTR) Table_Type); if (! Lstrcmp) , _T ("Table"))) {m_strlist.addstring (lpcstr) Table_name); // Add a name of the table} PRSTSCHEMA-> MOVENEXT (); } // clean Up Objects Before Exit (); PConnection-> Close ();} catch (_com_error & e) {// notify the user of errors if any. // pass a connection Pointer Accessed from the connection PRINTPROVIDERERROR (PCONNECTION); PrintComerror (E);} Couninitialize ();} // Print Error Code and Description

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 Errors-> GetItem (i); cstring strerror; strerror.format ("Error Number:% X / T% S", Perr-> Number, Perr-> description); AFXMessageBox (strold);}}} // Print COM error message

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_ALLOWMULTITILECT, SZFILTER);

// modify OpenFileName Members Directly to Point To BigBuff Dlg.m_ofn.lpstrfile = BigBuff; DLG.M_OFN.NMAXFILE = Sizeof (BigBuff);

IF (idok == dlg.domodal ()) {

m_strlist.resetcontent (); strfilename = dlg.getpathname (); lstrcpy (filename, strfilename); OpenSchemax (filename);}

Release section:

LRESULT WindowProc (UINT message, WPARAM wParam, LPARAM lParam) {// TODO: Add your specialized code here and / or call the base class switch (message) {case WM_CLOSE: CoUninitialize (); break; default: break;} return CDialog :: WINDOWPROC (Message, WPARAM, LPARAM);

Debugging environment: Win2000 ADO Access2000 VSP5

转载请注明原文地址:https://www.9cbs.com/read-25875.html

New Post(0)