myconnection.cpp

来自「本程序使用Visual C++6.0编写」· C++ 代码 · 共 129 行

CPP
129
字号
// MyConnection.cpp: implementation of the CMyConnection class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyConnection.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyConnection::CMyConnection()
{
   IsOpen=false;
}

CMyConnection::~CMyConnection()
{

}

 _ConnectionPtr CMyConnection::GetConnection()
 {
    if(IsOpen)return m_pConnection;
	else return NULL;
 }

bool CMyConnection::Open(CString FilePath)
{
   ::CoInitialize(NULL);
   m_pConnection=NULL;
   m_pConnection.CreateInstance(_uuidof(Connection));   

   //生成连接字符串
   CString LinkString;   
   LinkString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
   LinkString+=FilePath;
   LinkString+=";Persist Security Info=False";
   m_pConnection->ConnectionString=(_bstr_t)LinkString;

   try
   {
	   m_pConnection->CursorLocation=adUseClient; //游标类型 
       m_pConnection->Open(L"",   L"",   L"",   -1);   
   }
   catch(_com_error&e)
   {
      ErrorsPtr pErrors=m_pConnection->GetErrors();
	  if(pErrors->GetCount()==0)
	  {
	     CString ErrMsg=e.ErrorMessage();
		 MessageBox(NULL,"发生错误:\n\n"+ErrMsg,"系统提示",MB_OK|MB_ICONEXCLAMATION);
		 return false;
	  }
      else
	  {
	     for(int i=0;i<pErrors->GetCount();i++)
		 {
			 _bstr_t desc=pErrors->GetItem((long)i)->GetDescription();
		     MessageBox(NULL,"发生错误:\n\n"+desc,"系统提示",MB_OK|MB_ICONEXCLAMATION);
			 return false;
		 }
	  }
   }
   IsOpen=true;
   return true;
}

void CMyConnection::Close()
{
     m_pConnection->Close();
	 IsOpen=false;
	 ::CoUninitialize();
}


//SQL Server2000
bool CMyConnection::Open(CString IP,CString User,CString Password,CString Database) 
{
   ::CoInitialize(NULL);
   
   m_pConnection.CreateInstance("ADODB.Connection");   
   //生成连接字符串
   CString LinkString;
   LinkString.Format("Provider=SQLOLEDB;Server=%s;uid=%s;pwd=%s;Database=%s",IP,User,Password,Database); 
   m_pConnection->ConnectionString=(_bstr_t)LinkString;
   
   //MessageBox(NULL,LinkString,"张密提示",MB_OK|MB_ICONEXCLAMATION);
  
   try
   {
	   m_pConnection->CursorLocation=adUseClient; //游标类型 
       m_pConnection->Open(L"",   L"",   L"",   adConnectUnspecified);   
   }
   catch(_com_error&e)
   {
      ErrorsPtr pErrors=m_pConnection->GetErrors();
	  if(pErrors->GetCount()==0)
	  {
	     CString ErrMsg=e.ErrorMessage();
		 MessageBox(NULL,"数据库连接时发生错误:\n\n"+ErrMsg,"系统提示",MB_OK|MB_ICONEXCLAMATION);
		 return false;
	  }
      else
	  {
	     for(int i=0;i<pErrors->GetCount();i++)
		 {
			 _bstr_t desc=pErrors->GetItem((long)i)->GetDescription();
		     MessageBox(NULL,"数据库连接发生错误1:\n\n"+desc,"系统提示",MB_OK|MB_ICONEXCLAMATION);
			 return false;
		 }
	  }
   }
   IsOpen=true;
   return true;
}

void CMyConnection::Execute(CString SQLstr)
{
     if(m_pConnection)
	 {
	    m_pConnection->Execute(_bstr_t(SQLstr),NULL,adCmdText);
	 }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?