首先我有一个CString(MFC)
CString csSQL
格式化sql字符串时,例如
csSQL.Format( szFormat, szTableName, szColumn, szValue, intValue )
我需要处理szValue中的特殊字符,所以我需要编写一个新类MySQLString
mySQLcs.FormatSQL( szFormat, szTableName, szColumn, szValue, intValue )
具有功能
csSQL.Format( szFormat, szTableName, szColumn, HandleSpecialChar(szValue), intValue )
但是因为参数格式函数接受不固定。我发现很难。有没有解决方案?
答案 0 :(得分:1)
您不向CString类添加处理程序。
你写了一个(新)函数
CString FormatSQLString(CString const& format, CString const& tableName, CString const& columnName, CString const& value) {
CString csSQL;
return csSQL.Format(format, tableName, columnName, HandleSpecialChar(value));
}
和进一步包装该函数(例如CString GetUpdateStmt(tableName, columnName, value)
),这样您就不必在代码中遍布格式字符串了。
当您编写以结构化方式使用SQL语句输入的代码时,就像使用我提议的函数集合一样,通过使用或创建SQLQueryStringBuilder
(组成名称) )类,那么你就不会有必须将某些东西塞进不属于那里的字符串格式中。