SQL SERVER与C#的数据类型对应表
(一)C#与SQL Server 2005(或以下版本):
C# | C#取值 | SQL Server | SQL Server取值 |
System.DateTime |
| samlltime |
|
System.Object |
| variant |
|
(二)C#与SQL Server 2008(或以上版本):
C# | C#取值 | SQL Server | SQL Server取值 |
System.Boolean | true/false | bit | 1/0 |
System.SByte | -128~127 |
|
|
System.Byte | 0~255 | tinyint | 0~255 |
System.Int16 | -32768~32767 | smallint | -32768~32767 |
System.UInt16 | 0~65535 |
|
|
System.Int32 | -2147483648~2147483647 | int | -2147483648~2147483647 |
System.UInt32 | 0~4294967295 |
|
|
System.Int64 | -9223372036854775808~ 9223372036854775807L | bigint | -9223372036854775808~9223372036854775807 |
System.UInt64 | 0~18446744073709551615UL |
|
|
System.Single |
| real |
|
System.Double |
| float |
|
System.Decimal |
| smallmoney | -214748.3648~214748.3647 |
money | -922337203685477.5808~922337203685477.5807 | ||
decimal | -999999999999999999.49~999999999999999999.49 | ||
numeric | 1~38位 | ||
System.Char |
|
|
|
System.String |
| char | 1~8000 |
varchar | 1~8000 | ||
nchar | 1~4000 | ||
nvarchar | 1~4000 | ||
text |
| ||
ntext |
| ||
|
| time | 00:00:00.0000000~23:59:59.9999999 |
System.DateTime |
| date | 0001-01-01~9999-12-31 |
smalldatetime |
| ||
datetime | 1753-01-01 00:00:00:000~9999-12-31 23:59:59.997 | ||
datetime2 |
| ||
datetimeoffset |
| ||
System.Byte[] |
| timestamp |
|
image |
| ||
binary |
| ||
varbinary |
| ||
System.Guid |
| uniqueidentifier |
|
System.Object |
| sql_variant |
|
/// <summary>
/// 数据库中与c#中的数据类型对照
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private string changetocsharptype(string type)
{
string reval=string.empty;
switch(type.tolower())
{
case "int":
reval= "int32";
break;
case "text":
reval= "string";
break;
case "bigint":
reval= "int64";
break;
case "binary":
reval= "system.byte[]";
break;
case "bit":
reval= "boolean";
break;
case "char":
reval= "string";
break;
case "datetime":
reval= "system.datetime";
break;
case "decimal":
reval= "system.decimal";
break;
case "float":
reval= "system.double";
break;
case "image":
reval= "system.byte[]";
break;
case "money":
reval= "system.decimal";
break;
case "nchar":
reval= "string";
break;
case "ntext":
reval= "string";
break;
case "numeric":
reval= "system.decimal";
break;
case "nvarchar":
reval= "string";
break;
case "real":
reval= "system.single";
break;
case "smalldatetime":
reval= "system.datetime";
break;
case "smallint":
reval= "int16";
break;
case "smallmoney":
reval= "system.decimal";
break;
case "timestamp":
reval= "system.datetime";
break;
case "tinyint":
reval= "system.byte";
break;
case "uniqueidentifier":
reval= "system.guid";
break;
case "varbinary":
reval= "system.byte[]";
break;
case "varchar":
reval= "string";
break;
case "variant":
reval="object";
break;
default:
reval= "string";
break;
}
return reval;
}