Fork me on GitHub

CSRobot gen:mssql-c#类型映射

  CSRobot的gen命令,有一个参数--map,是指数据库字段类型到实体类型映射,本例是sql server到csharp的类型映射:

SQL Server

C#

bigint

Int64

binary

Byte[]

bit

Boolean

char

String,Char[]

date 

DateTime

datetime

DateTime

datetime2

DateTime

datetimeoffset

DateTimeOffset

Decimal

小数

FILESTREAM  attribute (varbinary(max))

Byte[]

FLOAT

【-1.79E + 308 至 -2.23E - 308、0 以及 2.23E - 308 至 1.79E + 308】

Double

【±5.0 × 10−324 到 ±1.7 × 10308】

image

Byte[]

int

Int32

money

小数

nchar

String,Char[]

ntext

String,Char[]

numeric

小数

nvarchar

String,Char[]

real

Single或float

rowversion

Byte[]

smalldatetime

DateTime

smallint

Int16

smallmoney

小数

sql_variant

Object 2

text

String,Char[]

time

TimeSpan

timestamp

Byte[]

tinyint

Byte

uniqueidentifier

Guid

varbinary

Byte[]

varchar

String,Char[]

xml

Xml

 

  在表格有“小数”字样,这里的意思是要根据数据库定义的具体精度,转换成对应的c#小数类型,下例是c#中三种小数类型的范围和精度,共参考:

 static void Main(string[] args)
 {
     Console.WriteLine("double:");
     double d1 = 0.0123456789012345678901234567890123456789d;
     Console.WriteLine(d1);
     double d2 = 1234567890123456789012345678901234567890.0123456789012345678901234567890123456789d;
     Console.WriteLine(d2);
     Console.WriteLine();
     Console.WriteLine("float:");
     float f1 = 0.0123456789012345678901234567890123456789f;
     Console.WriteLine(f1);
     float f2 = 123456789012345678901234567890123456789.0123456789012345678901234567890123456789f;
     Console.WriteLine(f2);
     Console.WriteLine();
     Console.WriteLine("decimal:");
     decimal m1 = 0.0123456789012345678901234567890123456789m;
     Console.WriteLine(m1);
     decimal m2 = 12345678901234567890123456789.0123456789012345678901234567890123456789m;
     Console.WriteLine(m2);
}

结果:


double:

0.012345678901234568

1.2345678901234568E+39

 

float:

0.012345679

1.2345679E+38

 

decimal:

0.0123456789012345678901234568

12345678901234567890123456789


 

  另外对于sqlserver中的一复杂类型,在c#中就得定义具体的实体类来对应了。

 

  想要更快更方便的了解相关知识,可以关注微信公众号 
 

 

 

posted @ 2022-02-04 09:08  桂素伟  阅读(36)  评论(0编辑  收藏  举报