C#相当于SQL Server数据类型
对于以下SQL Server数据类型,C#中对应的数据类型是什么 精确数字C#相当于SQL Server数据类型,c#,.net,sql-server,C#,.net,Sql Server,对于以下SQL Server数据类型,C#中对应的数据类型是什么 精确数字 bigint numeric bit smallint decimal smallmoney int tinyint money float real char varchar text nchar nvarchar ntext binary varbinary image cursor timestamp hierarchyid uniqueidentifier sql_variant xml table
bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
float
real
char
varchar
text
nchar
nvarchar
ntext
binary
varbinary
image
cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
近似数字
bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
float
real
char
varchar
text
nchar
nvarchar
ntext
binary
varbinary
image
cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
日期和时间
date
datetimeoffset
datetime2
smalldatetime
datetime
time
字符串
bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
float
real
char
varchar
text
nchar
nvarchar
ntext
binary
varbinary
image
cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
Unicode字符串
bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
float
real
char
varchar
text
nchar
nvarchar
ntext
binary
varbinary
image
cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
二进制字符串
bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
float
real
char
varchar
text
nchar
nvarchar
ntext
binary
varbinary
image
cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
其他数据类型
bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
float
real
char
varchar
text
nchar
nvarchar
ntext
binary
varbinary
image
cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
(来源:)这是给你的。和的表有更新版本
SQL Server数据类型及其.NET Framework等效项
下表列出了Microsoft SQL Server数据类型、它们在System.data.SqlTypes命名空间中的SQL Server公共语言运行库(CLR)中的等价物,以及它们在Microsoft.NET Framework中的本机CLR等价物
SQL Server data type CLR data type (SQL Server) CLR data type (.NET Framework)
varbinary SqlBytes, SqlBinary Byte[]
binary SqlBytes, SqlBinary Byte[]
varbinary(1), binary(1) SqlBytes, SqlBinary byte, Byte[]
image None None
varchar None None
char None None
nvarchar(1), nchar(1) SqlChars, SqlString Char, String, Char[]
nvarchar SqlChars, SqlString String, Char[]
nchar SqlChars, SqlString String, Char[]
text None None
ntext None None
uniqueidentifier SqlGuid Guid
rowversion None Byte[]
bit SqlBoolean Boolean
tinyint SqlByte Byte
smallint SqlInt16 Int16
int SqlInt32 Int32
bigint SqlInt64 Int64
smallmoney SqlMoney Decimal
money SqlMoney Decimal
numeric SqlDecimal Decimal
decimal SqlDecimal Decimal
real SqlSingle Single
float SqlDouble Double
smalldatetime SqlDateTime DateTime
datetime SqlDateTime DateTime
sql_variant None Object
User-defined type(UDT) None user-defined type
table None None
cursor None None
timestamp None None
xml SqlXml None
SQL Server和.NET Framework基于不同类型的系统。例如,.NET Framework Decimal结构的最大刻度为28,而SQL Server Decimal和numeric数据类型的最大刻度为38。点击这里的!详情
SQL Server和.Net数据类型映射
如果有人正在寻找从C#和SQL Server格式转换为C#和SQL Server格式的方法,下面是一个简单的实现:
private readonly string[] SqlServerTypes = { "bigint", "binary", "bit", "char", "date", "datetime", "datetime2", "datetimeoffset", "decimal", "filestream", "float", "geography", "geometry", "hierarchyid", "image", "int", "money", "nchar", "ntext", "numeric", "nvarchar", "real", "rowversion", "smalldatetime", "smallint", "smallmoney", "sql_variant", "text", "time", "timestamp", "tinyint", "uniqueidentifier", "varbinary", "varchar", "xml" };
private readonly string[] CSharpTypes = { "long", "byte[]", "bool", "char", "DateTime", "DateTime", "DateTime", "DateTimeOffset", "decimal", "byte[]", "double", "Microsoft.SqlServer.Types.SqlGeography", "Microsoft.SqlServer.Types.SqlGeometry", "Microsoft.SqlServer.Types.SqlHierarchyId", "byte[]", "int", "decimal", "string", "string", "decimal", "string", "Single", "byte[]", "DateTime", "short", "decimal", "object", "string", "TimeSpan", "byte[]", "byte", "Guid", "byte[]", "string", "string" };
public string ConvertSqlServerFormatToCSharp(string typeName)
{
var index = Array.IndexOf(SqlServerTypes, typeName);
return index > -1
? CSharpTypes[index]
: "object";
}
public string ConvertCSharpFormatToSqlServer(string typeName)
{
var index = Array.IndexOf(CSharpTypes, typeName);
return index > -1
? SqlServerTypes[index]
: null;
}
编辑:修正打字错误我想这就是你可能想要的:你能解释一下为什么我的答案得到-1吗?不是我否决了答案,但理想情况下你应该回答这个问题,而不是提供一个链接。在.Net framework中,
short
应该使用哪种CLR数据类型(SQL Server)?@yogeshpatel,short
()等于此列表中的System.Int16。重要提示:SQL数据类型“float”默认为“float(54)”,它转换为C#“double”。但是,SQL数据类型“float(24)”仅转换为C#“float”。因此,如果您不需要很多精度数字,并且希望提高性能/内存,请在SQL中使用float(24),在C#中使用float类型。您的方法ConvertCSharpFormatToSqlServer将始终返回找到的第一个实例,因为CSharp类型不唯一,即“byte[]”将始终返回“binary”即使它被映射到其他5种Sql Server类型。@David虽然您所说的在技术上没有错,但当您使用convertsqlserverformatocharp
方法时,它是有意义的。这只是一个例子,您可以根据自己的需要随时修改它。
public static string FromSqlType(string sqlTypeString)
{
if (! Enum.TryParse(sqlTypeString, out Enums.SQLType typeCode))
{
throw new Exception("sql type not found");
}
switch (typeCode)
{
case Enums.SQLType.varbinary:
case Enums.SQLType.binary:
case Enums.SQLType.filestream:
case Enums.SQLType.image:
case Enums.SQLType.rowversion:
case Enums.SQLType.timestamp://?
return "byte[]";
case Enums.SQLType.tinyint:
return "byte";
case Enums.SQLType.varchar:
case Enums.SQLType.nvarchar:
case Enums.SQLType.nchar:
case Enums.SQLType.text:
case Enums.SQLType.ntext:
case Enums.SQLType.xml:
return "string";
case Enums.SQLType.@char:
return "char";
case Enums.SQLType.bigint:
return "long";
case Enums.SQLType.bit:
return "bool";
case Enums.SQLType.smalldatetime:
case Enums.SQLType.datetime:
case Enums.SQLType.date:
case Enums.SQLType.datetime2:
return "DateTime";
case Enums.SQLType.datetimeoffset:
return "DateTimeOffset";
case Enums.SQLType.@decimal:
case Enums.SQLType.money:
case Enums.SQLType.numeric:
case Enums.SQLType.smallmoney:
return "decimal";
case Enums.SQLType.@float:
return "double";
case Enums.SQLType.@int:
return "int";
case Enums.SQLType.real:
return "Single";
case Enums.SQLType.smallint:
return "short";
case Enums.SQLType.uniqueidentifier:
return "Guid";
case Enums.SQLType.sql_variant:
return "object";
case Enums.SQLType.time:
return "TimeSpan";
default:
throw new Exception("none equal type");
}
}
public enum SQLType
{
varbinary,//(1)
binary,//(1)
image,
varchar,
@char,
nvarchar,//(1)
nchar,//(1)
text,
ntext,
uniqueidentifier,
rowversion,
bit,
tinyint,
smallint,
@int,
bigint,
smallmoney,
money,
numeric,
@decimal,
real,
@float,
smalldatetime,
datetime,
sql_variant,
table,
cursor,
timestamp,
xml,
date,
datetime2,
datetimeoffset,
filestream,
time,
}