在对_ConnectionPtr指针Close时需要try-catch

本文讨论了在使用_ConnectionPtr连接数据库时遇到的问题。当尝试通过m_pConnection连接到SQL2005数据库失败后,调用Close方法释放资源时可能会抛出未知错误。文章强调了在代码中需要对这类异常进行捕获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

_ConnectionPtr m_pConnection;

当m_pConnection.ConDB("hp-PC\\SQL2005","at","sa","123");没有连接上数据库时,

对m_PConnection->Close()时会抛出UnKnown error,需要进行捕捉。

#include "pch.h" #include "DBManager.h" #include <comdef.h> #include <comutil.h> // 只在CPP文件中导入ADO #import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" \ no_namespace \ rename("EOF", "adoEOF") \ rename("BOF", "adoBOF") // 使用COM智能指针 typedef _com_ptr_t<_com_IIID<_Recordset, &__uuidof(_Recordset)> > RecordsetPtr; typedef _com_ptr_t<_com_IIID<_Connection, &__uuidof(_Connection)> > ConnectionPtr; CDBManager::CDBManager() : m_pConnection(nullptr) { } CDBManager::~CDBManager() { Close(); } CString CDBManager::GetDatabasePath() { TCHAR szPath[MAX_PATH]; GetModuleFileName(NULL, szPath, MAX_PATH); CString strPath(szPath); int pos = strPath.ReverseFind('\\'); if (pos != -1) { strPath = strPath.Left(pos + 1) + _T("EmployeeDB.mdb"); } return strPath; } bool CDBManager::Initialize() { try { if (m_pConnection) return true; // 创建连接对象 ConnectionPtr* pConn = new ConnectionPtr; pConn->CreateInstance(__uuidof(Connection)); // 设置连接字符串 CString strConnect; strConnect.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;"), GetDatabasePath()); // 打开连接 pConn->Open((_bstr_t)strConnect, "", "", adConnectUnspecified); m_pConnection = pConn; return true; } catch (_com_error& e) { CString err; err.Format(_T("Database connection failed: %s"), (LPCTSTR)e.Description()); AfxMessageBox(err); return false; } } void CDBManager::Close() { if (m_pConnection) { ConnectionPtr* pConn = (ConnectionPtr*)m_pConnection; if (pConn->GetInterfacePtr() && pConn->GetInterfacePtr()->State == adStateOpen) { pConn->Close(); } delete pConn; m_pConnection = nullptr; } } std::vector<Employee> CDBManager::GetAllEmployees() { std::vector<Employee> employees; if (!m_pConnection) return employees; try { ConnectionPtr* pConn = (ConnectionPtr*)m_pConnection; RecordsetPtr pRs; pRs.CreateInstance(__uuidof(Recordset)); pRs->Open("SELECT * FROM tblEmployee", pConn->GetInterfacePtr(), adOpenStatic, adLockReadOnly, adCmdText); while (!pRs->adoEOF) { Employee emp; emp.ID = (LPCTSTR)(_bstr_t)pRs->Fields->GetItem("EmployeeID")->Value; emp.Name = (LPCTSTR)(_bstr_t)pRs->Fields->GetItem("Name")->Value; emp.NickName = (LPCTSTR)(_bstr_t)pRs->Fields->GetItem("NickName")->Value; emp.Department = (LPCTSTR)(_bstr_t)pRs->Fields->GetItem("Department")->Value; emp.Salary = pRs->Fields->GetItem("Salary")->Value; employees.push_back(emp); pRs->MoveNext(); } pRs->Close(); } catch (_com_error& e) { CString err; err.Format(_T("Query failed: %s"), (LPCTSTR)e.Description()); AfxMessageBox(err); } return employees; } bool CDBManager::AddEmployee(const Employee& emp) { if (!m_pConnection) return false; CString sql; sql.Format(_T("INSERT INTO tblEmployee (EmployeeID, Name, NickName, Department, Salary) ") _T("VALUES ('%s', '%s', '%s', '%s', %.2f)"), emp.ID, emp.Name, emp.NickName, emp.Department, emp.Salary); return ExecuteSQL(sql); } bool CDBManager::UpdateEmployee(const Employee& emp) { if (!m_pConnection) return false; CString sql; sql.Format(_T("UPDATE tblEmployee SET ") _T("Name = '%s', ") _T("NickName = '%s', ") _T("Department = '%s', ") _T("Salary = %.2f ") _T("WHERE EmployeeID = '%s'"), emp.Name, emp.NickName, emp.Department, emp.Salary, emp.ID); return ExecuteSQL(sql); } bool CDBManager::DeleteEmployee(const CString& id) { if (!m_pConnection) return false; CString sql; sql.Format(_T("DELETE FROM tblEmployee WHERE EmployeeID = '%s'"), id); return ExecuteSQL(sql); } bool CDBManager::ExecuteSQL(const CString& sql) { try { ConnectionPtr* pConn = (ConnectionPtr*)m_pConnection; pConn->Execute((_bstr_t)sql, NULL, adCmdText); return true; } catch (_com_error& e) { CString err; err.Format(_T("SQL execution failed: %s\nSQL: %s"), (LPCTSTR)e.Description(), (LPCTSTR)sql); AfxMessageBox(err); return false; } }为什么 open,close,execute 不是成员
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值