📄 ado.cpp
字号:
#include "stdafx.h"//实现数据库的连接
#include "ado.h"
ado::ado()
{
::CoInitialize(NULL);//ADO是基于COM技术要进行初始化
try
{
m_pConnection.CreateInstance(__uuidof(Connection));//建立ADO连接的句柄
_bstr_t strConnect="Provider=SQLOLEDB;SERVER=127.0.0.1;Database=db_client;uid=sa;pwd=;";//在类的析构函数中建立数据连接,这是连接字符串
//_bstr_t strConnect="driver={SQL server};server=127.0.0.1;DATABASE=db_Client;uid=sa;pwd=";
//_bstr_t strConnect="dsn=db_client";
//ntServer
m_pConnection->Open(strConnect,"","",0);//打开连接
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
ado::~ado()
{
//m_pRecordset->Close();
/// m_pConnection->Close();
// m_pRecordset=NULL;
// m_pConnection=NULL;
// ::CoUninitialize();
}
bool ado::Open(CString srecordset, UINT adCmd)//利用连接指针打开数据库,此方式打开的记录集无法回滚,可以打开空记录集
{
try{
m_pRecordset=m_pConnection->Execute((_bstr_t)srecordset,NULL,adCmd);//用连接指针打开记录集,并把记录传给记录指针
}
catch(_com_error&e)
{
this->GetErrors(e);
return false;
}
return true;
}
int ado::GetRecordCount()//返回记录集的个数
{
int nCount=0;
try{
m_pRecordset->MoveFirst();
}
catch(...)
{
return 0;
}
if(m_pRecordset->adoEOF)
return 0;
while (!m_pRecordset->adoEOF)//计算记录集个数,判断条件是记录集指针的游标是否指向末端
{
m_pRecordset->MoveNext();
nCount=nCount+1;
}
m_pRecordset->MoveFirst();
return nCount;
}
void ado::GetErrors(_com_error eErrors)//获取执行SQL语句时的出错信息
{
/*CString string;
CFile file;
file.Open("Error.Rxe",CFile::modeWrite|CFile::modeNoTruncate);
ErrorsPtr pErrors=cnn->GetErrors();
if (pErrors->GetCount()==0)
{
string=(char*)(_bstr_t)eErrors.ErrorMessage();
file.Write(string+"\r\n",string.GetLength()+1);
//::AfxMessageBox(string);
}
else
{
for (int i=0;i<pErrors->GetCount();i++)
{
_bstr_t desc=pErrors->GetItem((long)i)->GetDescription();
string=(char*)desc;
file.Write(string+"\r\n",string.GetLength()+1);
//::AfxMessageBox(string);
}
}
file.Close();
*/
ErrorsPtr pErrors=m_pConnection->GetErrors();
if (pErrors->GetCount()==0)
MessageBox(NULL,eErrors.ErrorMessage(),"错 误",MB_OK|MB_ICONEXCLAMATION);
else
{
for (int i=0;i<pErrors->GetCount();i++)//如果有多条语句,将用这个循环输出所有错误
{
_bstr_t desc=pErrors->GetItem((long)i)->GetDescription();
MessageBox(NULL,desc,"错 误",MB_OK|MB_ICONEXCLAMATION);//获得ADO执行时出错的语言描述,并用MessageBox函数通知用户
}
}
}
//_RecordsetPtr&
void ado::rstOpen(CString TSQL)//用记录指针打开一个记录集,打开空记录集时返回错误,如果出错就转向用连接打开记录集
{
try
{
_bstr_t bstrSQL=TSQL.AllocSysString();
m_pRecordset.CreateInstance(__uuidof(Recordset));
//m_pRecordset->Open(bstrSQL,(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);//用记录指针打开一个记录集
}
catch(_com_error e)
{
m_pRecordset=m_pConnection->Execute((_bstr_t)TSQL,NULL,adCmdText);
//return false;
}
//return m_pRecordset;
}
CString ado::GetFieldValue(CString Field)//获取记录集指定列的值
{
_variant_t Thevalue;
CString temp;
Thevalue=m_pRecordset->GetCollect((_bstr_t)Field);//获取记录集指定列的值,并把它传给变量Thevalue
if(Thevalue.vt==VT_EMPTY ||Thevalue.vt==VT_NULL)
temp="";
else
{
temp=(char*)(_bstr_t)Thevalue;
temp.TrimRight();
temp.TrimLeft();
}
return temp;
}
bool ado::MovePrevious()//指向记录集的游标向上移一条
{
try
{
m_pRecordset->MovePrevious();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
return true;
}
bool ado::Move(int nRecordNum)//指向记录集的游标移到指定处
{
try
{
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
m_pRecordset->Move(nRecordNum);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
return true;
}
bool ado::MoveNext()//指向记录集的游标向下移一条
{
try
{
m_pRecordset->MoveNext();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
return true;
}
bool ado::MoveFirst()//指向记录集的游标移到顶部
{
try
{
m_pRecordset->MoveFirst();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
return true;
}
bool ado::MoveLast()//指向记录集的游标移到尾部
{
try
{
m_pRecordset->MoveLast();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
return true;
}
void ado::ExecuteSQL(CString TSQL)//用指向连接的指针执行SQL语句,如果SQL居于有语法错误,就返回响应的错误
{
try
{
m_pConnection->Execute((_bstr_t)TSQL,NULL,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
void ado::close()//关闭指针连接
{
m_pRecordset->Close();
m_pConnection->Close();
m_pRecordset=NULL;
m_pConnection=NULL;
::CoUninitialize();
}
void ado::AddNew()
{
m_pRecordset->AddNew();
}
void ado::Update()
{
m_pRecordset->Update();
}
void ado::SetFieldValue(CString OField,CString value)
{_bstr_t tt=value.AllocSysString();
_bstr_t ss=OField.AllocSysString();
m_pRecordset->PutCollect((_variant_t)ss,(_variant_t)tt);
}
bool ado::recordbof()
{
if(m_pRecordset->BOF)
{
return true;
}else
{
return false;
}
}
bool ado::recordeof()
{
if(m_pRecordset->adoEOF)
{
return true;
}else
{
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -