C# - SQL 命令文本中的双引号

C# - Double quotes in SQL commandtext

我正在编写一个关于执行查询并在 datagridview 中显示结果的程序,查询如下: select * 来自“20000_es_asset”.tablename; 数据库是 HP Vertica,这是我的 C# 代码:

string strAsset = @"""20000_es_asset""";
string strSQL = "select * from " + strAsset + "." + table + ";";
VerticaCommand command = _conn.CreateCommand();
command.CommandText = strSQL;

strSQL中有两个双引号,问题是CommandText好像没有解析转义字符直接移动,CommandText的调试信息是: select * 来自\"20000_es_asset\".tablename; 异常:Vertica.Data.dll 中的“Vertica.Data.VerticaClient.VerticaException”,[42601] 错误:语法错误 我怎样才能摆脱这个“\”?提前致谢!

string strSQL = string.Format("select * from {0}.{1};", "\"20000_es_asset\"", table);

PS- 您应该始终将查询参数化。

您需要使用方括号转义 strAsset 中的双引号 ""[]

string strAsset = @"[""20000_es_asset""]";
string strSQL = "select * from " + strAsset + "." + table + ";";
VerticaCommand command = _conn.CreateCommand();
command.CommandText = strSQL;