这几篇是是在MFC框架下做保险项目的学习心得,只学了两周,但是还是学了很多知识。
下面介绍一下用ADO来进行数据库的各种操作。
1、数据库连接和打开
这次项目用的是SQL Server数据库,ADO连接数据遇到最重要的问题就是找到连接字符串,经过亲身经历各种尝试,在网上发现了一种无bug的方法,就是通过新建ado.udl文件,然后打开填写相应的数据库后用记事本打开即可得到连接字符串。下面是我封装的一个打开数据库的方法:
BOOL CDateOperator::OpenDateBase()
{
if(NULL == m_pConnection){
AfxMessageBox(L"Create Instance failed!");
return FALSE;
}
if(m_pConnection){
try
{
const _bstr_t strSRC= "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mydb;Data Source=LI5CH\\SQLEXPRESS";
HRESULT hr = m_pConnection->Open(strSRC,L"",L"",-1);
}
catch(_com_error &e)
{
CString errormsg;
errormsg.Format(L"关闭连接——操作错误!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormsg);
}
}
return TRUE;
}
2、ADO如何执行sql语句
BOOL CDateOperator::ExcuteSQL(CString strSQL){
if(NULL == m_pConnection) return FALSE;
try
{
HRESULT hr = m_pConnection->Execute(_bstr_t(strSQL),NULL,-1);
}
catch(_com_error &e)
{
return FALSE;
}
return TRUE;
}
3、ADO如何插入记录
BOOL CDateOperator::Insert_db(DEMO objuser)
{
if(NULL == m_pConnection) return FALSE;
_RecordsetPtr m_pRecordset;
HRESULT hr = m_pRecordset.CreateInstance(L"ADODB.Recordset");
if(FAILED(hr)) return FALSE;
CString strSQL = L"SELECT User_ID, User_Name FROM fate";
hr = m_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
if(FAILED(hr)){
m_pRecordset.Release();
return FALSE;
}
try
{
m_pRecordset->AddNew();
}
catch(_com_error &e)
{
m_pRecordset->Close();
m_pRecordset.Release();
return FALSE;
}
// byte byData[10000];
try
{
m_pRecordset->PutCollect("User_ID",_variant_t(objuser.User_ID));
m_pRecordset->PutCollect("User_Name",_variant_t(objuser.User_Name));
}
catch(_com_error &e)
{
m_pRecordset->Close();
m_pRecordset.Release();
return FALSE;
}
m_pRecordset->Update();
m_pRecordset->Close();
m_pRecordset.Release();
return TRUE;
}
4、ADO如何查询数据
BOOL CDateOperator::Select_db(vector<DEMO>& vecObjUser)
{
if(NULL == m_pConnection) return FALSE;
_RecordsetPtr m_pRecordset;
HRESULT hr = m_pRecordset.CreateInstance(L"ADODB.Recordset");
if(FAILED(hr)) return FALSE;
vecObjUser.clear();
CString strSQL = L"SELECT User_ID, User_Name FROM fate";
hr = m_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
if(FAILED(hr)){
m_pRecordset.Release();
return FALSE;
}
VARIANT_BOOL bRet = m_pRecordset->GetadoEOF();
while(!bRet){
_variant_t varUserID = m_pRecordset->GetCollect(L"User_ID");
_variant_t varUserName = m_pRecordset->GetCollect(L"User_Name");
DEMO objUser;
objUser.User_ID=varUserID.intVal;
_tcscpy(objUser.User_Name,(TCHAR*)(_bstr_t)varUserName);
vecObjUser.push_back(objUser);
m_pRecordset->MoveNext();
bRet = m_pRecordset->GetadoEOF();
}
m_pRecordset->Close();
m_pRecordset.Release();
return TRUE;
}
5、ADO如何更新数据
BOOL CDateOperator::update_db(DEMO objuser)
{
if(NULL == m_pConnection) return FALSE;
_RecordsetPtr m_pRecordset;
HRESULT hr = m_pRecordset.CreateInstance(L"ADODB.Recordset");
if(FAILED(hr)) return FALSE;
CString strSQL;
strSQL.Format(L"SELECT User_ID, User_Name FROM fate WHERE User_ID=%d", objuser.User_ID);
hr = m_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
if(FAILED(hr)){
m_pRecordset.Release();
return FALSE;
}
// byte byData[10000];
try
{
m_pRecordset->PutCollect("User_Name",_variant_t(objuser.User_Name));
}
catch(_com_error &e)
{
m_pRecordset->Close();
m_pRecordset.Release();
return FALSE;
}
m_pRecordset->Update();
m_pRecordset->Close();
m_pRecordset.Release();
return TRUE;
}
6、下面贴一下构造和析构函数
CDateOperator::CDateOperator(void)
{
try
{
HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");
if(FAILED(hr))
{
AfxMessageBox(L"Create Instance failed!");
m_pConnection = NULL;
}
}
catch(_com_error &e)
{
}
}
CDateOperator::~CDateOperator(void)
{
if(m_pConnection)
{
try
{
HRESULT hr = m_pConnection->Close();
}
catch(_com_error &e)
{
}
}
m_pConnection.Release();
m_pConnection = NULL;
}