VC 连接数据库

在StdAfx.h中包含#import "C:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","EndOfFile") rename("BOF","FirstOfFile") #include "icrsint.h" 在相应的部分填入下列代码:

 

_variant_t vFieldValue;  CString strFieldValue;  int AllCount = 0;  // LVCOLUMN *pColumn=new LVCOLUMN;    LVCOLUMN m_Column;  int age = 0;

         // CString sql("select * from serverloadtbl");  m_P.CreateInstance(_uuidof(Connection));  if(m_P->GetState() != adStateOpen)  {      m_P->Open("OrderDishSysOdbc","sa","",NULL);  }    m_R.CreateInstance(_uuidof(Recordset));  m_C.CreateInstance(_uuidof(Command));    if(m_R->State != adStateOpen)  {         m_C->ActiveConnection = m_P;   //Ҫͨ¹ým_R->GetRecordCount()»ñÈ¡¼Ç¼µÄ×ÜÌõÊý£¬±ØÐëÉèÖÃÓαêµÄÊôÐÔ£¬²¢ÇÒÒªÓÃmovenext()±éÀúÊý¾Ý¿â²ÅÄÜ»ñÈ¡µ½×ܼǼÊý   m_R->CursorType = adOpenStatic;   m_R->CursorLocation = adUseClient;   m_R->Open("select * from serverloadtbl",m_P.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);  }  m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | m_list.GetExtendedStyle());  m_list.InsertColumn(0,"·þÎñÔ±±àºÅ",LVCFMT_CENTER,100,-1);  m_list.InsertColumn(1,"µÇ½ÃÜÂë",LVCFMT_CENTER,80,-1);  m_list.InsertColumn(2,"·þÎñÔ±ÐÕÃû",LVCFMT_CENTER,100,-1);  m_list.InsertColumn(3,"·þÎñÔ±ÐÔ±ð",LVCFMT_CENTER,100,-1);  m_list.InsertColumn(4,"·þÎñÔ±ÄêÁä",LVCFMT_CENTER,100,-1);  m_list.InsertColumn(5,"·þÎñÔ±Éí·ÝÖ¤ºÅ",LVCFMT_CENTER,150,-1); // CString test;  // test.Format("%d",m_R->EndOfFile);  // MessageBox(test);     /*      * ·ÀֹûÓÐÊý¾Ý±¨´í      */  if(VARIANT_FALSE != m_R->FirstOfFile)  {   return TRUE;  }

 m_R->MoveNext();

 while(VARIANT_FALSE == m_R->EndOfFile)  {   m_R->MoveNext();  }  // m_P->MoveNext();    AllCount = m_R->GetRecordCount();

 m_R->MoveFirst();

 for(int count = 0; count < m_R->GetRecordCount(); count++)  {      if(VARIANT_FALSE == m_R->EndOfFile)   {    m_list.InsertItem(count,"");    vFieldValue = m_R->GetCollect("ServerNo");    strFieldValue = (char *)_bstr_t(vFieldValue);    m_list.SetItemText(count,0,strFieldValue);

   vFieldValue = m_R->GetCollect("ServerPwd");    strFieldValue = (char *)_bstr_t(vFieldValue);       m_list.SetItemText(count,1,strFieldValue);

   vFieldValue = m_R->GetCollect("ServerName");    strFieldValue = (char *)_bstr_t(vFieldValue);       m_list.SetItemText(count,2,strFieldValue);

   vFieldValue = m_R->GetCollect("ServerSex");    strFieldValue = (char *)_bstr_t(vFieldValue);    m_list.SetItemText(count,3,strFieldValue);

   vFieldValue = m_R->GetCollect("ServerAge");    strFieldValue = (char *)_bstr_t(vFieldValue);    // age = atoi(strFieldValue.GetBuffer(0));    m_list.SetItemText(count,4,strFieldValue);        vFieldValue = m_R->GetCollect("ServerID");    strFieldValue = (char *)_bstr_t(vFieldValue);    m_list.SetItemText(count,5,strFieldValue);

  }   m_R->MoveNext();  }  /*  if(m_pCon->State == adStateOpen)  {   m_pCon->Close();  }*/

 

你可能感兴趣的:(VC 连接数据库)