获取ACCESS2000数据库中的所有表的名称(vc + ado)

2016-02-19 12:56 29 1 收藏

今天图老师小编给大家介绍下获取ACCESS2000数据库中的所有表的名称(vc + ado),平时喜欢获取ACCESS2000数据库中的所有表的名称(vc + ado)的朋友赶紧收藏起来吧!记得点赞哦~

【 tulaoshi.com - 编程语言 】

原理就是通过枚举数据库中的表的类型,用户建立的表的类型一般是TBALE。所以通过判断当前数据库中表的类型,就可以获取表的名称了。

初始化部分

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

在stdafx.h中添加:

#include icrsint.h
#include basetsd.h
#include ole2.h

#import "c:Program FilesCommon FilesSystemADOmsado15.dll"
no_namespace rename("EOF", "EndOfFile") 

在主对话框的头文件中,映射一个:

 CListBox m_strList;

实现部分说明: 

在构造函数中添加:

 ::CoInitialize(NULL); //初始化Com库

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

实现代码:

//枚举数据表的名称处理

 void OpenSchemaX(TCHAR *TableName)
{
    HRESULT  hr = S_OK;

 ::CoInitialize(NULL); //初始化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);//枚举表的名称处理
 
        while(!(pRstSchema-EndOfFile))
        {
   CString strTableType;
  
            _bstr_t table_name = pRstSchema-Fields-
                GetItem("TABLE_NAME")-Value;//获取表的名称
  
            _bstr_t table_type = pRstSchema-Fields-
                GetItem("TABLE_TYPE")-Value;//获取表的类型
  
  
            strTableType.Format("%s",(LPCSTR) table_type);
  
   if(!lstrcmp(strTableType,_T("TABLE")))
            {
    m_strList.AddString((LPCSTR) table_name);//添加表的名称
   }
  
            pRstSchema-MoveNext();
        }
        // Clean up objects before exit.
 
        pRstSchema-Close();
        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();
}

//打印错误代码及描述

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%s", pErr-Number, pErr-Description);
   AfxMessageBox(strError);
        }
    }
}

//打印COM的错误信息

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 = %sCode meaning = %s",(LPCSTR) bstrDescription, e.ErrorMessage());
 AfxMessageBox(strError);

调用方法:

 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() )
 {

  m_strList.ResetContent();
  strFileName = dlg.GetPathName();
  lstrcpy(FileName,strFileName);
  OpenSchemaX(FileName);
 } 

释放部分:

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);
}

调试环境:

WIN2000 + ADO + ACCESS2000 + VSP5

来源:https://www.tulaoshi.com/n/20160219/1602546.html

延伸阅读
标签: ASP
  <% ' Define your sql info here strSQLDSN = "xxxx" strSQLUsername = "sa" strSQLPassword = "" ' This is where we connect to our sql server Set adoConn = Server.CreateObject("ADODB.Connection") ConnectionString = "dsn=" & strSQLDSN & ";uid=" &...
  在Office家族成员中,人们对于Access 2000的了解,往往只局限在它的操作界面中,对于数据库管理的功能仍只是停 留在建立表、数据输入、使用窗体向导、报表向导、数据访问页向导等一些简单的应用上。其实Access 2000的功能十分强 大,且超乎你的想像。它是微软自公布Access以来功能最全面、与Windows和Internet结合最紧密的数据库软...
人们对于数据库管理的功能只是停留在建立表、数据输入、使用窗体向导、报表向导、数据访问页向导等一些简单的应用上。其实Access 2000的功能十分强大,且超乎你的想像。它是微软自公布Access以来功能最全面、与Windows和Internet结合最紧密的数据库软件,是一个功能非常强大,而且简单易用的数据库管理系统(DBMS),即对数据库进行存储、...
标签: ASP
      主要思路: 用一条语句统计(Count)出记录数(而不在查询时获得 RecordCount 属性), 缓存在 Cookies 中, 跳转时就不用再次统计. 使用 ADO 的 AbsolutePage 属性进行页面跳转即可. 为方便调用而写成类, 代码主要地方已有说明 硬件环境: AMD Athlon XP 2600+, 256 DDR 软件环境: MS Windows 2000 Advanced Server + II...
DataSet ds=new DataSet(); System.Data.SqlClient.SqlConnection cnn=new System.Data.SqlClient.SqlConnection("server=(local);database=northwind;trusted_connection=yes") ; cnn.Open(); System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient.SqlCommand(); cmd.Connection=cnn; cmd.CommandText="Select * from PersonDeta...

经验教程

979

收藏

74
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部