ADO 연결 MS SQL

우선 stdafx. h 에서 매크로 로 ADO 대상 만 들 기:
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF") 
 
프로그램 클래스 에서 대화 식 COM 구성 요 소 를 초기 화 합 니 다:
CWinApp::InitInstance();
 AfxEnableControlContainer();
 AfxOleInit();//COM 구성 요소 초기 화 // 표준 초기 화 // 이 기능 을 사용 하지 않 고 줄 이 기 를 원한 다 면
 
작업 데이터베이스 클래스:
\ # pragma once / / 데이터베이스 에 접근 하 는 클래스 클래스 DBAccess {private: ConnectionPtr m ConnectionPtr; / 연결 포인터 RecordsetPtr m RecordsetPtr; / 결과 문자 집합 CString m ConnectString; / 연결 문자열 public: DBAccess (void); ~ DBAccess (void); / / 기록 집합 포인터 RecordsetPtr getRecordsetPtr (void); / / 데이터베이스 BOOL ConnectDB (void) 연결; / / 데이터베이스 연결 을 끊 은 void DisconnectDB (void); / / SQL 문 구 를 실행 하 는 BOOL Select (CString sql); / / 데이터 BOOL 업데이트 (CString sql);}; \ #include "Stdafx. h" \ # include "DBAccess. h" DBAccess:: DBAccess (void) {m ConnectString = L "driver = {SQL Server}; Server = 127.0.1; DATABASE = personMIS; UID = sa; PWD = 123456"; m ConnectionPtr. CreateInstance ( uuidof (Connection));} DBAccess:: ~ DBAccess (void) {} / / 기록 집합 포인터 가 져 오기RecoRecordsetPtr DBAccess:: getRecorecordsetPtr (void) {retm RecoRecordsetPtr;} / / 데이터베이스 에 연결 되 어 있 는 BOOLDBAccess:: ConnectDB (void) {m ConnectonPtr - > ConnectTimeout = 2; HRESUL시간 이 = 2; {hr = m ConnectonPtr - > (m ConnectString. allocSysString (), "", ",", ", adModUnknow알 수 없 음);;} catch ( com erre) {AfxMessageBox (e. 설명 (e..)); 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; (SUCCDEDEDED ((SUCCCD (hr){return 1;} return 0;} / 데이터베이스 에 연 결 된 void DBAccess 를 끊 습 니 다.: DisconnectDB (void) {if (m ConnectionPtr - > State = 1) {m ConnectionPtr - > Close ();} / 데이터베이스 BOOL DBAccess:: Select (CString sql) {if (m ConnectionPtr - > State = 1) {m RecordsetPtr. CreateInstance ( uuidof (Recordset)); m_RecordsetPtr->CursorLocation=adUseClient; _variant_t var; try { m_RecordsetPtr->Open(sql.AllocSysString(),m_ConnectionPtr.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); } catch (_com_error e) { AfxMessageBox(e.Description()); return 0; } if(m_RecordsetPtr->adoEOF) { return 0; } return 1; } return 0; }/ / 업데이트 데이터 BOOL DBAccess::: Update (CString sql) {if (m ConnectionPtr - > State = = 1) { variant t var; try {m ConnectionPtr - > Execute (sql. AllocSysString (), & var, adcmdText);} catch ( com error e) {AfxgeBox (e. Description ()); return 0;} return 1;} return 0;}
 
 결과 가 집 중 된 데 이 터 를 옮 겨 다 니 기:
DBAccess x; if(x.ConnectDB()==1) { if(x.Select(L"select * from student")) { CString str,str1; while(!x.getRecordsetPtr()->adoEOF) { str1.Format(L"%d",x.getRecordsetPtr()->GetCollect(_variant_t("id")).intVal); str+=str1; x.getRecordsetPtr()->MoveNext(); } MessageBox(str); } x.DisconnectDB(); }
 
데이터 형식 변환:
GetCollect 함수 로 되 돌아 오 는 종 류 는 variant 입 니 다.
variant t 에서 다른 유형의 변환:
void ChangeType(
   VARTYPE vartype,
   const _variant_t* pSrc = NULL 
);
VARTYPE   :

   
   
   
   
VT_EMPTY
Variable type is not specified.
VT_NULL
Variable type is NULL.
VT_I2
Variable type is 2-byte signed INT.
VT_I4
Variable type is 4-byte signed INT.
VT_R4
Variable type is 4-byte real.
VT_R8
Variable type is 8-byte real.
VT_CY
Variable type is currency.
VT_DATE
Variable type is date.
VT_BSTR
Variable type is binary string.
VT_DISPATCH
Variable type is IDispatch FAR*.
VT_ERROR
Variable type is SCODE.
VT_BOOL
Variable type is Boolean; True=-1, False=0.
VT_VARIANT
Variable type is VARIANT FAR*.
VT_UNKNOWN
Variable type is IUnknown FAR*.
VT_UI1
Variable type is unsigned char.
VT_RESERVED
This element is reserved.
VT_BYREF
Variable type is a pointer to data.
VT_ARRAY
Variable type is a safe array.

     _variant_t var=_varinat_t("haha");
     var.ChangeType(VT_BSTR);
     CString str=var;
 
 다른 유형 에서 variant t 로 의 변환:
 보통 variant t (값)
예 를 들 어 variant t (50);     주의해 야 할 것 은 이런 방법 은 각 유형 간 에 쉽게 전환 할 수 있다 는 것 이다.
다음은 MSDN 상세 정보.
     _variant_t( )   Constructs an empty _variant_t object, VT_EMPTY.
     _variant_t( VARIANT& varSrc )   Constructs a _variant_t object from a copy of the VARIANT object. The variant type is retained.
  • _variant_t( VARIANT* pVarSrc )   Constructs a _variant_t object from a copy of the VARIANT object. The variant type is retained.
  • _variant_t( _variant_t& var_t_Src )   Constructs a _variant_t object from another _variant_t object. The variant type is retained.
  • _variant_t( VARIANT& varSrc, bool fCopy )   Constructs a _variant_t object from an existing VARIANT object. If fCopy is false, the VARIANT object is attached to the new object without making a copy.
  • _variant_t( short sSrc, VARTYPE vtSrc = VT_I2 )   Constructs a _variant_t object of type VT_I2 or VT_BOOL from a short integer value. Any other VARTYPE results in an E_INVALIDARG error.
  • _variant_t( long lSrc, VARTYPE vtSrc = VT_I4 )   Constructs a _variant_t object of type VT_I4, VT_BOOL, or VT_ERROR from a long integer value. Any other VARTYPE results in an E_INVALIDARG error.
  • _variant_t( float fltSrc )   Constructs a _variant_t object of type VT_R4 from a float numerical value.
  • _variant_t( double dblSrc, VARTYPE vtSrc = VT_R8 )   Constructs a _variant_t object of type VT_R8 or VT_DATE from a double numerical value. Any other VARTYPE results in an E_INVALIDARG error.
  • _variant_t( CY& cySrc )   Constructs a _variant_t object of type VT_CY from a CY object.
  • _variant_t( _bstr_t& bstrSrc )   Constructs a _variant_t object of type VT_BSTR from a _bstr_t object. A new BSTR is allocated.
  • _variant_t( wchar_t *wstrSrc )   Constructs a _variant_t object of type VT_BSTR from a Unicode string. A new BSTR is allocated.
  • _variant_t( char* strSrc )   Constructs a _variant_t object of type VT_BSTR from a string. A new BSTR is allocated.
  • _variant_t( bool bSrc )   Constructs a _variant_t object of type VT_BOOL from a bool value.
  • _variant_t( IUnknown* pIUknownSrc, bool fAddRef = true )   Constructs a _variant_t object of type VT_UNKNOWN from a COM interface pointer. If fAddRef is true, then AddRef is called on the supplied interface pointer to match the call to Release that will occur when the _variant_t object is destroyed. It is up to you to call Release on the supplied interface pointer. If fAddRef is false, this constructor takes ownership of the supplied interface pointer; do not call Release on the supplied interface pointer.
  • _variant_t( IDispatch* pDispSrc, bool fAddRef = true )   Constructs a _variant_t object of type VT_DISPATCH from a COM interface pointer. If fAddRef is true, then AddRef is called on the supplied interface pointer to match the call to Release that will occur when the _variant_t object is destroyed. It is up to you to call Release on the supplied interface pointer. If fAddRef is false, this constructor takes ownership of the supplied interface pointer; do not call Release on the supplied interface pointer.
  • _variant_t( DECIMAL& decSrc )   Constructs a _variant_t object of type VT_DECIMAL from a DECIMAL value.
  • _variant_t( BYTE bSrc )   Constructs a _variant_t object of type VT_UI1 from a BYTE value.
  • 좋은 웹페이지 즐겨찾기