帮我书写C#与sql server的类型映射枚举类
时间: 2023-09-30 20:03:28 浏览: 194
C# 枚举类型的实例说明
好的,以下是C#与SQL Server的类型映射枚举类的代码:
```
public enum SqlType
{
BigInt,
Binary,
Bit,
Char,
Date,
DateTime,
DateTime2,
DateTimeOffset,
Decimal,
Float,
Geography,
Geometry,
HierarchyId,
Image,
Int,
Money,
NChar,
NText,
Numeric,
NVarChar,
Real,
SmallDateTime,
SmallInt,
SmallMoney,
SqlVariant,
SysName,
Text,
Time,
Timestamp,
TinyInt,
UniqueIdentifier,
VarBinary,
VarChar,
Xml
}
public static class SqlTypeMapper
{
public static string Map(SqlType type)
{
switch (type)
{
case SqlType.BigInt:
return "bigint";
case SqlType.Binary:
return "binary";
case SqlType.Bit:
return "bit";
case SqlType.Char:
return "char";
case SqlType.Date:
return "date";
case SqlType.DateTime:
return "datetime";
case SqlType.DateTime2:
return "datetime2";
case SqlType.DateTimeOffset:
return "datetimeoffset";
case SqlType.Decimal:
return "decimal";
case SqlType.Float:
return "float";
case SqlType.Geography:
return "geography";
case SqlType.Geometry:
return "geometry";
case SqlType.HierarchyId:
return "hierarchyid";
case SqlType.Image:
return "image";
case SqlType.Int:
return "int";
case SqlType.Money:
return "money";
case SqlType.NChar:
return "nchar";
case SqlType.NText:
return "ntext";
case SqlType.Numeric:
return "numeric";
case SqlType.NVarChar:
return "nvarchar";
case SqlType.Real:
return "real";
case SqlType.SmallDateTime:
return "smalldatetime";
case SqlType.SmallInt:
return "smallint";
case SqlType.SmallMoney:
return "smallmoney";
case SqlType.SqlVariant:
return "sql_variant";
case SqlType.SysName:
return "sysname";
case SqlType.Text:
return "text";
case SqlType.Time:
return "time";
case SqlType.Timestamp:
return "timestamp";
case SqlType.TinyInt:
return "tinyint";
case SqlType.UniqueIdentifier:
return "uniqueidentifier";
case SqlType.VarBinary:
return "varbinary";
case SqlType.VarChar:
return "varchar";
case SqlType.Xml:
return "xml";
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
}
```
使用时,可以通过 `SqlTypeMapper.Map(SqlType)` 方法来获取对应的SQL Server数据类型。例如:
```
var sqlType = SqlTypeMapper.Map(SqlType.VarChar);
```
这样就可以得到 `varchar` 这个数据类型了。
阅读全文