现在的位置: 首页 > 综合 > 正文

MFC ado数据库:所有表名称

2017年02月08日 ⁄ 综合 ⁄ 共 1584字 ⁄ 字号 评论关闭

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;

};

BOOL CAdoDatabase::OpenMDB(CString strPath)
{
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;
}

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

【上篇】
【下篇】

抱歉!评论已关闭.