VC under the Access database using ADO connection

  # include <iostream.h> 
  # import "c: \ program files \ common files \ system \ ado \ msado15.dll" no_namespace rename ( "EOF", "adoEOF") 

  int main () ( 

  / / ADO connecting to the database … 
  //———————————————— ——————————— 
  _ConnectionPtr M_pConnection; 

  CoInitialize (NULL); 
  m_pConnection.CreateInstance (__uuidof (Connection)); 

  / / ADO operation in the proposed statement of commonly used to try … catch () to capture the wrong message, 
  / / Because it sometimes will keep some unexpected errors. 
try
  ( 
  / / Open Access for local db1.mdb 
  m_pConnection-> Open ( "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = db1.mdb ","","", adModeUnknown); 

  ) 
  catch (_com_error e) 
  ( 
  cout << "database connection failed, to confirm whether the database db1.mdb under the current path!" <<endl; 
  return FALSE; 
  ) 
  //———————————————— ————————————- 
  / / Build data sets 
  //———————————————— ————————————- 
  _RecordsetPtr M_pRecordset; 
  m_pRecordset.CreateInstance (__uuidof (Recordset)); 

  / / ADO operation in the proposed statement of commonly used to try … catch () to capture the wrong message, 
  / / Because it sometimes will keep some unexpected errors. 
try
  ( 
  m_pRecordset-> Open ( "SELECT * FROM Home2", 
  m_pConnection.GetInterfacePtr (), / / access to the library to guide the IDispatch 
adOpenDynamic,
adLockOptimistic,
adCmdText);
  ) 
  catch (_com_error * e) 
  ( 
  / / AfxMessageBox (e-> ErrorMessage ()); 
  cout <<e-> ErrorMessage () <<endl; 
  ) 

  //———————————————— ————————————– 
  / / Read data 
  //———————————————— ————————————– 
  _variant_t var; 
  char * strID, * strX, * strY; 
  float X, Y; 
try
  ( 
  if (! m_pRecordset-> BOF) 
  m_pRecordset-> MoveFirst (); 
else
  ( 
  cout << "data table is empty" <<endl; 
  return 1; 
  ) 

  / / Reading storage in various fields and to join the list box 
  while (! m_pRecordset-> adoEOF) 
  ( 
  var = m_pRecordset-> GetCollect ( "ID"); 
  if (var.vt! = VT_NULL) 
  strID = _com_util:: ConvertBSTRToString ((_bstr_t) var); / / _variant_t to string 
  var = m_pRecordset-> GetCollect ( "X"); 
  if (var.vt! = VT_NULL) 
  strX = _com_util:: ConvertBSTRToString ((_bstr_t) var); 
  var = m_pRecordset-> GetCollect ( "Y"); 
  if (var.vt! = VT_NULL) 
  strY = _com_util:: ConvertBSTRToString ((_bstr_t) var); 
  cout <<strID << "is" <<strX << "" <<strY <<endl; 

  m_pRecordset-> MoveNext (); 
  ) 
  ) 
  catch (_com_error * e) 
  ( 
  cout <<e-> ErrorMessage () <<endl; 
  ) 

  //———————————————— ————————————– 
  / / Off data sets 
  m_pRecordset-> Close (); 
  m_pRecordset = NULL; 
  //———————————————— ————————————– 
  / / Close database connection 
  //———————————————— ————————————– 
  if (m_pConnection-> State) 
  m_pConnection-> Close (); 
  m_pConnection = NULL; 
  return 0; 
  ) 

  These are some of the source code, a question is very strange, I database from the original name is position, to be accessed is, I had a not-for-Home2 is on the line, do not know how the story » 

  How to join the SQL followed by a corresponding order data sets: 
  CommandPtr intelligent guide, you can use _ ConnectionPtr or _ RecordsetPtr to carry out its mandate, the definition of output parameters, the implementation process or storage SQL statement. 
  — Implementation of SQL statement.    _ CommandPtr to create a guideline for example, then the connections and SQL statement as a parameter, the Executive Execute () method can.    Code as follows: _CommandPtr m_pCommand; 
  m_pCommand.CreateInstance (__uuidof (Command)); 
  m_pCommand-> ActiveConnection = m_pConnection; / / will be given to the link it 
  m_pCommand-> CommandText = "SELECT * FROM DemoTable"; / / SQL statement 
  m_pRecordset = m_pCommand-> Execute (NULL, NULL, adCmdText); / / implementation of SQL statements, set to return to record 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google
  • DZone
  • Netvouz
  • NewsVine
  • Technorati

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Tags: , ,