数据库相关

一、ADO方式一     
    #include "ado2.h"
    CADODatabase m_cnn;    
    CADORecordset m_rst;

    strCurDir=theApp.m_strAppPath;    strFile="InitData\\config.accdb";     ////OFFICE2007以上版本*.accdb文件使用。
    strCnn.Format("Provider=Microsoft.Ace.Oledb.12.0;Data Source=%s;Persist Security Info=False",strCurPath);
    ////OFFICE2003以下的版本*.mdb文件,  使用Provider=Microsoft.Jet.OLEDB.4.0
    b=m_cnn.Open(strCnn,"","");       if(!b)    {        AfxMessageBox("不能打开:config.accdb数据文件!");        return;    }
    b1=m_rst.Open(m_cnn.GetActiveConnection(),"select * from 图层");
    if(!b1)    {            AfxMessageBox("不能访问:config.accdb数据文件的图层表!");        return;    }
    nCount=m_rst.GetRecordCount();   
    if (nCount==0)     {        m_rst.Close();        m_cnn.Close();        return;    }
    m_rst.GetFieldValue("图层标号",pml->ID);     
    m_rst.MoveNext();

    m_rst.AddNew();        
    m_rst.SetFieldValue("图层标号",pml->ID);        
    m_rst.Update();
    
m_rst.Close();        m_cnn.Close(); 
二、ADO方式二
    #import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF", "adoEOF") 
    CoInitialize(NULL);
    _ConnectionPtr pConn(_uuidof(Connection));
    _RecordsetPtr pRst(_uuidof(Recordset));
    pConn->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ./Dbase/农气簿记录索引.mdb;";
    try
    {
        pConn->Open("","","",adConnectUnspecified);
    }
    catch(_com_error e)
    {
        CString str("数据库连接失败!");
        AfxMessageBox(str);
        return ;
    }
    pRst = pConn->Execute("select * from 作物生育状况观测索引 where 作物名称 = '春小麦'",NULL,adCmdText);
    while(!pRst->adoEOF)
    {
        ((CListBox*)GetDlgItem(IDC_LIST2))->AddString((_bstr_t)pRst->GetCollect("记录簿名称")+" "+(_bstr_t)pRst->GetCollect("记录簿编码"));
         pRst->MoveNext();
    }
    pRst->Close();    pConn->Close();    pRst.Release();    pConn.Release();    CoUninitialize();
三、EXCEL文件读取写入
    ADO方式读取EXCEL文件:strCnn.Format("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=%s;

            Extended Properties='Excel 8.0;HDR=Yes'",strCurPath);

四、MYSQL数据库
     CString query,sHOURR;    
     mysql_query(mysqld,"SET NAMES 'GB2312'");    
     query.Format("insert into %s(时间,小时雨量,分钟雨量) values(\'%s\',\'%s\',\'%s\')",TT,sHOURR,MINR);
     if(mysql_real_query(mysqld,query,query.GetLength()))
         AfxMessageBox(mysql_error(mysqld));  

 

posted @ 2016-11-10 10:24  fyk1Ex  阅读(168)  评论(0)    收藏  举报