C语言连接数据库

时间:2022-07-25 07:24:53

举个例子,连接SQL:
  // 打开数据库
  
strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=%s;Jet OLEDB:Database Password=%s"),
m_strUnEntryptMdbFilePath,m_strMDBPassword);
  // 创建连接
  HRESULT hr = m_pConnection.CreateInstance(_uuidof(Connection));
  _ConnectionPtr m_pConnection->Open(m_strDBClass,_T(""),_T(""),adConnectUnspecified);
  // 声明表单指针
  _RecordsetPtr pBandRecordset;
  pBandRecordset.CreateInstance(__uuidof(Recordset));
  // 执行语句
  CString strSQL(L"SELECT * FROM [Band]");
  m_pConnection->Execute((LPCTSTR)strSQL,NULL,0);
  // 提取某一项 例如BandInfo
  int iBandInfo = wcscmp(colum, L"BandInfo");
  while(!recordsetPtr->adoEOF)
  {
  var = recordsetPtr->GetCollect(colum);
  if(var.vt != VT_NULL)
  strName = (LPCSTR)_bstr_t(var);
  recordsetPtr->MoveNext();
  }