PostgreSQL和C#数据类型

73

我在搜索 PostgreSQL 和 C# 之间的类型转换表,但是没有找到任何信息。 如果我有时间,我会研究上面表格中的空单元格。 但是如果您知道拥有这些信息的网页,我非常感谢您的帮助。

Postgre Type --->C# Type

bigint --->Int64

bigserial --->

bit [ (n) ] --->Byte[]

bit varying [ (n) ] --->Byte

boolean --->Boolean

box --->

bytea --->Byte[]

character varying [ (n) ] ---> String

character --->String

cidr

circle 

date --->DateTime

double precision --->Double

inet

integer --->Int32

interval [ (p) ] --->TimeSpan

line 

lseg 

macaddr

money

numeric [ (p, s) ] --->Decimal

decimal [ (p, s) ] --->Decimal

path  

point 

polygon 

real --->Single

smallint --->Int16

serial 

text --->String

time [ (p) ] [ without time zone ] --->

time [ (p) ] with time zone --->

timestamp [ (p) ] [ without time zone ] --->

timestamp [ (p) ] with time zone --->

tsquery 

tsvector 

txid_snapshot

uuid --->Guid

xml   
1个回答

136

也许你可以通过查看Npgsql文档找到相关的内容,这是一种用于PostgreSQL的.NET数据提供程序的实现。

文档中的这个页面实际上包含了你要找的完整表格。搜索“4. Current Npgsql Status” - “Supported data types”。有一个很好的表格,列出了所有的PostgreSQL数据类型以及它们在.NET中的对应类型。

Postgresql  NpgsqlDbType System.DbType Enum .NET System Type
----------  ------------ ------------------ ----------------
int8        Bigint       Int64              Int64
bool        Boolean      Boolean            Boolean
bytea       Bytea        Binary             Byte[]
date        Date         Date               DateTime
float8      Double       Double             Double
int4        Integer      Int32              Int32
money       Money        Decimal            Decimal
numeric     Numeric      Decimal            Decimal
float4      Real         Single             Single
int2        Smallint     Int16              Int16
text        Text         String             String
time        Time         Time               DateTime
timetz      Time         Time               DateTime
timestamp   Timestamp    DateTime           DateTime
timestamptz TimestampTZ  DateTime           DateTime
interval    Interval     Object             TimeSpan
varchar     Varchar      String             String
inet        Inet         Object             IPAddress
bit         Bit          Boolean            Boolean
uuid        Uuid         Guid               Guid
array       Array        Object             Array

1
不确定这是否已经过时,但我在将DateTime对象转换为Postgresql“time”类型时遇到了一些问题。http://stackoverflow.com/questions/6129558/nhibernate-postgresql-datetime-to-time-conversion/6138382。我需要使用TimeSpan对象才能将其保存为Postgresql时间对象。 - Marcus King
@Spilarix 谢谢。已修复链接。 - splattne
这个表格是错误的,DB类型TIME可以是负数,实际上它是SQL推荐用于存储一个时间戳减去另一个时间戳的结果(即存储时间跨度的推荐类型)。DB TIME必须映射到C# TimeSpan,因为C# DateTime无法容纳负值,因此会转换失败。DB Time被指定为既可用于存储绝对时间也可用于存储相对时间,并且有效范围从-hh:mm到+hh:mm,其中我认为hh大约是850小时,但我不记得了。 - AnorZaken
你好!关于 PostgreSQL 的 jsonb 类型怎么样?我有一个数据列其类型为jsonb。我将它映射到一个字符串属性中,但是似乎出现了错误 Npgsql.PostgresException: 42804:列“data”的类型为jsonb,但表达式的类型为文本。 - kevinob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接