SqlServer数据类型、C#SqlDbType对应关系及转换

SqlServer数据类型、C#SqlDbType对应关系及转换,第1张

概述// SqlDbType转换为C#数据类型public static Type SqlType2CsharpType(SqlDbType sqlType){switch (sqlType){ case SqlDbType.BigInt: return typeof(Int64); case SqlDbType.Binary:
// 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数据类型(如:varchar)// 转换为sqlDbType类型public static sqlDbType sqlTypestring2sqlType(string sqlTypestring){sqlDbType dbType = sqlDbType.Variant;//默认为Objectswitch (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;}

  

// 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;}

  

 

有如下类型的映射对照:System.Data.sqlClIEnt.sqlDbTypeSystem.Data.oleDb.oleDbTypeSystem.Data.Odbc.OdbcTypeSystem.Data.OracleClIEnt.OracleTypeOracle.ManagedDataAccess.ClIEntOracleDbType格式:DbType.枚举名(枚举值) = DbType.枚举名(枚举值)回到顶部1、与 System.Data.sqlClIEnt.sqlDbType转换System.Data.sqlClIEnt.sqlDbType.BigInt(0)   =  System.Data.DbType.Int64(12)System.Data.sqlClIEnt.sqlDbType.Binary(1)   =  System.Data.DbType.Binary(1)System.Data.sqlClIEnt.sqlDbType.Bit(2)   =  System.Data.DbType.Boolean(3)System.Data.sqlClIEnt.sqlDbType.Char(3)   =  System.Data.DbType.AnsiStringFixedLength(22)System.Data.sqlClIEnt.sqlDbType.DateTime(4)   =  System.Data.DbType.DateTime(6)System.Data.sqlClIEnt.sqlDbType.Decimal(5)   =  System.Data.DbType.Decimal(7)System.Data.sqlClIEnt.sqlDbType.float(6)   =  System.Data.DbType.Double(8)System.Data.sqlClIEnt.sqlDbType.Image(7)   =  System.Data.DbType.Binary(1)System.Data.sqlClIEnt.sqlDbType.Int(8)   =  System.Data.DbType.Int32(11)System.Data.sqlClIEnt.sqlDbType.Money(9)   =  System.Data.DbType.Currency(4)System.Data.sqlClIEnt.sqlDbType.NChar(10)   =  System.Data.DbType.StringFixedLength(23)System.Data.sqlClIEnt.sqlDbType.NText(11)   =  System.Data.DbType.String(16)System.Data.sqlClIEnt.sqlDbType.NVarChar(12)   =  System.Data.DbType.String(16)System.Data.sqlClIEnt.sqlDbType.Real(13)   =  System.Data.DbType.Single(15)System.Data.sqlClIEnt.sqlDbType.UniqueIDentifIEr(14)   =  System.Data.DbType.GuID(9)System.Data.sqlClIEnt.sqlDbType.SmallDateTime(15)   =  System.Data.DbType.DateTime(6)System.Data.sqlClIEnt.sqlDbType.Smallint(16)   =  System.Data.DbType.Int16(10)System.Data.sqlClIEnt.sqlDbType.SmallMoney(17)   =  System.Data.DbType.Currency(4)System.Data.sqlClIEnt.sqlDbType.Text(18)   =  System.Data.DbType.AnsiString(0)System.Data.sqlClIEnt.sqlDbType.Timestamp(19)   =  System.Data.DbType.Binary(1)System.Data.sqlClIEnt.sqlDbType.tinyint(20)   =  System.Data.DbType.Byte(2)System.Data.sqlClIEnt.sqlDbType.VarBinary(21)   =  System.Data.DbType.Binary(1)System.Data.sqlClIEnt.sqlDbType.VarChar(22)   =  System.Data.DbType.AnsiString(0)System.Data.sqlClIEnt.sqlDbType.Variant(23)   =  System.Data.DbType.Object(13)System.Data.sqlClIEnt.sqlDbType.Xml(25)   =  System.Data.DbType.Xml(25)System.Data.sqlClIEnt.sqlDbType.Udt(29)   =  System.Data.DbType.Object(13)System.Data.sqlClIEnt.sqlDbType.Structured(30)   =  System.Data.DbType.Object(13)System.Data.sqlClIEnt.sqlDbType.Date(31)   =  System.Data.DbType.Date(5)System.Data.sqlClIEnt.sqlDbType.Time(32)   =  System.Data.DbType.Time(17)System.Data.sqlClIEnt.sqlDbType.DateTime2(33)   =  System.Data.DbType.DateTime2(26)System.Data.sqlClIEnt.sqlDbType.DateTimeOffset(34)   =  System.Data.DbType.DateTimeOffset(27)回到顶部2、与 System.Data.oleDb.oleDbType转换System.Data.oleDb.oleDbType.Empty(0)   =  System.Data.DbType.Object(13)System.Data.oleDb.oleDbType.Smallint(2)   =  System.Data.DbType.Int16(10)System.Data.oleDb.oleDbType.Integer(3)   =  System.Data.DbType.Int32(11)System.Data.oleDb.oleDbType.Single(4)   =  System.Data.DbType.Single(15)System.Data.oleDb.oleDbType.Double(5)   =  System.Data.DbType.Double(8)System.Data.oleDb.oleDbType.Currency(6)   =  System.Data.DbType.Currency(4)System.Data.oleDb.oleDbType.Date(7)   =  System.Data.DbType.DateTime(6)System.Data.oleDb.oleDbType.BSTR(8)   =  System.Data.DbType.String(16)System.Data.oleDb.oleDbType.Idispatch(9)   =  System.Data.DbType.Object(13)System.Data.oleDb.oleDbType.Error(10)   =  System.Data.DbType.Int32(11)System.Data.oleDb.oleDbType.Boolean(11)   =  System.Data.DbType.Boolean(3)System.Data.oleDb.oleDbType.Variant(12)   =  System.Data.DbType.Object(13)System.Data.oleDb.oleDbType.IUnkNown(13)   =  System.Data.DbType.Object(13)System.Data.oleDb.oleDbType.Decimal(14)   =  System.Data.DbType.Decimal(7)System.Data.oleDb.oleDbType.tinyint(16)   =  System.Data.DbType.SByte(14)System.Data.oleDb.oleDbType.Unsignedtinyint(17)   =  System.Data.DbType.Byte(2)System.Data.oleDb.oleDbType.UnsignedSmallint(18)   =  System.Data.DbType.UInt16(18)System.Data.oleDb.oleDbType.UnsignedInt(19)   =  System.Data.DbType.UInt32(19)System.Data.oleDb.oleDbType.BigInt(20)   =  System.Data.DbType.Int64(12)System.Data.oleDb.oleDbType.UnsignedBigInt(21)   =  System.Data.DbType.UInt64(20)System.Data.oleDb.oleDbType.filetime(64)   =  System.Data.DbType.DateTime(6)System.Data.oleDb.oleDbType.GuID(72)   =  System.Data.DbType.GuID(9)System.Data.oleDb.oleDbType.Binary(128)   =  System.Data.DbType.Binary(1)System.Data.oleDb.oleDbType.Char(129)   =  System.Data.DbType.AnsiStringFixedLength(22)System.Data.oleDb.oleDbType.WChar(130)   =  System.Data.DbType.StringFixedLength(23)System.Data.oleDb.oleDbType.Numeric(131)   =  System.Data.DbType.Decimal(7)System.Data.oleDb.oleDbType.DBDate(133)   =  System.Data.DbType.Date(5)System.Data.oleDb.oleDbType.DBTime(134)   =  System.Data.DbType.Time(17)System.Data.oleDb.oleDbType.DBTimeStamp(135)   =  System.Data.DbType.DateTime(6)System.Data.oleDb.oleDbType.PropVariant(138)   =  System.Data.DbType.Object(13)System.Data.oleDb.oleDbType.VarNumeric(139)   =  System.Data.DbType.VarNumeric(21)System.Data.oleDb.oleDbType.VarChar(200)   =  System.Data.DbType.AnsiString(0)System.Data.oleDb.oleDbType.LongVarChar(201)   =  System.Data.DbType.AnsiString(0)System.Data.oleDb.oleDbType.VarWChar(202)   =  System.Data.DbType.String(16)System.Data.oleDb.oleDbType.LongVarWChar(203)   =  System.Data.DbType.String(16)System.Data.oleDb.oleDbType.VarBinary(204)   =  System.Data.DbType.Binary(1)System.Data.oleDb.oleDbType.LongVarBinary(205)   =  System.Data.DbType.Binary(1)回到顶部3、与System.Data.Odbc.OdbcType转换System.Data.Odbc.OdbcType.BigInt(1)   =  System.Data.DbType.Int64(12)System.Data.Odbc.OdbcType.Binary(2)   =  System.Data.DbType.Binary(1)System.Data.Odbc.OdbcType.Bit(3)   =  System.Data.DbType.Boolean(3)System.Data.Odbc.OdbcType.Char(4)   =  System.Data.DbType.AnsiStringFixedLength(22)System.Data.Odbc.OdbcType.DateTime(5)   =  System.Data.DbType.DateTime(6)System.Data.Odbc.OdbcType.Decimal(6)   =  System.Data.DbType.Decimal(7)System.Data.Odbc.OdbcType.Numeric(7)   =  System.Data.DbType.Decimal(7)System.Data.Odbc.OdbcType.Double(8)   =  System.Data.DbType.Double(8)System.Data.Odbc.OdbcType.Image(9)   =  System.Data.DbType.Binary(1)System.Data.Odbc.OdbcType.Int(10)   =  System.Data.DbType.Int32(11)System.Data.Odbc.OdbcType.NChar(11)   =  System.Data.DbType.StringFixedLength(23)System.Data.Odbc.OdbcType.NText(12)   =  System.Data.DbType.String(16)System.Data.Odbc.OdbcType.NVarChar(13)   =  System.Data.DbType.String(16)System.Data.Odbc.OdbcType.Real(14)   =  System.Data.DbType.Single(15)System.Data.Odbc.OdbcType.UniqueIDentifIEr(15)   =  System.Data.DbType.GuID(9)System.Data.Odbc.OdbcType.SmallDateTime(16)   =  System.Data.DbType.DateTime(6)System.Data.Odbc.OdbcType.Smallint(17)   =  System.Data.DbType.Int16(10)System.Data.Odbc.OdbcType.Text(18)   =  System.Data.DbType.AnsiString(0)System.Data.Odbc.OdbcType.Timestamp(19)   =  System.Data.DbType.Binary(1)System.Data.Odbc.OdbcType.tinyint(20)   =  System.Data.DbType.Byte(2)System.Data.Odbc.OdbcType.VarBinary(21)   =  System.Data.DbType.Binary(1)System.Data.Odbc.OdbcType.VarChar(22)   =  System.Data.DbType.AnsiString(0)System.Data.Odbc.OdbcType.Date(23)   =  System.Data.DbType.Date(5)System.Data.Odbc.OdbcType.Time(24)   =  System.Data.DbType.Time(17)回到顶部4、与System.Data.OracleClIEnt.OracleType转换System.Data.OracleClIEnt.OracleType.Bfile(1)   =  System.Data.DbType.Binary(1)System.Data.OracleClIEnt.OracleType.Blob(2)   =  System.Data.DbType.Binary(1)System.Data.OracleClIEnt.OracleType.Char(3)   =  System.Data.DbType.AnsiStringFixedLength(22)System.Data.OracleClIEnt.OracleType.Clob(4)   =  System.Data.DbType.AnsiString(0)System.Data.OracleClIEnt.OracleType.Cursor(5)   =  System.Data.DbType.Object(13)System.Data.OracleClIEnt.OracleType.DateTime(6)   =  System.Data.DbType.DateTime(6)System.Data.OracleClIEnt.OracleType.IntervalDayToSecond(7)   =  System.Data.DbType.Object(13)System.Data.OracleClIEnt.OracleType.IntervalYearToMonth(8)   =  System.Data.DbType.Int32(11)System.Data.OracleClIEnt.OracleType.LongRaw(9)   =  System.Data.DbType.Binary(1)System.Data.OracleClIEnt.OracleType.LongVarChar(10)   =  System.Data.DbType.AnsiString(0)System.Data.OracleClIEnt.OracleType.NChar(11)   =  System.Data.DbType.StringFixedLength(23)System.Data.OracleClIEnt.OracleType.NClob(12)   =  System.Data.DbType.String(16)System.Data.OracleClIEnt.OracleType.Number(13)   =  System.Data.DbType.VarNumeric(21)System.Data.OracleClIEnt.OracleType.NVarChar(14)   =  System.Data.DbType.String(16)System.Data.OracleClIEnt.OracleType.Raw(15)   =  System.Data.DbType.Binary(1)System.Data.OracleClIEnt.OracleType.RowID(16)   =  System.Data.DbType.AnsiString(0)System.Data.OracleClIEnt.OracleType.Timestamp(18)   =  System.Data.DbType.DateTime(6)System.Data.OracleClIEnt.OracleType.TimestampLocal(19)   =  System.Data.DbType.DateTime(6)System.Data.OracleClIEnt.OracleType.TimestampWithTZ(20)   =  System.Data.DbType.DateTime(6)System.Data.OracleClIEnt.OracleType.VarChar(22)   =  System.Data.DbType.AnsiString(0)System.Data.OracleClIEnt.OracleType.Byte(23)   =  System.Data.DbType.Byte(2)System.Data.OracleClIEnt.OracleType.UInt16(24)   =  System.Data.DbType.UInt16(18)System.Data.OracleClIEnt.OracleType.UInt32(25)   =  System.Data.DbType.UInt32(19)System.Data.OracleClIEnt.OracleType.SByte(26)   =  System.Data.DbType.SByte(14)System.Data.OracleClIEnt.OracleType.Int16(27)   =  System.Data.DbType.Int16(10)System.Data.OracleClIEnt.OracleType.Int32(28)   =  System.Data.DbType.Int32(11)System.Data.OracleClIEnt.OracleType.float(29)   =  System.Data.DbType.Single(15)System.Data.OracleClIEnt.OracleType.Double(30)   =  System.Data.DbType.Double(8)回到顶部5、与Oracle.ManagedDataAccess.ClIEntOracleDbType转换Oracle.ManagedDataAccess.ClIEntOracleDbType.Bfile =  System.Data.DbType.Binary(1)Oracle.ManagedDataAccess.ClIEntOracleDbType.Blob = 102,Oracle.ManagedDataAccess.ClIEntOracleDbType.Byte = 103,Oracle.ManagedDataAccess.ClIEntOracleDbType.Char = 104,Oracle.ManagedDataAccess.ClIEntOracleDbType.Clob = 105,Oracle.ManagedDataAccess.ClIEntOracleDbType.Date = 106,Oracle.ManagedDataAccess.ClIEntOracleDbType.Decimal = 107,Oracle.ManagedDataAccess.ClIEntOracleDbType.Double = 108,Oracle.ManagedDataAccess.ClIEntOracleDbType.Long = 109,Oracle.ManagedDataAccess.ClIEntOracleDbType.LongRaw = 110,Oracle.ManagedDataAccess.ClIEntOracleDbType.Int16 = 111,Oracle.ManagedDataAccess.ClIEntOracleDbType.Int32 = 112,Oracle.ManagedDataAccess.ClIEntOracleDbType.Int64 = 113,Oracle.ManagedDataAccess.ClIEntOracleDbType.IntervalDS = 114,Oracle.ManagedDataAccess.ClIEntOracleDbType.IntervalYM = 115,Oracle.ManagedDataAccess.ClIEntOracleDbType.NClob = 116,Oracle.ManagedDataAccess.ClIEntOracleDbType.NChar = 117,Oracle.ManagedDataAccess.ClIEntOracleDbType.NVarchar2 = 119,Oracle.ManagedDataAccess.ClIEntOracleDbType.Raw = 120,Oracle.ManagedDataAccess.ClIEntOracleDbType.RefCursor = 121,Oracle.ManagedDataAccess.ClIEntOracleDbType.Single = 122,Oracle.ManagedDataAccess.ClIEntOracleDbType.TimeStamp = 123,Oracle.ManagedDataAccess.ClIEntOracleDbType.TimeStampltZ = 124,Oracle.ManagedDataAccess.ClIEntOracleDbType.TimeStampTZ = 125,Oracle.ManagedDataAccess.ClIEntOracleDbType.Varchar2 = 126,Oracle.ManagedDataAccess.ClIEntOracleDbType.XmlType = 127,Oracle.ManagedDataAccess.ClIEntOracleDbType.BinaryDouble = 132,Oracle.ManagedDataAccess.ClIEntOracleDbType.Binaryfloat = 133,Oracle.ManagedDataAccess.ClIEntOracleDbType.Boolean = 134,
总结

以上是内存溢出为你收集整理的SqlServer数据类型、C#SqlDbType对应关系及转换全部内容,希望文章能够帮你解决SqlServer数据类型、C#SqlDbType对应关系及转换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1176425.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存