connection.cpp

来自「库存管理系统 用来管理仓库的」· C++ 代码 · 共 81 行

CPP
81
字号
#include "StdAfx.h"
#include "Connection.h"

CConnection::CConnection(CString strConnection)
{
   AfxOleInit();
   conn=NULL;
   m_strConnection=strConnection;
}

CConnection::~CConnection()
{
}
bool CConnection::Open()
{   
	try
	{
		HRESULT hcon;
		HRESULT result;
		hcon=conn.CreateInstance(_uuidof(Connection));
		if(hcon==S_OK)
			result=conn->Open(_bstr_t(m_strConnection),"","",0);
		if(result==S_OK)
			return true;
	}
	catch(_com_error e)
	{
        AfxMessageBox("连接数据库出错!");
	}
	return false;
}

bool CConnection::Close()
{
	if(isConnected())
	{
	  conn->Close();
	  conn=NULL;
      AfxMessageBox("关闭数据库!");
	  return true;
	}
	return false;
}

_RecordsetPtr CConnection::Execute(CString strSql)
{
	_RecordsetPtr rs=NULL;
	try
	{
		if(rs.CreateInstance(_uuidof(Recordset))==S_OK)
		{

			rs->Open(_variant_t(strSql),conn.GetInterfacePtr(),adOpenDynamic, adLockOptimistic,adCmdText); 
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
    return rs;
}
bool CConnection::isConnected()
{
	if(conn->State)
		return true;
	else 
		return false;
}
bool CConnection::executeUpdate(CString strSql)
{
	try
	{
		conn->Execute(_bstr_t(strSql),NULL,adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->Description());
		return false;
	}
	return true;
}

⌨️ 快捷键说明

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