本人不是很熟悉数据库,我把一切代码移植到控制台就不能用了。
有那位大侠给我一些简单的数据库操作代码好吗??[email protected]

解决方案 »

  1.   

    CFileDialog filedlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,
    "XLS Files (*.xls)|*.xls",NULL);
    CString filename;
    CDatabase database;
    CString sSql;
    CString sItem1;
    CString sDriver;
    CString sDsn;
    CString strSMS, sms[10000];
    char cDefaultfile[MAX_PATH];
    CDBVariant varValue;
    int iNum, count, i;
    long iIndex[10000];
    char cSms[2000]; memset(cDefaultfile, 0, MAX_PATH); if (strCurrType.GetLength() == 0)
    {
    MessageBox("请先选择短信分组,如果没有分组,则请先建立分组","常用短信");
    return;
    } sDriver = theApp.GetExcelDriver();
    if( sDriver.IsEmpty() )
    {

    AfxMessageBox("No Excel ODBC driver found");
    return;
    } sprintf(cDefaultfile, "%s\\temple\\常用短信\\常用短信模板.xls", theApp.GetAppPath());
    filedlg.m_ofn.lpstrFile = cDefaultfile;
    filedlg.m_ofn.nMaxFile = MAX_PATH;
    filedlg.m_ofn.lpstrDefExt = "XLS";
    if (filedlg.DoModal() == IDOK)
    {
    m_List.DeleteAllItems();
    filename = filedlg.GetPathName();
    sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,filename); TRY
    {
    // Open the database using the former created pseudo DSN
    database.Open(NULL,false,false,sDsn);

    // Allocate the recordset
    CRecordset recset( &database ); // Build the SQL string
    // Remember to name a section of data in the Excel sheet using "Insert->Names" to be
    // able to work with the data like you would with a table in a "real" database. There
    // may be more than one table contained in a worksheet.
    sSql = "SELECT * from [Sheet1$]";

    // Execute that query (implicitly by opening the recordset)
    recset.Open(CRecordset::forwardOnly,sSql,CRecordset::readOnly); // Browse the result
    short nFields = recset.GetODBCFieldCount( ); while( !recset.IsEOF() )
    {
    // Read the result line
    for( short index = 0; index < nFields; index++ )
    {
      // do something with varValu
    switch (index)
    {
    case 0:
    recset.GetFieldValue(index, sItem1);
    if (sItem1.GetLength()>140)
    {
    MessageBox("导入的内容大于140个字节,将不允许被导入","短信导入");
    recset.MoveNext();
    continue;
    }
    iNum = theApp.GetNormalSMSNum();
    iNum++;
    theApp.m_db.InsertSMS(iNum, strCurrType, sItem1);
    theApp.SetNormalSMSNum(iNum);
    break;
    default:
    break;
    }
    }

    // Skip to the next resultline
    recset.MoveNext();
    } // Close the database
    database.Close(); count = theApp.m_db.SelSMS(iIndex, strCurrType, sms); m_List.DeleteAllItems();
    for(i=0; i<count; i++)
    {
    sprintf(cSms, "%s", sms[i]);
    InsertPhoneBook(iIndex[i], cSms);
    }
    }
    CATCH(CDBException, e)
    {
    // A database exception occured. Pop out the details...
    AfxMessageBox("Database error: "+e->m_strError);
    }
    END_CATCH;
    }写EXCEL的一段代码,基于mfc的