c# - 为什么我需要使用 Npgsql for C# 和 PostgreSQL 引用列和表?

标签 c# sql-server postgresql npgsql

因此,我将从 SQL Server 2008 R2(在 Windows Server 2012 上)切换到 PostgreSQL 9.5(再次在 Windows 上),并且我所有现有的 SQL 语句都以此格式编写:

INSERT INTO Address (Address1, Address4, Address5, Postcode, IsoCountryCode) 
VALUES(@P0, @P1, @P2, @P3, @P4)

我使用 Ngpsql v3.1.3 作为我的 .NET 库,通过 C# 与 PostgreSQL 对话。执行相同的语句会导致找不到表Address 的错误。为了让它工作,我必须将所有列名和表名用双引号括起来,如下所示:

INSERT INTO \"Address\" (\"Address1\", \"Address4\", \"Address5\", \"Postcode\", \"IsoCountryCode\") 
VALUES(@P0, @P1, @P2, @P3, @P4)

我的应用程序中有超过 600 个 SQL 查询,我不想遍历所有查询并在各处注入(inject)引号。

Npgsql 文档示例不使用引号,所以我错过了什么吗?看到这里Npgsql Documentation Example

具体来说,他们的示例是这样的:

INSERT INTO data (some_field) VALUES ('Hello world')

所以我想我不必双引号。谁能帮我解答一下吗?

最佳答案

正如评论中提到的,这就是 PostgreSQL 的工作原理 - PostgreSQL 隐式地将不带引号的标识符转换为小写。据我所知,没有设置可以更改此行为。 The docs on lexical structure声明:

Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)

关于c# - 为什么我需要使用 Npgsql for C# 和 PostgreSQL 引用列和表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640693/

相关文章:

sql - 具有可变列名的动态更新语句

sql - 如何使用 PostgreSQL 更新表中的记录?

c# - 如何更改文件权限以阻止 Vista 写入 VirtualStore 目录中的文件?

javascript - 在javascript中声明可观察到的动态 knockout

c# - 如何在 MVVM View 模型中处理 C# WPF 线程

c# - 何时在 Entity Framework Code First 中使用存储过程和 SQL 查询

sql-server - 数据库丢失!寻找根本原因

postgresql jdbc、pgobject可用类型、数组类型

database - 为什么两个表不能有同名索引?

c# - 在 Xamarin Forms 的某个页面中隐藏 TabBar