1. 程式人生 > >SQL SERVER與C#的資料型別對應表

SQL SERVER與C#的資料型別對應表

序號 類別 SQLServer C Sharp 備註
1 整數 bit Boolean True轉換為1False轉換為0
2 tinyint Byte C Sharp 資料型別都位於System名稱空間
3 smallint Int16
4 int Int32
5 bigint Int64
6 smallmoney Decimal
7 money Decimal
8 numeric Decimal
9 decimal Decimal
10 浮點數 float Double
11 real Single
12 日期和時間 smalldatetime DateTime
13 datetime DateTime
14 timestamp DateTime
15 字串 char String
16 text String
17 varchar String
18 nchar String
19 ntext String
20 nvarchar String
21 二進位制資料     binary Byte[]
22 varbinary Byte[]
23 image Byte[]
24 其他 uniqueidentifier Guid
25 Variant Object
     
/// <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;
  }