我用DAO:
CDaoDatabase db;
db.Create(DbName);
CDaoTableDef tbf(&db);
tbf.Create("index_table");
tbf.CreateField("序号",dbLong,dbAutoIncrField);
tbf.CreateField("产品型号",dbText,40,dbVariableField);
tbf.CreateField("出厂序号",dbText,20,dbVariableField);
tbf.CreateField("日期",dbText,20,dbVariableField);
tbf.CreateField("时间",dbText,20,dbVariableField);
tbf.CreateField("评价",dbText,20,dbVariableField);
tbf.CreateField("状态",dbByte,1,dbVariableField);
tbf.CreateField("测试数据库路径",dbText,80,dbVariableField);
CDaoIndexInfo indexinfo;
CDaoIndexFieldInfo indexfieldinfo;
indexfieldinfo.m_bDescending=FALSE;
indexfieldinfo.m_strName="序号";
indexinfo.m_bPrimary=TRUE;
indexinfo.m_strName="序号";
indexinfo.m_nFields=1;
indexinfo.m_pFieldInfos=&indexfieldinfo;
tbf.CreateIndex(indexinfo);
tbf.Append();
tbf.Close();
db.Close()

解决方案 »

  1.   

    ODBC:char* szDesc;
    int mlen;
    szDesc=new char[256];
    CString strpath;
    strpath = dbfile_name;
    while((CString)(strpath.GetAt(strpath.GetLength()-1))!="\\")
    {
        strpath.Delete(strpath.GetLength()-1,1);
    }
    strpath.Delete(strpath.GetLength()-1,1);sprintf(szDesc,"DSN=%s^ DESCRIPTION=TOC support source^ DBQ=%s^ FIL=MicrosoftAccess^ DEFAULTDIR=%s^^",
    "MyDB",dbfile_name,strpath);
    mlen = strlen(szDesc);for (int i=0; i<mlen; i++)
    {
        if (szDesc[i] == '^')
        szDesc[i] = '\0';
    }if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0",(LPCSTR)szDesc))
    {
        MessageBox("Failed","INFORMATION",MB_OK);
        return;
    }
    //else
    // MessageBox("Sucess","INFORMATION",MB_OK);
    m_MsGrid.ClearFields();
    m_MsGrid.SetCaption(dbfile_name);
    m_Rdc.SetDataSourceName(_T("MyDB"));
    m_Rdc.SetSql(_T("select * from 产品测试数据表"));
    m_Rdc.SetUserName(_T(""));
        
                m_Rdc.SetPassword(_T(""));
                m_Rdc.SetCaption(m_gDb_path);
                m_Rdc.SetCursorDriver(0);
                m_Rdc.Refresh();