gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:43:30 25 4
gpt4 key购买 nike

所以我从 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 隐式地将未加引号的标识符转换为小写。没有设置 AFAIK 来更改此行为。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com