判断数据库是否存在,表是否存在,否的话新建!

void Caccess_test_1Dlg::existmdb()
{
    if (!PathFileExists(_T(filename)))
    {
        AfxMessageBox(_T("数据库目前不存在!\r将重新为您创建数据库!"));
        //return;
        //创建数据库:新建一个Catalog对象,然后调用它的Create方法
        m_pCatalog = NULL;
        CString DBName = _T("Provider=Microsoft.JET.OLEDB.4.0;Data source=");
        CString str;
        str = _T(filename);
        DBName = DBName + str;
        try
        {
            m_pCatalog.CreateInstance(__uuidof(ADOX::Catalog));
            m_pCatalog->Create(_bstr_t((LPCTSTR)DBName));
        }
        catch (_com_error &e)
        {
            AfxMessageBox(e.ErrorMessage());
            return;
        }
        //连接数据库
        try
        {
            m_pConnection.CreateInstance(__uuidof(Connection));
            m_pConnection->Open(StrCmd, "", "", adModeUnknown);
        }
        catch (_com_error &e)
        {
            CString errormessage;
            errormessage.Format(_T("连接数据库失败!/r错误信息:%s"), e.ErrorMessage());
            AfxMessageBox(errormessage);
            return;
        }
        //createtable();
        existtable();
        return;
    }
    else
    {
        existtable();
    }
}

不能有那个return,不然没有数据库的话也不会重新创建一个新的~!

posted @ 2017-09-08 14:54  ostartech  阅读(587)  评论(0编辑  收藏  举报