SQL Server中数据类型对应C#中数据类型
在SQL Server 2008中新建数据表的时候有33种数据类型可选,下面分别列举了这些类型对应的C#数据类型
//------------------------------------------------------------------------------
// .NET Framework 版本4.6目前不支持数据类型“hierarchyid”
// .NET Framework 版本4.6目前不支持数据类型“sql_variant”
// SQL Server限定同一张表中只能有一个时间戳(timestamp)列
//------------------------------------------------------------------------------ using System;
using System.Collections.Generic; public partial class TypeTest
{
public Nullable<long> bigint_Null { get; set; }
public long bigint { get; set; }
public byte[] binary_Null { get; set; }
public byte[] binary { get; set; }
public Nullable<bool> bit_Null { get; set; }
public bool bit { get; set; }
public string char_Null { get; set; }
public string @char { get; set; }
public Nullable<System.DateTime> date_Null { get; set; }
public System.DateTime date { get; set; }
public Nullable<System.DateTime> datetime_Null { get; set; }
public System.DateTime datetime { get; set; }
public Nullable<System.DateTime> datetime2_Null { get; set; }
public System.DateTime datetime2 { get; set; }
public Nullable<System.DateTimeOffset> datetimeoffset_Null { get; set; }
public System.DateTimeOffset datetimeoffset { get; set; }
public Nullable<decimal> decimal_Null { get; set; }
public decimal @decimal { get; set; }
public Nullable<double> float_Null { get; set; }
public double @float { get; set; }
public System.Data.Spatial.DbGeography geography_Null { get; set; }
public System.Data.Spatial.DbGeography geography { get; set; }
public System.Data.Spatial.DbGeometry geometry_Null { get; set; }
public System.Data.Spatial.DbGeometry geometry { get; set; }
public byte[] image_Null { get; set; }
public byte[] image { get; set; }
public Nullable<int> int_Null { get; set; }
public int @int { get; set; }
public Nullable<decimal> money_Null { get; set; }
public decimal money { get; set; }
public string nchar_Null { get; set; }
public string nchar { get; set; }
public string ntext_Null { get; set; }
public string ntext { get; set; }
public Nullable<decimal> numeric_Null { get; set; }
public decimal numeric { get; set; }
public string nvarchar_Null { get; set; }
public string nvarchar { get; set; }
public Nullable<float> real_Null { get; set; }
public float real { get; set; }
public Nullable<System.DateTime> smalldatetime_Null { get; set; }
public System.DateTime smalldatetime { get; set; }
public Nullable<short> smallint_Null { get; set; }
public short smallint { get; set; }
public Nullable<decimal> smallmoney_Null { get; set; }
public decimal smallmoney { get; set; }
public string text_Null { get; set; }
public string text { get; set; }
public Nullable<System.TimeSpan> time_Null { get; set; }
public System.TimeSpan time { get; set; }
public byte[] timestamp_Null { get; set; }
public Nullable<byte> tinyint_Null { get; set; }
public byte tinyint { get; set; }
public Nullable<System.Guid> uniqueidentifier_Null { get; set; }
public System.Guid uniqueidentifier { get; set; }
public byte[] varbinary_Null { get; set; }
public byte[] varbinary { get; set; }
public string varchar_Null { get; set; }
public string varchar { get; set; }
public string xml_Null { get; set; }
public string xml { get; set; }
}
CREATE TABLE [dbo].[TypeTest](
[bigint_Null] [bigint] NULL,
[bigint] [bigint] NOT NULL,
[binary_Null] [binary](50) NULL,
[binary] [binary](50) NOT NULL,
[bit_Null] [bit] NULL,
[bit] [bit] NOT NULL,
[char_Null] [char](10) NULL,
[char] [char](10) NOT NULL,
[date_Null] [date] NULL,
[date] [date] NOT NULL,
[datetime_Null] [datetime] NULL,
[datetime] [datetime] NOT NULL,
[datetime2_Null] [datetime2](7) NULL,
[datetime2] [datetime2](7) NOT NULL,
[datetimeoffset_Null] [datetimeoffset](7) NULL,
[datetimeoffset] [datetimeoffset](7) NOT NULL,
[decimal_Null] [decimal](18, 0) NULL,
[decimal] [decimal](18, 0) NOT NULL,
[float_Null] [float] NULL,
[float] [float] NOT NULL,
[geography_Null] [geography] NULL,
[geography] [geography] NOT NULL,
[geometry_Null] [geometry] NULL,
[geometry] [geometry] NOT NULL,
[hierarchyid_Null] [hierarchyid] NULL,
[hierarchyid] [hierarchyid] NOT NULL,
[image_Null] [image] NULL,
[image] [image] NOT NULL,
[int_Null] [int] NULL,
[int] [int] NOT NULL,
[money_Null] [money] NULL,
[money] [money] NOT NULL,
[nchar_Null] [nchar](10) NULL,
[nchar] [nchar](10) NOT NULL,
[ntext_Null] [ntext] NULL,
[ntext] [ntext] NOT NULL,
[numeric_Null] [numeric](18, 0) NULL,
[numeric] [numeric](18, 0) NOT NULL,
[nvarchar_Null] [nvarchar](max) NULL,
[nvarchar] [nvarchar](max) NOT NULL,
[real_Null] [real] NULL,
[real] [real] NOT NULL,
[smalldatetime_Null] [smalldatetime] NULL,
[smalldatetime] [smalldatetime] NOT NULL,
[smallint_Null] [smallint] NULL,
[smallint] [smallint] NOT NULL,
[smallmoney_Null] [smallmoney] NULL,
[smallmoney] [smallmoney] NOT NULL,
[sql_variant_Null] [sql_variant] NULL,
[sql_variant] [sql_variant] NOT NULL,
[text_Null] [text] NULL,
[text] [text] NOT NULL,
[time_Null] [time](7) NULL,
[time] [time](7) NOT NULL,
[timestamp_Null] [timestamp] NULL,
[tinyint_Null] [tinyint] NULL,
[tinyint] [tinyint] NOT NULL,
[uniqueidentifier_Null] [uniqueidentifier] NULL,
[uniqueidentifier] [uniqueidentifier] NOT NULL,
[varbinary_Null] [varbinary](max) NULL,
[varbinary] [varbinary](max) NOT NULL,
[varchar_Null] [varchar](max) NULL,
[varchar] [varchar](max) NOT NULL,
[xml_Null] [xml] NULL,
[xml] [xml] NOT NULL,
CONSTRAINT [PK_TypeTest] PRIMARY KEY CLUSTERED
(
[bigint] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO
以下是用于生成实体类(C#)的SQL脚本:
-- 表名
DECLARE @TableName NVARCHAR(50)
SET @TableName = 'SettlementStatistics' -- 用于控制生成的属于是否区分Null
-- 例:@IgnoreNull为TRUE时 public int Id { get; set; }
-- 例:@IgnoreNull为False时 public int? Id { get; set; }
DECLARE @IgnoreNull NVARCHAR(50)
SET @IgnoreNull = 'FALSE' -- 'TRUE/FALSE' DECLARE @ObjectId BIGINT
SET @ObjectId = OBJECT_ID(@TableName,N'U') IF OBJECT_ID(N'Tempdb..#Model',N'U') IS NOT NULL
DROP TABLE #Model
CREATE TABLE #Model(Field NVARCHAR(200)) DECLARE @MaxColumnIndex INT
SELECT @MaxColumnIndex = Max(column_id) FROM sys.columns WHERE object_id = @ObjectId DECLARE @Index INT = 1 WHILE @Index <= @MaxColumnIndex
BEGIN
INSERT INTO #Model(Field) VALUES('/// <summary>') IF EXISTS(SELECT * FROM sys.extended_properties WHERE major_id = @ObjectId AND minor_id = @Index)
BEGIN
INSERT INTO #Model(Field)
SELECT '/// ' + CONVERT(NVARCHAR(200),value) FROM sys.extended_properties WHERE major_id = @ObjectId AND minor_id = @Index
END
ELSE
BEGIN
INSERT INTO #Model(Field) VALUES('///')
END INSERT INTO #Model(Field) VALUES('/// </summary>') INSERT INTO #Model(Field)
SELECT 'public ' + (CASE [Types].name
WHEN 'image' THEN 'byte[]'
WHEN 'text' THEN 'string'
WHEN 'uniqueidentifier' THEN 'Guid'
WHEN 'date' THEN 'DateTime'
WHEN 'time' THEN 'TimeSpan'
WHEN 'datetime2' THEN 'DateTime'
WHEN 'datetimeoffset' THEN 'DateTimeOffset'
WHEN 'tinyint' THEN 'byte'
WHEN 'smallint' THEN 'short'
WHEN 'int' THEN 'int'
WHEN 'smalldatetime' THEN 'DateTime'
WHEN 'real' THEN 'float'
WHEN 'money' THEN 'decimal'
WHEN 'datetime' THEN 'DateTime'
WHEN 'float' THEN 'double'
WHEN 'ntext' THEN 'string'
WHEN 'bit' THEN 'bool'
WHEN 'decimal' THEN 'decimal'
WHEN 'numeric' THEN 'decimal'
WHEN 'smallmoney' THEN 'decimal'
WHEN 'bigint' THEN 'long'
WHEN 'geometry' THEN 'System.Data.Spatial.DbGeometry'
WHEN 'geography' THEN 'System.Data.Spatial.DbGeography'
WHEN 'varbinary' THEN 'byte[]'
WHEN 'varchar' THEN 'string'
WHEN 'binary' THEN 'byte[]'
WHEN 'char' THEN 'string'
WHEN 'timestamp' THEN 'byte[]'
WHEN 'nvarchar' THEN 'string'
WHEN 'nchar' THEN 'string'
WHEN 'xml' THEN 'string'
ELSE 'string' END)
+ (CASE WHEN 'FALSE'=@IgnoreNull AND [Columns].is_nullable=1
AND ',bigint,bit,date,datetime,datetime2,datetimeoffset,decimal,float,int,money,numeric,real,smalldatetime,smallint,smallmoney,time,tinyint,uniqueidentifier,' LIKE '%,'+[Types].name+',%'
THEN '?' ELSE '' END)
+ ' ' + [Columns].name + ' { get; set; }'
FROM sys.columns AS [Columns]
INNER JOIN sys.types AS [Types] ON [Columns].user_type_id = [Types].user_type_id
WHERE [Columns].object_id = @ObjectId AND [Columns].column_id = @Index IF @Index < @MaxColumnIndex INSERT INTO #Model(Field) VALUES('') SET @Index = @Index + 1
END SELECT * FROM #Model
DROP TABLE #Model
SQL Server中数据类型对应C#中数据类型的更多相关文章
- SQL Server、Oracle和MySQL中查出值为NULL的替换
参考文献: http://database.51cto.com/art/200803/67397.htm 正文 在SQL Server Oracle MySQL当数据库中查出某值为NULL怎么办? 1 ...
- ArcSDE for SQL Server安装及在ArcMap中创建ArcSDE连接
原文:ArcSDE for SQL Server安装及在ArcMap中创建ArcSDE连接 安装ArcSDE for SQL Server,最后一步成功后的界面如下: 在ArcMap中创建ArcSDE ...
- SQL Server 2008 R2 清空数据库中ldf日志文件
/************************************************************ * Sql Server 2008 R2 清空数据库中ldf日志文件 * 将 ...
- SQL Server的非聚集索引中会存储NULL吗?
原文:SQL Server的非聚集索引中会存储NULL吗? SQL Server的非聚集索引中会存储NULL吗? 这是个很有意思的问题,下面通过如下的代码,来说明,到底会不会存储NULL. --1.建 ...
- SQL Server 深入解析索引存储(中)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/堆 概述 本篇文章是关于堆的存储结构.堆是不含聚集索引的表(所以只有非聚集索引的表也是堆).堆的 sys.parti ...
- 如何将SQL Server 2008库导入2000中
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- 《Pro SQL Server Internals, 2nd edition》中CHAPTER 7 Designing and Tuning the Indexes中的Clustered Index Design Considerations一节(译)
<Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress出版年: 2016-12-29页数: 804定价: USD 59 ...
- SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二: ...
- 在 SQL Server 数据库的 WHERE 语句中使用子查询
这是关于子查询语句的一系列文章中的第三篇.在这篇文章中我们将讨论WHERE语句中的子查询语句.其他的文章讨论了其他语句中的子查询语句. 本次课程中的所有例子都是基于Microsoft SQL Serv ...
- SQL Server 重新初始化系统数据库中的单引号问题
在最近的数据库跨机房迁移中,由于硬件的限制,需要滚动式地将数据库一台台迁移到新机房,先在新机房搭建一个新环境,将数据迁移过去,再将旧机房的机器下架搬到新机房,重新配置后用于下一轮的升级,重新配置过程中 ...
随机推荐
- Java创建和解析Json数据方法(二)——org.json包的使用
(二)org.json包的使用 1.简介 工具包org.json.jar,是一个轻量级的,JAVA下的json构造和解析工具包,它还包含JSON与XML, HTTP headers, Cookie ...
- 分享tiny4412,emmc烧录u-boot, 支持fastboot模式烧写emmc
转载 : http://www.arm9home.net/read.php?tid-83474.html 本人是第一次在此发帖,希望大家多多支持,发帖目的是为了分享,分享的目的是传递开源的精神.Tin ...
- svn hooks 实现自动更新
搞来搞去,原来是hooks 下面的脚本名称必须是post-commit才可以, 写成fly-commit一直不行.晕死~~~ https://serverfault.com/questions/144 ...
- [c++菜鸟]《Accelerate C++》习题解答
第0章 0-0 编译并运行Hello, world! 程序. #include <iostream> using namespace std; int main() { cout < ...
- 并行程序设计---cuda memory
CUDA存储器模型: GPU片内:register,shared memory: host 内存: host memory, pinned memory. 板载显存:local memory,cons ...
- C++ Primer 学习笔记_6_标准库类型 -- 命名空间using与string类型
标准库类型(一) --命名空间using与string类型 引: 标准库类型是语言组成部分中更主要的哪些数据类型(如:数组.指针)的抽象! C++标准库定义的是高级的抽象数据类型: 1.高级:由 ...
- 兔子--html,js,php,ASP,ASP.NET,JSP的关系
html是超文本链接语言.是静态的.显示在client.仅仅用HTML做出来的网页是静态网页.没不论什么交互功能. JS是一种基于对象和事件驱动的脚本语言,执行在client.是一种比較简单的编程语言 ...
- python(8)- python基础数据类型
数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视 频.网页等各种各样的数据,不同的数据,需要 ...
- 关于提高沟通能力的书单zz
上周推荐了一份关于提高写作能力的书单,这周,我们来聊聊沟通能力. 在现代社会,沟通能力变得越来越重要.人与人之间的社交渠道越来越丰富,工作中的协同合作也越来越普遍.我们要沟通的人越来越多,节奏越来越快 ...
- [单元測试]_[VC2010使用gtest单元測试入门]
场景: 1. gtest作为C++的单元測试工具非常优秀了,它集成了非常多标准assert所没有的功能,比方让流程继续运行的EXPECT,仅仅測试特定測试用例的--gtest_filter, 输出xm ...