TNewADO.cpp
上传用户:haiweijt
上传日期:2018-02-23
资源大小:8195k
文件大小:2k
- #include "TNewADO.h"
- #include <stdio.h>
- TNewADO::TNewADO(void)
- {
- m_pConnection = NULL;
- m_IsConnect = FALSE;
- }
- TNewADO::~TNewADO(void)
- {
- }
- BOOL TNewADO::Open(const char* IP, const char* User, const char* PWD, const char* DB)
- {
- strcpy(m_IP, IP);
- strcpy(m_User, User);
- strcpy(m_PWD, PWD);
- strcpy(m_DB, DB);
- return TRUE;
- }
- void TNewADO::Close()
- {
-
- }
- BOOL TNewADO::Connect()
- {
- ::CoInitialize(NULL);
- char strConn[256];
- sprintf(strConn, "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=%s;Password=%s;Data Source=%s;Initial Catalog=%s",
- m_User, m_PWD, m_IP, m_DB);
- HRESULT hRes = m_pConnection.CreateInstance(__uuidof(Connection));
- if(m_pConnection == NULL)
- {
- m_IsConnect = FALSE;
- return FALSE;
- }
- try
- {
- m_pConnection->Open ( strConn , "" , "" , adModeUnknown );
- m_IsConnect = TRUE;
- return TRUE;
- }
- catch ( _com_error &e )
- {
- m_IsConnect = FALSE;
- return FALSE;
- }
- }
- void TNewADO::Disconnect()
- {
- if ( m_pConnection != NULL)
- {
- if ( m_pConnection->State & adStateOpen)
- {
- m_pConnection->Close ( );
- // m_pConnection.Release ( );
- }
- m_pConnection = NULL;
- }
- ::CoUninitialize();
- m_IsConnect = FALSE;
- }
- int TNewADO::Execute(const char *SQL)
- {
- if (SQL == NULL)
- return 3;
- if (m_pConnection == NULL)
- {
- if (!Connect())
- return 2;
- }
- if ( m_pConnection )
- {
- try
- {
- DWORD tt = GetTickCount();
- _CommandPtr pCommand;
- pCommand.CreateInstance(__uuidof(Command));
- pCommand->ActiveConnection = m_pConnection; // 将库连接赋于它
- pCommand->CommandText = SQL; // SQL语句
- pCommand->Execute(NULL, NULL,adCmdText);
- //m_pConnection->CursorLocation = adUseClient;
- //m_pConnection->Execute ( SQL , NULL , adExecuteNoRecords );
- //char buf[256];
- //memcpy(buf, SQL, 128);
- //buf[128] = ' ';
- //LogInfo3("execute time = %d, %s",GetTickCount() - tt, buf);
- return 0;
- }
- catch( _com_error &e )
- {
- switch(e.Error())
- {
- case -2147217900:
- case -2147217865:
- return 1;
- case -2147467259:
- Disconnect();
- return 2;
- default:
- Disconnect();
- return 1;
- }
- }
- }
- else
- {
- return 2;
- }
- }