我想在软件运行时把使用的acess数据库导出为*.mdb数据库文件,并且运行中把一个mdb文件替换使用中的数据库,用代码应该如何实现?谢谢

解决方案 »

  1.   

    这是建库的代码,库和数据源建立好之后要导出什么数据是比较容易实现的
    /////////////////////ACCESSBOOL CDialogExport::CreateDSN(CString strDSN)
    {//创建ACCESS数据库文件并建立临时数据源
    CString sPath,sSql,sFileName;
    int nPos = m_strPath.ReverseFind('\\');
    int nLen = m_strPath.GetLength();
    sPath = m_strPath.Left(nPos);
    sFileName = m_strPath.Right(nLen - nPos - 1);
    BOOL ret;
    //创建ACCESS文件
    CFileStatus fileStatus;
    if (CFile::GetStatus(m_strPath, fileStatus)) {
    #ifdef ENGLISH
    ret = AfxMessageBox("Are you sure to replace the file?",MB_YESNO|MB_ICONQUESTION);
    #else
    ret = AfxMessageBox("文件已存在,是否替换该文件?",MB_YESNO|MB_ICONQUESTION);
    #endif
    if (IDNO == ret)
    {
    return FALSE;
    }
    else
    {
    CFile::Remove(m_strPath);
    }
    }
    sSql.Format("CREATE_DB=\"%s\" General\0",m_strPath);
    ret = SQLConfigDataSource(NULL,ODBC_ADD_DSN,
    "Microsoft Access Driver (*.mdb)", sSql);
    if (!ret)
    {
    #ifdef ENGLISH
    AfxMessageBox("CREATE ACCESS FILE FAILED.", MB_OK|MB_ICONERROR);
    #else
    AfxMessageBox("创建数据库文件失败!" , MB_OK|MB_ICONERROR);
    #endif
    return FALSE;
    }
    //建立ODBC数据源
    sSql.Format("DSN=%s;DBQ=%s;DEFAULTDIR=%s\0\0",strDSN,sFileName,sPath);
    ret = SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,
    "Microsoft Access Driver (*.mdb)", sSql);
    if (!ret)
    {
    #ifdef ENGLISH
    AfxMessageBox("CREATE DSN FAILED.", MB_OK|MB_ICONERROR);
    #else
    AfxMessageBox("创建ODBC数据源失败!" , MB_OK|MB_ICONERROR);
    #endif
    return FALSE;
    }
    return TRUE;
    }