Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2321105
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: C/C++

2011-11-10 21:37:23

头文件 ADO.h:
  1. // ADO.h: interface for the CADO class.
  2. //
  3. //////////////////////////////////////////////////////////////////////

  4. #if !defined(AFX_ADO_H__EE0962C4_DC53_489D_981F_B112A39F9C1B__INCLUDED_)
  5. #define AFX_ADO_H__EE0962C4_DC53_489D_981F_B112A39F9C1B__INCLUDED_

  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000


  9. #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")


  10. class CADOConnection
  11. {
  12. private:
  13.     static void InitADO();
  14.     static void UnInitADO();
  15. protected:
  16.     _ConnectionPtr m_Connection;
  17. public:
  18.     BOOL IsOpen();
  19.     _ConnectionPtr GetConnection();
  20.     CString GetSQLConStr(CString IP,CString DBName);
  21.     BOOL Open(CString ConStr);
  22.     CADOConnection();
  23.     virtual ~CADOConnection();

  24. };

  25. class CADODataSet
  26. {
  27. protected:
  28.     _RecordsetPtr m_DataSet;
  29.     CADOConnection *m_Connection;
  30. public:
  31.     BOOL Open(CString SQLStr,int LockType);
  32.     void Delete();
  33.     int GetRecordNo();
  34.     void move(int nIndex);
  35.     void Save();
  36.     void SetFieldValue(CString FieldName,_variant_t Value);
  37.     void AddNew();
  38.     BOOL Next();
  39.     FieldsPtr GetFields();
  40.     int GetRecordCount();
  41.     void SetConnection(CADOConnection *pCon);
  42.     BOOL Open(CString SQLStr);

  43.     CADODataSet();
  44.     virtual ~CADODataSet();
  45. private:
  46.     BOOL IsOpen();
  47. };


  48. CADOConnection * GetConnection();
  49. #endif // !defined(AFX_ADO_H__EE0962C4_DC53_489D_981F_B112A39F9C1B__INCLUDED_)

 

定义 ADO.CPP :

 

  1. // ADO.cpp: implementation of the CADO class.
  2. //
  3. //////////////////////////////////////////////////////////////////////

  4. #include "stdafx.h"
  5. #include "ADO.h"
  6. #include <comdef.h>

  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif

  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. int ConCount = 0;
  16. CADOConnection g_Connection;//全局数据库连接对象

  17. CADOConnection * GetConnection()
  18. {
  19.     return &g_Connection;
  20. }

  21. CADOConnection::CADOConnection()
  22. {
  23.     InitADO();
  24.     m_Connection.CreateInstance("ADODB.Connection");
  25. }

  26. CADOConnection::~CADOConnection()
  27. {
  28.     if (IsOpen())
  29.         m_Connection->Close();
  30.     m_Connection = NULL;
  31.     UnInitADO();
  32. }

  33. void CADOConnection::InitADO()
  34. {
  35.         if (ConCount++ == 0)
  36.             CoInitialize(NULL);
  37. };
  38. void CADOConnection::UnInitADO()
  39. {
  40.         if (--ConCount == 0)
  41.              CoUninitialize();
  42. };

  43. BOOL CADOConnection::Open(CString ConStr)
  44. {
  45.     if (IsOpen())
  46.         m_Connection->Close();
  47.     m_Connection->Open((_bstr_t)ConStr,"","",adModeUnknown);
  48.     return IsOpen();
  49. }

  50. CString CADOConnection::GetSQLConStr(CString IP, CString DBName)
  51. {
  52.     CString Str;
  53.     Str.Format("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=%s;Data Source=%s",DBName,IP);
  54.     return Str;
  55. }


  56. _ConnectionPtr CADOConnection::GetConnection()
  57. {
  58.     return m_Connection;
  59. }

  60. BOOL CADOConnection::IsOpen()
  61. {
  62.     long State;
  63.     m_Connection->get_State(&State);
  64.     if (State == adStateOpen)
  65.         return true;
  66.     return false;
  67. }

  68. ////////////////////////////////////////
  69. CADODataSet::CADODataSet()
  70. {
  71.     m_DataSet.CreateInstance("ADODB.Recordset");
  72. }

  73. CADODataSet::~CADODataSet()
  74. {
  75.     if (IsOpen())
  76.         m_DataSet->Close();
  77.     m_DataSet = NULL;
  78.     m_Connection = NULL;
  79. }

  80. void CADODataSet::SetConnection(CADOConnection *pCon)
  81. {
  82.     m_Connection = pCon;
  83. }

  84. int CADODataSet::GetRecordCount()
  85. {
  86.     if (IsOpen())
  87.         return m_DataSet->GetRecordCount();
  88.     else
  89.         return 0;
  90. }

  91. BOOL CADODataSet::Open(CString SQLStr)
  92. {
  93.     if (IsOpen())
  94.         m_DataSet->Close();
  95.     //*/
  96.     m_DataSet->Open(_bstr_t(SQLStr),
  97.             _variant_t((IDispatch*)g_Connection.GetConnection(), true),
  98.             adOpenKeyset, adLockOptimistic, adCmdText);
  99.     return IsOpen();
  100.     //*/

  101. }

  102. BOOL CADODataSet::IsOpen()
  103. {
  104.     long State;
  105.     m_DataSet->get_State(&State);
  106.     if (State == adStateOpen)
  107.         return true;
  108.     
  109.     return false;
  110. }

  111. FieldsPtr CADODataSet::GetFields()
  112. {
  113.     return m_DataSet->GetFields();
  114. }

  115. BOOL CADODataSet::Next()
  116. {
  117.     if (m_DataSet->adoEOF)
  118.         return false;
  119.     m_DataSet->MoveNext();
  120.     return true;
  121. }

  122. void CADODataSet::AddNew()
  123. {
  124.     m_DataSet->AddNew();
  125. }

  126. void CADODataSet::SetFieldValue(CString FieldName, _variant_t Value)
  127. {
  128.     m_DataSet->PutCollect((_bstr_t)FieldName,Value);
  129. }

  130. void CADODataSet::Save()
  131. {
  132.     m_DataSet->Update();
  133. }

  134. void CADODataSet::move(int nIndex)
  135. {
  136.     m_DataSet->MoveFirst();
  137.     m_DataSet->Move(nIndex);
  138. }

  139. int CADODataSet::GetRecordNo()
  140. {
  141.     return m_DataSet->AbsolutePosition;
  142. }

  143. void CADODataSet::Delete()
  144. {
  145.     m_DataSet->Delete(adAffectCurrent);
  146. }

  147. BOOL CADODataSet::Open(CString SQLStr, int LockType)
  148. {
  149.     if (IsOpen())
  150.         m_DataSet->Close();
  151.     //*/
  152.     m_DataSet->Open(_bstr_t(SQLStr),
  153.             _variant_t((IDispatch*)g_Connection.GetConnection(), true),
  154.             adOpenKeyset,(LockTypeEnum) LockType, adCmdText);
  155.     return IsOpen();
  156. }
阅读(6034) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~