MFC工控项目实例三十五读取数据库数据

点击按钮打开文件夹中的数据文件生成曲线

相关代码

void CSEAL_PRESSUREDlg::OnTesReport() 
{
 	
CFileDialog dlgOpen(TRUE/*TRUE打开,FALSE保存*/,
        0,
        0,
        OFN_NOCHANGEDIR|OFN_FILEMUSTEXIST,
        "All Files(mdb.*)|*.*||",//文件过滤器
        NULL);
  
CString  mdb_1, mdb_2;
    dlgOpen.m_ofn.lpstrInitialDir =theApp.m_DataPath;
    if(IDOK == dlgOpen.DoModal())
	{
             mdb_1=dlgOpen.GetPathName();
             mdb_2=dlgOpen.GetFileTitle();    
			 UpdateData(true); 
 _variant_t RecordsAffected; 
    //链接数据库
       try
	   {
		//创建连接对象实例
		m_pConnection.CreateInstance("ADODB.Connection");	
	CString strConnect="DRIVER={Microsoft Access Driver (*.mdb)};\
			uid=;pwd=;DBQ="+mdb_1;	
		m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);  
	   }	
          catch(...)
		  {
		MessageBox("连接数据库失败");
		return;
		  }    
            try
			{
       m_pRecordset.CreateInstance("ADODB.Recordset"); //为Recordset对象创建实例
       _bstr_t strCmd = "SELECT * FROM  "+mdb_2;
       m_pRecordset = m_pConnection->Execute(strCmd, &RecordsAffected, adCmdText);
			}
              catch(_com_error &e)
			  {
       AfxMessageBox(e.Description());
			  }
			   m_ctrlPlot_1.RemoveAllChannels();
               m_ctrlPlot_1.AddChannel();
                while(!m_pRecordset->adoEOF)
				{	
	 	 double X=(double)atof((char*)(_bstr_t)m_pRecordset->GetCollect("X值"));
		 double Y=(double)atof((char*)(_bstr_t)m_pRecordset->GetCollect("Y值"));
	 	 m_ctrlPlot_1.GetChannel(0).AddXY(X/10, Y);	
		m_pRecordset->MoveNext();	//将记录集指针移动到下一条记
				}
	}	
}

上一篇:Qt Designer Ui设计


下一篇:STM32编码器接口及编码器测速模板代码