C# 和 SQL Server 数据类型对应关系

前些天我在找关于 C# 和 SQL Server 之间的数据类型对应转换的一些信息。找不难,网上都是(但是有些不是很正确,比如有很多网上查出来说 SQL Server 的 image 字段对应的 C# 类型是 Object,但根据我自己的实际使用经验,是byte[]。)
问题是,我现在要做的是 C# 转 SQL Server 类型。但是 C# 相对 SQL Server 是多对一的关系,这时需要考虑多个 SQL Server 类型之间有无包含的关系。他们之间是否可以在程序中始终指定一个类型的值来对其它相关类型的值进行读写?比如:byte[] 对应三个类型,image,timestamp 和 varbinary。能否用varbinary来直接存取其他两个类型的字段的值?在经过测试之后发现可以。而且关系如下表,其中 Priority 就是指当指定的 C# 类型对应多个 SQL Server 类型的时候,代码中所使用的SqlDbType类型。
两者的关系对应如下:
SQL Server
C#
Priority
Bit
bool
TinyInt
byte
Image
byte[]
Timestamp
byte[]
Varbinary
byte[]

DateTime
DateTime

SmallDateTime
DateTime
Decimal
decimal

Money
decimal
SmallMoney
decimal
Float
double
Real
float
Int
int
BigInt
long
Binary
object
SmallInt
short
Char
string
NChar
string
NText
string
NVarchar
string

Text
string
Varchar
string
Xml
string
UniqueIdentifier
Guid
Tags: 

延伸阅读

最新评论

发表评论