//
// MODULE: CBBAdoConnection.h
//
// AUTHOR: Dave Yong
//
// mailto: 376040925@qq.com
//
// Date: 10/29/2009
//
// Version 1.0
//
// CopyRight@Dave Yong
//
#ifndef _BBADOCONNECT_H
#define _BBADOCONNECT_H
#if !defined(__AFXADO_H)
#pragma warning(disable: 4146)//忽略此警告
//BOF 指示当前记录位置位于 Recordset 对象的第一个记录之前。
//EOF 指示当前记录位置位于 Recordset 对象的最后一个记录之后。
//返回值
#import "msado15.dll" no_namespace rename ("EOF", "adoEOF") /
rename ("LockTypeEnum", "adoLockTypeEnum") /
rename ("DataTypeEnum", "adoDataTypeEnum") /
rename ("FieldAttributeEnum", "adoFieldAttributeEnum") /
rename ("EditModeEnum", "adoEditModeEnum") /
rename ("RecordStatusEnum", "adoRecordStatusEnum") /
rename ("ParameterDirectionEnum", "adoParameterDirectionEnum")
#endif // !defined(__AFXADO_H)
//////////////////////////////////////////////////////////////////////
//connection
class CBBAdoConnection
{
public:
CBBAdoConnection();
virtual ~CBBAdoConnection();
public:
BOOL OpenConnection(CString strConnString ); //打开数据库
void CloseConnection(); // 关闭数据库连接
BOOL OpenRecordset(CString strConnString ); //打开数据集
void CloseRecordset(); //关闭数据集
void Close(); //关闭数据库连接和数据集
BOOL IsRcordsetClose(); //数据集是否关闭
_RecordsetPtr getRecordset(); //获得数据集
DWORD GetRecordCount(_RecordsetPtr m_pRs); //记录集个数
_ConnectionPtr getConnection(); //获得连接
BOOL IsOpen(); //判断是否打开
BOOL FindFirst(LPCTSTR lpFind);
CString getstrConnection(); //获得连接字符串
int SetConnTimeOut(long lTimeOut); // 设置连接超时
int SetCommTimeOut(long lTimeOut); // 设置命令执行超时
int ExecuteSQL(LPCSTR szSQL); // 执行SQL操作,不返回记录集
BOOL IsConnectionOpen() //判断数据库是否连接
{return m_pConn != NULL && m_pConn->GetState() != adStateClosed;};
// 返回是否处在连接状态 TRUE-关闭 FALSE-打开
BOOL CBBAdoConnection::IsConnectClose()
{
return (m_pConn==NULL)||(m_pConn->State==adStateClosed);
}
//charge end
BOOL IsEof()
{return m_pRecordset->adoEOF == VARIANT_TRUE;};
BOOL IsEOF()
{return m_pRecordset->adoEOF == VARIANT_TRUE;};
//charge begin
BOOL IsBof()
{return m_pRecordset->BOF == VARIANT_TRUE;};
BOOL IsBOF()
{return m_pRecordset->BOF == VARIANT_TRUE;};
void MoveFirst() //移到第一个记录
{m_pRecordset->MoveFirst();};
void MoveNext() //移到下一条记录
{m_pRecordset->MoveNext();};
void MovePrevious() // 移到前一条记录
{m_pRecordset->MovePrevious();};
void MoveLast() //移到最后一条记录
{m_pRecordset->MoveLast();};
CString IntToStr(int nVal);
CString LongToStr(long lVal);
CString ULongToStr(unsigned long ulVal);
CString DblToStr(double dblVal, int ndigits = 20);
CString DblToStr(float fltVal);
private:
enum ERRORFrom {
ErrFormOpenConnsction,
ErrFromOpenRecordset,
ErrFormCloseConnection,
ErrFormTanslation
};
_ConnectionPtr m_pConn;
_RecordsetPtr m_pRecordset;
CString str;
protected:
void ReportError(int nERRORfrom);
};
#endif
//
// MODULE: CBBAdoConnection.cpp
//
// AUTHOR: Dave Yong
//
// mailto: 376040925@qq.com
//
// Date: 10/29/2009
//
// Version 1.0
//
// CopyRight@Dave Yong
//
#include "stdafx.h" //添加
#include "AdoConnection.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBBAdoConnection::CBBAdoConnection()
{
m_pConn=NULL;//连接指针空
}
CBBAdoConnection::~CBBAdoConnection()
{
// 关闭连接
CloseConnection();
}
_RecordsetPtr CBBAdoConnection::getRecordset()
{
return m_pRecordset;
}//获得数据集
_ConnectionPtr CBBAdoConnection::getConnection()
{
return m_pConn;
}//获得连接
// 设置连接超时
int CBBAdoConnection::SetConnTimeOut(long lTimeOut)
{
return m_pConn->put_ConnectionTimeout(lTimeOut);
}
int CBBAdoConnection::SetCommTimeOut(long lTimeOut)
{
return m_pConn->put_CommandTimeout(lTimeOut);
}
BOOL CBBAdoConnection::OpenConnection(CString strConnString)//open connection
{
try
{
str=strConnString;
m_pConn.CreateInstance(__uuidof(Connection));
m_pConn->Open((_bstr_t)strConnString,"","",adModeUnknown);
}
catch(_com_error e)
{
//ReportError(ErrFormOpenConnsction);
CString errormessage;
errormessage.Format("连接数据库失败!/r错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
return false;
}
return true;
}
void CBBAdoConnection::CloseConnection()
{
try
{
if(m_pConn!=NULL)
{
if (m_pConn->State!=adStateClosed)
m_pConn->Close();
// delete m_pConn;
m_pConn=NULL;
}
}
catch(_com_error)
{
ReportError(ErrFormCloseConnection);
}
catch(...)
{
AfxMessageBox("关闭数据库连接未知错误!");
}
}
BOOL CBBAdoConnection::OpenRecordset(CString strConnString )//open recordset
{
_variant_t RecordsAffected;
try
{
m_pRecordset.CreateInstance("ADODB.Recordset"); //为Recordset对象创建实例
_bstr_t strCmd=strConnString;
m_pRecordset=m_pConn->Execute(strCmd,&RecordsAffected,adCmdText);//指示提供者应按命令的文本定义计算 CommandText
} //&RecordsAffected提供者向其返回操作所影响的记录数目
catch(_com_error &e)
{
AfxMessageBox(e.Description());
return false;
}
return true;
}
void CBBAdoConnection::CloseRecordset()//open recordset
{
try
{
if(IsOpen())
{
if ( m_pRecordset->State!=adStateClosed)
m_pRecordset->Close();
// delete m_pRecordset;
m_pRecordset=NULL;
}
}
catch (_com_error)
{
AfxMessageBox("关闭数据集连接未知错误!");
}
}
DWORD CBBAdoConnection::GetRecordCount(_RecordsetPtr m_pRs)
{
DWORD numRows = 0;
numRows = m_pRs->GetRecordCount();
if(numRows == -1)
{
numRows = 0;
if(m_pRs->adoEOF != VARIANT_TRUE)
m_pRs->MoveFirst();
while(m_pRs->adoEOF != VARIANT_TRUE)
{
numRows++;
m_pRs->MoveNext();
}
if(numRows > 0)
m_pRs->MoveFirst();
}
return numRows;
}
BOOL CBBAdoConnection::IsOpen()//charge open or not
{
if(m_pRecordset != NULL && IsConnectionOpen())
return m_pRecordset->GetState() != adStateClosed;
return FALSE;
}
CString CBBAdoConnection::getstrConnection(){
return str;
}
BOOL CBBAdoConnection::IsRcordsetClose()
{
if(m_pRecordset==NULL) //为什么关闭之后用if(m_pRecordset->State!=adStateClosed)不行呢
return true;
}
void CBBAdoConnection::Close()
{
CloseRecordset();
CloseConnection();
::CoUninitialize;
}
int CBBAdoConnection::ExecuteSQL(LPCSTR szSQL)
{
// VARIANT vEffect;
try
{
m_pConn->Execute(szSQL,NULL,adCmdText|adExecuteNoRecords);
return TRUE;
}
catch(_com_error)
{
ReportError(ErrFormTanslation);
return FALSE;
}
// return vEffect.lVal;
}
// 报告错误
void CBBAdoConnection::ReportError(int nERRORfrom)
{
switch(nERRORfrom)
{
case ErrFormOpenConnsction:
#ifdef _DEBUG // 调试试时显示相应的错误信息
try
{
for(long l=0;lErrors->Count;l++)
{
ErrorPtr pErr;
pErr=m_pConn->Errors->GetItem(l);
CString str;
str=(char*)pErr->Description;
MessageBox(NULL,str,"连接失败",MB_OK|MB_ICONINFORMATION);
}
}
catch(...)
{
MessageBox(NULL,"数据库连接未知错误,无法捕捉具体错误信息1!","错误",MB_ICONINFORMATION);
}
#else
MessageBox(NULL,"连接数据失败,请检查网络和数据库设置是否正确!","连接失败",MB_OK|MB_ICONINFORMATION);
#endif
break;
case ErrFromOpenRecordset:
#ifdef _DEBUG
try
{
for(long i=0;iErrors->Count;i++)
{
ErrorPtr pErr;
pErr=m_pConn->Errors->GetItem(i);
AfxMessageBox(pErr->Description);
}
}
catch(...)
{
MessageBox(NULL,"数据库连接未知错误,无法捕捉具体错误信息2!","错误",MB_ICONINFORMATION);
}
#else
MessageBox(NULL,"打开数据库失败,请检查网络,并尝试重新连接数据库!","记录失败",MB_OK|MB_ICONINFORMATION);
#endif
break;
case ErrFormCloseConnection:
#ifdef _DEBUG // 调试时显示相应的错误信息
try
{
for(long l=0;lErrors->Count;l++)
{
ErrorPtr pErr;
pErr=m_pConn->Errors->GetItem(l);
CString str;
str=(char*)pErr->Description;
MessageBox(NULL,str,"连接失败",MB_OK|MB_ICONINFORMATION);
}
}
catch(...)
{
MessageBox(NULL,"数据库连接未知错误,无法捕捉具体错误信息3!","错误",MB_ICONINFORMATION);
}
#else
MessageBox(NULL,"关闭数据库连接异常","关闭异常",MB_OK|MB_ICONINFORMATION);
#endif
break;
case ErrFormTanslation:
#ifdef _DEBUG
try
{
for(long i=0;iErrors->Count;i++)
{
ErrorPtr pErr;
pErr=m_pConn->Errors->GetItem(i);
AfxMessageBox(pErr->Description);
}
}
catch(...)
{
MessageBox(NULL,"数据库连接未知错误,无法捕捉具体错误信息4!","错误",MB_ICONINFORMATION);
}
#else
MessageBox(NULL,"数据库执行任务失败,请检查数据库。","任务失败",MB_OK|MB_ICONINFORMATION);
#endif
break;
default:
break;
}
}
//用 法:char *itoa(int value, char *string, int radix);
//详细解释:itoa是英文integer to array(将int整型数转化为一个字符串,并将值保存在数组string中)的缩写.
//
//itoa该函数的头文件是"stdlib.h"
//value: 待转化的整数。
//radix: 是基数的意思,即先将value转化为radix进制的数,范围介于2-36,比如10表示10进制,16表示16进制。
// * string: 保存转换后得到的字符串。
// 返回值:
// char * : 指向生成的字符串, 同*string。
CString CBBAdoConnection::IntToStr(int nVal)
{
CString strRet;
char buff[10];
itoa(nVal, buff, 10);
strRet = buff;
return strRet;
}
CString CBBAdoConnection::LongToStr(long lVal)
{
CString strRet;
char buff[20];
ltoa(lVal, buff, 10);
strRet = buff;
return strRet;
}
CString CBBAdoConnection::ULongToStr(unsigned long ulVal)
{
CString strRet;
char buff[20];
ultoa(ulVal, buff, 10);
strRet = buff;
return strRet;
}
/*
功 能: 把浮点数转换成字符串
用 法: char *gcvt(double value, int ndigit, char *buf);
gcvt函数把一个浮点值转换成一个字符串(包括一个小数点和可能的
符号字节)并存储该字符串在buffer中。该buffer应足够大以便容纳转换
的值加上结尾的空格字符,它是自动添加的。如果一个缓冲区的尺寸为
digits的尺寸+1,该函数覆盖该缓冲区的末尾。这是因为转换的字符串包
括一个小数点以及可能包含符号和指数信息。不提供上溢出。gcvt试图
以十进制格式产生digits数字,如果不可能,它以指数格式产生digits数字,
在转换时可能截除尾部的0。
*/
CString CBBAdoConnection::DblToStr(double dblVal, int ndigits)
{
CString strRet;
char buff[50];
_gcvt(dblVal, ndigits, buff);
strRet = buff;
return strRet;
}
CString CBBAdoConnection::DblToStr(float fltVal)
{
CString strRet = _T("");
char buff[50];
_gcvt(fltVal, 10, buff);
strRet = buff;
return strRet;
}
VC++连接SQL数据库
最新推荐文章于 2013-06-24 10:39:30 发布
324

被折叠的 条评论
为什么被折叠?



