阳光城愉景湾业主论坛:ADOConnect.cpp
来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 18:37:34
#include "StdAfx.h"
#include "ADOConn.h"CADOConn::CADOConn(void)
{
}CADOConn::~CADOConn(void)
{
}//初始化,连接数据库
void CADOConn::OnInitADOConn()
{ //初始化OLD/COM库环境 ::CoInitialize(NULL);
try { //创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection"); //设置连接字符串
_bstr_t strConnect ="Provider=SQLOLEDB.1;Persist Security Info=False;User Id=sa; Password=1234;Initial Catalog=qjycl;Data Source=cjp;"; //SERVER和UID,PWD根据实际情况来设置
m_pConnection->Open(strConnect,"","",adModeUnknown);
} //捕捉异常
catch(_com_error e)
{ //显示错误信息
AfxMessageBox(e.Description());
}
}//执行查询
_RecordsetPtr& CADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{ //连接数据库,如果Connection对象为空,则重新连接数据库
if (m_pConnection == NULL)
OnInitADOConn(); //创建记录集对象
m_pRecordset.CreateInstance(_uuidof(Recordset)); //取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
} //返回记录集
return m_pRecordset;
}//执行SQL语句
BOOL CADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
_variant_t RecordsAffected; try { //是否已连接数据库
if (m_pConnection == NULL)
OnInitADOConn();
//Connection对象的Execute方法(_bstr_t CommandText,VARIANT *RecordsAffected,long Options)
//CommandText是命令字符串,通常是SQL命令
//RecordsAffected操作完成后所影响的行数
//Options表示的是CommandText类型,adCmdText-文本命令,adCmdTable-表名
//adCmdProc-存储过程,adCmdUnknown-未知 m_pConnection->Execute(bstrSQL,NULL,adCmdText); return true;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
} //断开数据库连接
void CADOConn::ExitConnect()
{ //关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close(); //释放环境
::CoUninitialize();
}
#include "ADOConn.h"CADOConn::CADOConn(void)
{
}CADOConn::~CADOConn(void)
{
}//初始化,连接数据库
void CADOConn::OnInitADOConn()
{ //初始化OLD/COM库环境 ::CoInitialize(NULL);
try { //创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection"); //设置连接字符串
_bstr_t strConnect ="Provider=SQLOLEDB.1;Persist Security Info=False;User Id=sa; Password=1234;Initial Catalog=qjycl;Data Source=cjp;"; //SERVER和UID,PWD根据实际情况来设置
m_pConnection->Open(strConnect,"","",adModeUnknown);
} //捕捉异常
catch(_com_error e)
{ //显示错误信息
AfxMessageBox(e.Description());
}
}//执行查询
_RecordsetPtr& CADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{ //连接数据库,如果Connection对象为空,则重新连接数据库
if (m_pConnection == NULL)
OnInitADOConn(); //创建记录集对象
m_pRecordset.CreateInstance(_uuidof(Recordset)); //取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
} //返回记录集
return m_pRecordset;
}//执行SQL语句
BOOL CADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
_variant_t RecordsAffected; try { //是否已连接数据库
if (m_pConnection == NULL)
OnInitADOConn();
//Connection对象的Execute方法(_bstr_t CommandText,VARIANT *RecordsAffected,long Options)
//CommandText是命令字符串,通常是SQL命令
//RecordsAffected操作完成后所影响的行数
//Options表示的是CommandText类型,adCmdText-文本命令,adCmdTable-表名
//adCmdProc-存储过程,adCmdUnknown-未知 m_pConnection->Execute(bstrSQL,NULL,adCmdText); return true;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
} //断开数据库连接
void CADOConn::ExitConnect()
{ //关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close(); //释放环境
::CoUninitialize();
}