c#和mysql之间的类型_【SqlServer数据类型、C#数据类型、SqlDbType】对应关系及转换...

// sql server数据类型(如:varchar) // 转换为SqlDbType类型 public static SqlDbType SqlTypeString2SqlType(string sqlTypeString) { SqlDbType dbType = SqlDbType.Variant;//默认为Object switch (sqlTypeString) { case "int": dbType = SqlDbType.I

// sql server数据类型(如:varchar)

// 转换为SqlDbType类型

public static SqlDbType SqlTypeString2SqlType(string sqlTypeString)

{

SqlDbType dbType = SqlDbType.Variant;//默认为Object

switch (sqlTypeString)

{

case "int":

dbType = SqlDbType.Int;

break;

case "varchar":

dbType = SqlDbType.VarChar;

break;

case "bit":

dbType = SqlDbType.Bit;

break;

case "datetime":

dbType = SqlDbType.DateTime;

break;

case "decimal":

dbType = SqlDbType.Decimal;

break;

case "float":

dbType = SqlDbType.Float;

break;

case "image":

dbType = SqlDbType.Image;

break;

case "money":

dbType = SqlDbType.Money;

break;

case "ntext":

dbType = SqlDbType.NText;

break;

case "nvarchar":

dbType = SqlDbType.NVarChar;

break;

case "smalldatetime":

dbType = SqlDbType.SmallDateTime;

break;

case "smallint":

dbType = SqlDbType.SmallInt;

break;

case "text":

dbType = SqlDbType.Text;

break;

case "bigint":

dbType = SqlDbType.BigInt;

break;

case "binary":

dbType = SqlDbType.Binary;

break;

case "char":

dbType = SqlDbType.Char;

break;

case "nchar":

dbType = SqlDbType.NChar;

break;

case "numeric":

dbType = SqlDbType.Decimal;

break;

case "real":

dbType = SqlDbType.Real;

break;

case "smallmoney":

dbType = SqlDbType.SmallMoney;

break;

case "sql_variant":

dbType = SqlDbType.Variant;

break;

case "timestamp":

dbType = SqlDbType.Timestamp;

break;

case "tinyint":

dbType = SqlDbType.TinyInt;

break;

case "uniqueidentifier":

dbType = SqlDbType.UniqueIdentifier;

break;

case "varbinary":

dbType = SqlDbType.VarBinary;

break;

case "xml":

dbType = SqlDbType.Xml;

break;

}

return dbType;

}

// SqlDbType转换为C#数据类型

public static Type SqlType2CsharpType(SqlDbType sqlType)

{

switch (sqlType)

{

case SqlDbType.BigInt:

return typeof(Int64);

case SqlDbType.Binary:

return typeof(Object);

case SqlDbType.Bit:

return typeof(Boolean);

case SqlDbType.Char:

return typeof(String);

case SqlDbType.DateTime:

return typeof(DateTime);

case SqlDbType.Decimal:

return typeof(Decimal);

case SqlDbType.Float:

return typeof(Double);

case SqlDbType.Image:

return typeof(Object);

case SqlDbType.Int:

return typeof(Int32);

case SqlDbType.Money:

return typeof(Decimal);

case SqlDbType.NChar:

return typeof(String);

case SqlDbType.NText:

return typeof(String);

case SqlDbType.NVarChar:

return typeof(String);

case SqlDbType.Real:

return typeof(Single);

case SqlDbType.SmallDateTime:

return typeof(DateTime);

case SqlDbType.SmallInt:

return typeof(Int16);

case SqlDbType.SmallMoney:

return typeof(Decimal);

case SqlDbType.Text:

return typeof(String);

case SqlDbType.Timestamp:

return typeof(Object);

case SqlDbType.TinyInt:

return typeof(Byte);

case SqlDbType.Udt://自定义的数据类型

return typeof(Object);

case SqlDbType.UniqueIdentifier:

return typeof(Object);

case SqlDbType.VarBinary:

return typeof(Object);

case SqlDbType.VarChar:

return typeof(String);

case SqlDbType.Variant:

return typeof(Object);

case SqlDbType.Xml:

return typeof(Object);

default:

return null;

}

}

代码:

// sql server中的数据类型,转换为C#中的类型类型

public static Type SqlTypeString2CsharpType(string sqlTypeString)

{

SqlDbType dbTpe = SqlTypeString2SqlType(sqlTypeString);

return SqlType2CsharpType(dbTpe);

}

// 将sql server中的数据类型,转化为C#中的类型的字符串

public static string SqlTypeString2CsharpTypeString(string sqlTypeString)

{

Type type = SqlTypeString2CsharpType(sqlTypeString);

return type.Name;

}

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值