c# - C# for Postgres 中的 .CommandText 删除双引号
问题描述
我有一个连接到 Postgres DB 的小型 C# 应用程序。所有表名都以大写字母开头,所以我发现解析表的查询需要有双引号。例如,从“表名”中选择 *。我有这个:
using var cmd = new NpgsqlCommand();
cmd.Connection = npgsqlConnection;
string newstring = " \"x\" ";
string tab = newstring.Replace("x", tableName);
string strSQL = string.Format("select * from {0};", tab);
cmd.CommandText = strSQL;
Console.WriteLine(strSQL);// I can see the double quotes here
cmd.ExecuteNonQuery();
我从 cmd.ExecuteNonQuery 得到一个异常,因为它找不到表名。我发现 CommandText 中不包含双引号,因此它找不到 TableX,因为它执行 SELECT * FROM TableX 而不是 SELECT * FROM "TableX"。
任何建议都会有所帮助,谢谢。
解决方案
发现我必须指定架构名称,并且它没有在连接字符串中使用架构名称。架构名称不应包含在双引号中。
这有效:
string newstring = " \"x\" ";
string tab = newstring.Replace("x", tableName);
string strSQL = string.Format("select * from schema.{0};", tab);
cmd.CommandText = strSQL;
这行不通
string newstring = " \"x\" ";
string tab = newstring.Replace("x", schema.tableName);
string strSQL = string.Format("select * from schema.{0};", tab);
cmd.CommandText = strSQL;
在插入时,列也应该用双引号引起来。
推荐阅读
- haskell - 如何使用 regex-compat-tdfa 而不是 regex-compat
- php - php中的sort()如何排列目录结构数组?
- kubernetes - 获取 Kubernentes + Flux 工作负载错误的详细信息?
- ios - 如果从笔尖与情节提要加载 UITableViewCell 框架不同
- python - 未知形状 TensorFlow 对象检测
- javascript - 使用输入和按钮更改图像大小
- database - 使用 id 搜索对象是否比在 mongoose 中使用其他参数更有效?
- charts - 当ag网格为rtl模式时图表样式的问题
- javascript - $.getjson 通过请求正文发送参数不起作用
- typescript - 显式键入泛型函数参数和在 TypeScript 中隐式键入它有什么区别?