我正在尝试修复代码中的单引号错误:
std::string Index;
connection->Open();
String^ sTableName = gcnew String(TableName.c_str());
String^ insertstring = String::Format("INSERT INTO {0} (idx, rec, date) VALUES (@idx, @rec, getdate())", sTableName);
SqlCommand^ command = gcnew SqlCommand(insertstring, connection);
String^ idx = gcnew String(Index.c_str());
command->Parameters->Add("@idx", SqlDbType::VarChar)->Value = idx;
错误是如果 idx="that's",SQL 会失败,说明存在语法错误。显然,问题出在报价单上。但是一些谷歌搜索表明使用参数是使用引号的方法。如果类型是 TEXT 而不是 VARCHAR,则 SqlParameter 运行良好。
除了手动将字符串中的引号符号数量加倍之外,还有其他解决方案吗?
更新:我尝试在 SQL Management Studio 中手动编辑此字段,但它不允许在 VARCHAR 字段中使用单引号。这在 SQL 中正常吗?