VC连接数据库

void OnInitAdoConn()
{

  _ConnectionPtr m_pConnection;
   ::CoInitialize(NULL); //初始化COM
   try
   {
      m_pConnection.CreateInstance("ADODB.Connection");
      _bstr_t strConnect = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=testDB;Data Source=WJXTG6C1NWLLITG;";//连接SQL Server 数据库
      //_bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Working\C++\Chat\DB\database.mdb;";//连接Access 数据库
      m_pConnection->Open(strConnect, "", "", adModeUnknown);
   }
   catch (_com_error e)
   {
      AfxMessageBox(e.Description());
   }
}


void ExitConnect()
{
   if (m_pRecordset)
   {
      m_pRecordset->Close();
   }
   m_pConnection->Close();
   ::CoUninitialize();
}

posted on 2013-06-25 23:08  忄坚 zero  阅读(159)  评论(0编辑  收藏  举报

导航