我想在打开数据库文件失败时创建数据库代码如下:
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=QwestarDBMS.mdb","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;  }
if(!(SUCCEEDED(hr)))
///创建数据库
}
创建access数据库如何实现?给个示范。

解决方案 »

  1.   

    void CreateAccessDataBase(CString sAccessFileName)
    {
    HRESULT hr;

    try
    {
    ADOX::_CatalogPtr pCatalog  = NULL;

    //Create object instances:
    hr=pCatalog.CreateInstance(__uuidof (ADOX::Catalog));

    //Create Database
    TCHAR constr[512];
    _stprintf(constr, _T("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s"),(const TCHAR*)sAccessFileName);
    pCatalog->Create(constr); //this function returns a _variant_t datatype result
    }
    catch (_com_error e)
    {
    AfxMessageBox(_T("创建Access数据库失败"));
    }
    }
      

  2.   

    我在stdafx.h加入一下语句:
    #import "c:\Program Files\Common Files\system\ado\msadox.dll" no_namespace
    #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")然后在CMyxxx.cpp中包含了
    #include "stdafx.h"BOOL CMyxxxApp::InitInstance()
    {   
        HRESULT hr;
        ADOX::_CatalogPtr m_pCatalog = NULL;//或者改用::_CatalogPtr m_pCatalog = NULL
    //可就是以上语句编译不通过,错误提示如下: error C2653: 'ADOX' : is not a class or namespace name
    : error C2065: '_CatalogPtr' : undeclared identifier
    : error C2146: syntax error : missing ';' before identifier 'm_pCatalog'
    : error C2065: 'm_pCatalog' : undeclared identifier
    hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
    if(SUCCEEDED(hr))
    {
    hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=QwestarDBMS.mdb","","",adModeUnknown);///连接数据库
    ///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;  }
    if(!(SUCCEEDED(hr)))
    ///创建数据库
    }}
      

  3.   

    我又编译了一下,出错提示为:
    \debug\msado15.tlh(184) : error C2011: 'DataTypeEnum' : 'enum' type redefinition
    \debug\msado15.tlh(569) : error C2011: '_Collection' : 'struct' type redefinition
    \debug\msado15.tlh(598) : error C2011: '_DynaCollection' : 'struct' type redefinition
    \debug\msado15.tlh(644) : error C2011: 'Properties' : 'struct' type redefinition
    \debug\msado15.tlh(670) : error C2011: 'Property' : 'struct' type redefinition
    \debug\msado15.tli(14) : error C2084: function 'long __thiscall _Collection::GetCount(void)' already has a body
    \debug\msado15.tli(21) : error C2084: function 'class _com_ptr_t<class _com_IIID<struct IUnknown,&struct __s_GUID  _GUID_00000000_0000_0000_c000_000000000046> > __thiscall _Collection::_NewEnum(void)' already has a body
    \debug\msado15.tli(28) : error C2084: function 'long __thiscall _Collection::Refresh(void)' already has a body
    \debug\msado15.tli(38) : error C2084: function 'long __thiscall _DynaCollection::Append(struct IDispatch *)' already has a body
    \debug\msado15.tli(44) : error C2084: function 'long __thiscall _DynaCollection::Delete(const class _variant_t &)' already has a body
    \debug\msado15.tli(65) : error C2084: function 'class _com_ptr_t<class _com_IIID<struct Property,&struct __s_GUID  _GUID_00000503_0000_0010_8000_00aa006d2ea4> > __thiscall Properties::GetItem(const class _variant_t &)' already has a b
    ody
    \debug\msado15.tli(76) : error C2084: function 'class _variant_t __thiscall Property::GetValue(void)' already has a body
    \debug\msado15.tli(84) : error C2084: function 'void __thiscall Property::PutValue(const class _variant_t &)' already has a body
    \debug\msado15.tli(89) : error C2084: function 'class _bstr_t __thiscall Property::GetName(void)' already has a body
    \debug\msado15.tli(96) : error C2084: function 'enum DataTypeEnum __thiscall Property::GetType(void)' already has a body
    \debug\msado15.tli(103) : error C2084: function 'long __thiscall Property::GetAttributes(void)' already has a body
    \debug\msado15.tli(110) : error C2084: function 'void __thiscall Property::PutAttributes(long)' already has a body
    Error executing cl.exe.
      

  4.   

    流程应该没有问题。import语句将生成一个tli文件和一个tlh文件,你干脆直接include
    这两个文件试试。我没有试过。
      

  5.   

    我采用import语句将生成一个tli文件和一个tlh文件,直接include
    这两个文件,还是编译出错。
      

  6.   

    你仔细检查,检查adox对应的tli,tlh,文件中声明了ADOX这个namespace的,应该没有问题。
      

  7.   

    Rebuild all看看或者,在程序初始化前记得加上CoInitialize(NULL);
      

  8.   

    CoInitialize(NULL)加过了,可是还是老毛病.
      

  9.   

    没遇到过
    不过有个简单的办法就是在你的程序里代一个.mdb的资源空的
    要用的时候读出来保存即可使用!还不用多调用adox.dll ^_^.
      

  10.   

    我必须用ADOX.DLL,因为一旦系统数据库被删除,可能导致系统崩溃.这样做保证了在系统的数据库被删除了后,可以在程序里自动创建数据库,保证系统正常运行.
      

  11.   

    是ado 的 加
    ADODB::
    是 adox 的加
    ADOX::