connection.cpp

来自「数据库编程」· C++ 代码 · 共 81 行

CPP
81
字号
// Connection.cpp: implementation of the CConnection class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Course.h"
#include "Connection.h"

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

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

CConnection::CConnection()
{

}

CConnection::~CConnection()
{

}
void CConnection::open(){
	try{
		conn.CreateInstance(_uuidof(Connection));
		conn->ConnectionTimeout=10;
	    conn->Open("Provider=OraOLEDB.Oracle.1;Password=liangcha;Persist Security Info=True;User ID=system;Data Source=Course","","",adModeUnknown);
	}
	catch(_com_error *e)
	{
		CString errer;
		errer.Format("出错:%s",e->ErrorMessage());
		AfxMessageBox(errer);
	}
}

bool CConnection::close(){
	if(conn->State){
	    conn->Close();
	    conn=NULL;
        AfxMessageBox("close db connection!");
	    return true;
	}
	return false;
}

_RecordsetPtr CConnection::execute(CString sql)
{
	_RecordsetPtr rs=NULL;
    try{
	    if(rs.CreateInstance(_uuidof(Recordset))==S_OK)
		{
		   rs->Open(_bstr_t(sql),conn.GetInterfacePtr(),adOpenStatic, adLockOptimistic,adCmdText); 
		}
	}
	catch(_com_error *e){
	   AfxMessageBox(e->ErrorMessage());
	}
    return rs;
}

bool CConnection::executeUpdate(CString sql)
{
	try
	{
		conn->Execute(_bstr_t(sql),NULL,adCmdText);
	}
	catch(_com_error *e)
	{
		 AfxMessageBox(e->Description());
     	 return FALSE;
	}
	return true;
}

⌨️ 快捷键说明

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