MFC ado資料庫:所有表名稱
阿新 • • 發佈:2018-11-07
class CAdoDatabase
{
public:
CAdoDatabase();
virtual ~CAdoDatabase();
BOOL OpenMDB(CString strPath);
BOOL CreateTable(CString sql);
BOOL InsertValues(int i);
BOOL TableNameSaveFile();
public:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
HRESULT hr;
CString strSQL;
strSQL.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False", strPath);
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///建立Connection物件
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open((_bstr_t)strSQL,"","",adModeUnknown);///連線資料庫
AfxMessageBox("ado connection");
}
}
catch(_com_error e)///捕捉異常
{
CString errormessage;
errormessage.Format("連線資料庫失敗!\r\n錯誤:%s!",e.ErrorMessage());
AfxMessageBox(errormessage);///顯示錯誤資訊
return FALSE;
}
return TRUE;
}
BOOL CAdoDatabase::CreateTable(CString sql)
{
return TRUE;
}
BOOL CAdoDatabase::TableNameSaveFile()
{
m_pRecordset = m_pConnection->OpenSchema(adSchemaTables);
FILE* fp = fopen("1.txt","w");
while(!(m_pRecordset ->adoEOF))
{
_bstr_t tblname = m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;//獲取表格
_bstr_t tbltype = m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;//獲取表格型別
//這裡可以對錶格型別進行判斷,判斷後即可處理tblname
if (!strcmp(tbltype ,"TABLE"))
{
AfxMessageBox(tblname);
}
fprintf(fp, "%s\n",(LPCSTR)tblname);
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
return TRUE;
}
{
public:
CAdoDatabase();
virtual ~CAdoDatabase();
BOOL OpenMDB(CString strPath);
BOOL CreateTable(CString sql);
BOOL InsertValues(int i);
BOOL TableNameSaveFile();
public:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
};
BOOL CAdoDatabase::OpenMDB(CString strPath)
{
CString strSQL;
strSQL.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False", strPath);
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///建立Connection物件
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open((_bstr_t)strSQL,"","",adModeUnknown);///連線資料庫
}
}
catch(_com_error e)///捕捉異常
{
CString errormessage;
errormessage.Format("連線資料庫失敗!\r\n錯誤:%s!",e.ErrorMessage());
AfxMessageBox(errormessage);///顯示錯誤資訊
return FALSE;
}
}
BOOL CAdoDatabase::CreateTable(CString sql)
{
return TRUE;
}
BOOL CAdoDatabase::TableNameSaveFile()
{
m_pRecordset = m_pConnection->OpenSchema(adSchemaTables);
FILE* fp = fopen("1.txt","w");
while(!(m_pRecordset ->adoEOF))
{
_bstr_t tblname = m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;//獲取表格
_bstr_t tbltype = m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;//獲取表格型別
//這裡可以對錶格型別進行判斷,判斷後即可處理tblname
if (!strcmp(tbltype ,"TABLE"))
{
AfxMessageBox(tblname);
}
fprintf(fp, "%s\n",(LPCSTR)tblname);
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
return TRUE;
}
CFileDialog dlg(TRUE);
static CString filepath;
if(IDOK == dlg.DoModal())
{
POSITION pos = dlg.GetStartPosition();
while(pos!= NULL)
{
filepath = dlg.GetNextPathName(pos); //得到路徑/
}
}
CAdoDatabase ado1;
ado1.OpenMDB(filepath);
ado1.TableNameSaveFile();