手机图片 苏州便民网 诺基亚手机
首页 | 英语四六级 | 日语学习 | 网络学院 | 考研 | 公务员考试 | 计算机 | 笑话 | 减肥 设大学生网为首页 加入收藏
C语言 VB VFP VC++ ACCESS JAVA
 
 
 
 
 

您的位置:首页 >> 计算机 >> 二级考试 >> ACCESS >> 浏览信息

正文内容

获取ACCESS2000数据库中所有表的名称

时间:2007-05-17 来源:大学生计算机等级考试 打印本文

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

  }

  调用方法:

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


上一篇:下面没有链接了
下一篇:在VB中用代码打印ACCESS报表

相关阅读:

无相关信息

网站简介 | 广告服务 | 联系方式 | 意见建议 | 网站地图 | 版权声明 | 友情连接

大学生网 Stuun.com 版权所有