tuiliji/GenAlogrithm/relus/ADOConn.cpp

158 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include"ADOConn.h"
#include<iostream>
ADOConn::ADOConn(void)
{
}
ADOConn::~ADOConn(void)
{
}
void ADOConn::OnInitADOConn(_bstr_t add)
{
//初始化ole/com库环境
::CoInitialize(NULL);
try
{
//创建connection对象
m_pConnection.CreateInstance("ADODB.Connection");
add1 = add;
_bstr_t FileName = add;//".\\AllData.mdb"
_bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName;
m_pConnection->Open(strConnect, "", "", adModeUnknown);
//m_pConnection.CreateInstance("ADODB.Connection");
////设置连接字符串
//m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=lolic.accdb;Persist Security Info=False ", "", "", adModeUnknown);
//设置连接字符串
}
//捕捉异常
catch (_com_error e)
{
//显示错误信息
cout << e.Description() << endl;
}
}
void ADOConn::ExitConnect(void)
{
//关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
//释放环境
::CoUninitialize();
}
void ADOConn::GetRecordSet(_bstr_t bstrSql, int c)
{
try
{
if (!Query((_bstr_t)bstrSql))
{
//执行失败,返回错误
cout << "错误!!!\n" << endl;
return ;
}
//连接数据库如果connection是空则重新连接数据库
if (m_pConnection == NULL)
OnInitADOConn(add1);
//创建记录对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
//取得表中的记录
m_pRecordset->Open(bstrSql, m_pConnection.GetInterfacePtr(), adOpenDynamic,
adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
cout << e.Description() << endl;
}
search = m_pRecordset;//返回记录集
}
bool ADOConn::Query(_bstr_t bstrSQL)
{
try
{
//链接数据库如果connection对象空则重新链接数据库
if (m_pConnection == NULL)
OnInitADOConn(add1);
//创建记录集对象
m_pRecordset.CreateInstance(_uuidof(Recordset));
//取得标准记录
m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
return true;
}
catch (_com_error e)
{
return false;
}
}
string ADOConn::search_RES(_variant_t resid)
{
_bstr_t strRecord;
string c;
while (!search->adoEOF)
{
strRecord = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect(resid);
c = (string)strRecord;
}
return c;
}
_RecordsetPtr ADOConn::GetRecordSet(_bstr_t bstrSql)
{
try
{
//连接数据库如果connection是空则重新连接数据库
if (m_pConnection == NULL)
OnInitADOConn(add1);
//创建记录对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
//取得表中的记录
m_pRecordset->Open(bstrSql, m_pConnection.GetInterfacePtr(), adOpenDynamic,
adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
cout << e.Description() << endl;
}
//返回记录集
return m_pRecordset;
}
bool ADOConn::ExecuteSQL(_bstr_t bstrSql)
{
_variant_t recordsAffected;
try
{
//数据库是否已连接
if (m_pConnection == NULL)
OnInitADOConn(add1);
m_pConnection->Execute(bstrSql, NULL, adCmdText);
return true;
}
catch (_com_error e)
{
cout << e.Description() << endl;
return false;
}
return false;
}