CString.Format使用一例

本文解析了一个使用CString.Format的代码案例,介绍了如何通过指定资源文件中预定义字符串ID来格式化字符串,解决了运行时出现的问题。

本人不才,最近遇到一名使用CString.Format的代码。编译通得过,运行却直接失败。


	CString str;
	str.Format(1);

最初还怀疑编译器,为什么不直接报编码错。查询Format接口说明才知上面用法完全符合语法。

void __cdecl Format(
   UINT nFormatID,
   [, argument]...
);
void __cdecl Format(
   PCXSTR pszFormat,
   [, argument]...
);

nFormatID

The string resource identifier that contains the format-control string.


原来啊,可以指定一个预先在资源文件的StringTable中定义好的字符串的ID作为参数。
这下释然了,不知道的就Mark一下吧。


#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、付费专栏及课程。

余额充值