0


【数据类型】C#和Sql Server、Mysql、Oracle等常见数据库的数据类型对应关系

🏆🏆这是小5写的第二篇城市领跑者文章,一起为所在城市领跑助力吧!
🏆🏆在实际项目中,不管是用C#后端编程语言也好,还是Java后端编程语言,都可能会用到不同端的数据类型转换和对应关系

目录

1、C#与Sql Server

1.1、对应关系

在 C# 和 SQL Server 之间,以下是一些最常用的数据类型对应关系:
编号C#数据类型Mssql数据类型1bigintlong 或 Int642intint 或 Int323smallintshort 或 Int164tinyintbyte 或者 SByte5decimal或 numericdecimal 或 Decimal6money 或 smallmoneydecimal 或 Decimal7floatdouble 或 Double8realfloat 或 Single9dateDateTime 或 DateTime210datetimeDateTime 或 DateTime211datetimeDateTime 或 DateTime212datetime2DateTime 或 DateTime213datetimeoffsetDateTimeOffset 或 DateTimeOffset14timeTimeSpan 或 TimeSpan15char 或 ncharstring 或 String16varchar 或 nvarcharstring 或 String17text 或 ntextstring 或 String18binarybyte[] 或 Byte[]19varbinarybyte[] 或 Byte[]20imagebyte[] 或 Byte[]

需要注意的是,这些数据类型对应关系仅适用于大多数情况,具体的实现可能会因需要和其他因素而有所不同。此外,SqlDbType 枚举也提供了一些有用的成员,可用于根据数据库中使用的数据类型限制转换。因此在编写应用程序时请根据实际情况考虑使用哪种数据类型对应关系。

1.2、关系代码

根据上面对应的关系,可以编写如下代码,并非是全部类型转换代码,小伙伴们可以根据自己业务情况补齐

  1. private static string DataTypeMssql(string dType)
  2. {
  3. string dataType = string.Empty;
  4. if (dType.ToLower() == "int".ToLower())
  5. {
  6. dataType = "int";
  7. }
  8. else if (dType.ToLower() == "varchar".ToLower() || dType.ToLower() == "nvarchar".ToLower()
  9. || dType.ToLower() == "char".ToLower() || dType.ToLower() == "nchar".ToLower()
  10. || dType.ToLower() == "text".ToLower() || dType.ToLower() == "ntext".ToLower())
  11. {
  12. dataType = "string";
  13. }
  14. else if (dType.ToLower() == "bigint".ToLower())
  15. {
  16. dataType = "long";
  17. }
  18. else if (dType.ToLower() == "smallint".ToLower())
  19. {
  20. dataType = "short";
  21. }
  22. else if (dType.ToLower() == "tinyint".ToLower())
  23. {
  24. dataType = "byte";
  25. }
  26. else if (dType.ToLower() == "decimal".ToLower()|| dType.ToLower() == "numeric".ToLower())
  27. {
  28. dataType = "decimal";
  29. }
  30. else if (dType.ToLower() == "money".ToLower() || dType.ToLower() == "smallmoney".ToLower())
  31. {
  32. dataType = "decimal";
  33. }
  34. else if (dType.ToLower() == "float".ToLower())
  35. {
  36. dataType = "double";
  37. }
  38. else if (dType.ToLower() == "real".ToLower())
  39. {
  40. dataType = "float";
  41. }
  42. else if (dType.ToLower() == "date".ToLower() || dType.ToLower() == "datetime".ToLower() || dType.ToLower() == "datetime2".ToLower())
  43. {
  44. dataType = "DateTime";
  45. }
  46. else if (dType.ToLower() == "datetimeoffset".ToLower())
  47. {
  48. dataType = "DateTimeOffset";
  49. }
  50. else if (dType.ToLower() == "time".ToLower())
  51. {
  52. dataType = "TimeSpan";
  53. }
  54. else if (dType.ToLower() == "char".ToLower())
  55. {
  56. dataType = "TimeSpan";
  57. }
  58. else if (dType.ToLower() == "binary".ToLower() || dType.ToLower() == "varbinary".ToLower() || dType.ToLower() == "image".ToLower())
  59. {
  60. dataType = "byte[]";
  61. }
  62. return dataType;
  63. }

2、C#与Mysql

2.1、对应关系

在 C# 和 Mysql 之间,以下是一些最常用的数据类型对应关系:
编号C#数据类型Mysql数据类型1bigintlong 或 Int642intint 或 Int323smallintshort 或 Int164tinyintbyte 或者 SByte5decimal或 numericdecimal 或 Decimal6floatdouble 或 Double7doublefloat 或 Single8dateDateTime 或 DateTime29datetimeDateTime 或 DateTime210timeTimeSpan 或 TimeSpan11charstring 或 String12varcharstring 或 String13textstring 或 String14binarybyte[] 或 Byte[]15varbinarybyte[] 或 Byte[]16blobbyte[] 或 Byte[]

需要注意的是,在 C# 中,MySQL 的一些数据类型名称与 SQL Server 或 Oracle 等其他数据库不同,需要使用不同的对应关系来匹配这些名称。此外,这些数据类型对应关系仅适用于大多数情况,具体的实现可能会因需要和其他因素而有所不同。因此,在编写应用程序时请根据实际情况考虑使用哪种数据类型对应关系

2.2、关系代码

根据上面对应的关系,可以编写如下代码,并非是全部类型转换代码,小伙伴们可以根据自己业务情况补齐

  1. private static string DataTypeMysql(string dType)
  2. {
  3. string dataType = string.Empty;
  4. if (dType.ToLower() == "int".ToLower())
  5. {
  6. dataType = "int";
  7. }
  8. else if (dType.ToLower() == "bigint".ToLower())
  9. {
  10. dataType = "long";
  11. }
  12. else if (dType.ToLower() == "smallint".ToLower())
  13. {
  14. dataType = "short";
  15. }
  16. else if (dType.ToLower() == "tinyint".ToLower())
  17. {
  18. dataType = "byte";
  19. }
  20. else if (dType.ToLower() == "decimal".ToLower() || dType.ToLower() == "numeric".ToLower())
  21. {
  22. dataType = "decimal";
  23. }
  24. else if (dType.ToLower() == "float".ToLower())
  25. {
  26. dataType = "double";
  27. }
  28. else if (dType.ToLower() == "double".ToLower())
  29. {
  30. dataType = "float";
  31. }
  32. else if (dType.ToLower() == "date".ToLower() || dType.ToLower() == "datetime".ToLower())
  33. {
  34. dataType = "DateTime";
  35. }
  36. else if (dType.ToLower() == "time".ToLower())
  37. {
  38. dataType = "TimeSpan";
  39. }
  40. else if (dType.ToLower() == "varchar".ToLower() || dType.ToLower() == "char".ToLower() || dType.ToLower() == "text".ToLower())
  41. {
  42. dataType = "string";
  43. }
  44. else if (dType.ToLower() == "binary".ToLower() || dType.ToLower() == "varbinary".ToLower() || dType.ToLower() == "blob".ToLower())
  45. {
  46. dataType = "byte[]";
  47. }
  48. return dataType;
  49. }

3、C#与Oracle

3.1、对应关系

在 C# 和 Oracle 之间,以下是一些最常用的数据类型对应关系:
编号C#数据类型Mysql数据类型1bigintlong 或 Int642interval year to monthint 或 Int323number, int, smallint或 numericdecimal 或 Decimal4binary_doubledouble 或 Double5binary_floatfloat 或 Single6dateDateTime 或 DateTime27interval day to secondTimeSpan 或 TimeSpan8charstring 或 String9clobstring 或 String10blobbyte[] 或 Byte[]

3.2、关系代码

  1. private static string DataTypeOracle(string dType)
  2. {
  3. string dataType = string.Empty;
  4. if (dType.ToLower() == "interval year to month".ToLower())
  5. {
  6. dataType = "int";
  7. }
  8. else if (dType.ToLower() == "bigint".ToLower())
  9. {
  10. dataType = "long";
  11. }
  12. else if (dType.ToLower() == "int".ToLower() || dType.ToLower() == "smallint".ToLower() || dType.ToLower() == "numeric".ToLower())
  13. {
  14. dataType = "decimal";
  15. }
  16. else if (dType.ToLower() == "binary_double".ToLower())
  17. {
  18. dataType = "double";
  19. }
  20. else if (dType.ToLower() == "binary_float".ToLower())
  21. {
  22. dataType = "float";
  23. }
  24. else if (dType.ToLower() == "date".ToLower())
  25. {
  26. dataType = "DateTime";
  27. }
  28. else if (dType.ToLower() == "interval day to second".ToLower())
  29. {
  30. dataType = "TimeSpan";
  31. }
  32. else if (dType.ToLower() == "char".ToLower() || dType.ToLower() == "clob".ToLower() || dType.ToLower() == "blob".ToLower())
  33. {
  34. dataType = "string";
  35. }
  36. return dataType;
  37. }

🏆🏆 原则:Write Less Do More!
🍎🍎简介:一只喜欢全栈方向的程序员,专注基础和实战分享,欢迎咨询,尽绵薄之力答疑解惑!

4、SqlDbType数据类型枚举

在C#开发语言里,也有一个数据类型枚举

4.1、如下图片

在这里插入图片描述

4.2、代码如下

代码里有数据类型的详细说明,包括一些取值范围

  1. #region 程序集 netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
  2. // C:\Users\15633\.nuget\packages\netstandard.library\2.0.3\build\netstandard2.0\ref\netstandard.dll
  3. #endregion
  4. namespace System.Data
  5. {
  6. //
  7. // 摘要:
  8. // Specifies SQL Server-specific data type of a field, property, forusein a System.Data.SqlClient.SqlParameter.
  9. public enum SqlDbType
  10. {
  11. //
  12. // 摘要:
  13. // System.Int64. A 64-bit signed integer.
  14. BigInt =0,
  15. //
  16. // 摘要:
  17. // System.Array of type System.Byte. A fixed-length stream of binary data ranging
  18. // between 1 and 8,000 bytes.
  19. Binary =1,
  20. //
  21. // 摘要:
  22. // System.Boolean. An unsigned numeric value that can be 0, 1, or null.
  23. Bit =2,
  24. //
  25. // 摘要:
  26. // System.String. A fixed-length stream of non-Unicode characters ranging between
  27. // 1 and 8,000 characters.
  28. Char =3,
  29. //
  30. // 摘要:
  31. // System.DateTime. Date and time data ranging in value from January 1, 1753 to
  32. // December 31, 9999 to an accuracy of 3.33 milliseconds.
  33. DateTime =4,
  34. //
  35. // 摘要:
  36. // System.Decimal. A fixed precision and scale numeric value between -10 38 -1 and
  37. // 1038 -1.
  38. Decimal =5,
  39. //
  40. // 摘要:
  41. // System.Double. A floating point number within the range of -1.79E +308 through
  42. // 1.79E +308.
  43. Float =6,
  44. //
  45. // 摘要:
  46. // System.Array of type System.Byte. A variable-length stream of binary data ranging
  47. // from 0 to 231 -1 (or 2,147,483,647) bytes.
  48. Image =7,
  49. //
  50. // 摘要:
  51. // System.Int32. A 32-bit signed integer.
  52. Int =8,
  53. //
  54. // 摘要:
  55. // System.Decimal. A currency value ranging from -2 63(or -9,223,372,036,854,775,808)
  56. // to 263 -1 (or +9,223,372,036,854,775,807) with an accuracy to a ten-thousandth
  57. // of a currency unit.
  58. Money =9,
  59. //
  60. // 摘要:
  61. // System.String. A fixed-length stream of Unicode characters ranging between 1
  62. // and 4,000 characters.
  63. NChar =10,
  64. //
  65. // 摘要:
  66. // System.String. A variable-length stream of Unicode data with a maximum length
  67. // of 230 - 1(or 1,073,741,823) characters.
  68. NText =11,
  69. //
  70. // 摘要:
  71. // System.String. A variable-length stream of Unicode characters ranging between
  72. // 1 and 4,000 characters. Implicit conversion fails if the string is greater than
  73. // 4,000 characters. Explicitly set the object when working with strings longer
  74. // than 4,000 characters. Use System.Data.SqlDbType.NVarChar when the database column
  75. // is nvarchar(max).
  76. NVarChar =12,
  77. //
  78. // 摘要:
  79. // System.Single. A floating point number within the range of -3.40E +38 through
  80. // 3.40E +38.
  81. Real =13,
  82. //
  83. // 摘要:
  84. // System.Guid. A globally unique identifier (or GUID).
  85. UniqueIdentifier =14,
  86. //
  87. // 摘要:
  88. // System.DateTime. Date and time data ranging in value from January 1, 1900 to
  89. // June 6, 2079 to an accuracy of one minute.
  90. SmallDateTime =15,
  91. //
  92. // 摘要:
  93. // System.Int16. A 16-bit signed integer.
  94. SmallInt =16,
  95. //
  96. // 摘要:
  97. // System.Decimal. A currency value ranging from -214,748.3648 to +214,748.3647
  98. // with an accuracy to a ten-thousandth of a currency unit.
  99. SmallMoney =17,
  100. //
  101. // 摘要:
  102. // System.String. A variable-length stream of non-Unicode data with a maximum length
  103. // of 231 -1 (or 2,147,483,647) characters.
  104. Text =18,
  105. //
  106. // 摘要:
  107. // System.Array of type System.Byte. Automatically generated binary numbers, which
  108. // are guaranteed to be unique within a database. timestamp is used typically as
  109. // a mechanism for version-stamping table rows. The storage size is 8 bytes.
  110. Timestamp =19,
  111. //
  112. // 摘要:
  113. // System.Byte. An 8-bit unsigned integer.
  114. TinyInt =20,
  115. //
  116. // 摘要:
  117. // System.Array of type System.Byte. A variable-length stream of binary data ranging
  118. // between 1 and 8,000 bytes. Implicit conversion fails if the byte array is greater
  119. // than 8,000 bytes. Explicitly set the object when working with byte arrays larger
  120. // than 8,000 bytes.
  121. VarBinary =21,
  122. //
  123. // 摘要:
  124. // System.String. A variable-length stream of non-Unicode characters ranging between
  125. // 1 and 8,000 characters. Use System.Data.SqlDbType.VarChar when the database column
  126. // is varchar(max).
  127. VarChar =22,
  128. //
  129. // 摘要:
  130. // System.Object. A special data type that can contain numeric, string, binary,
  131. // or date data as well as the SQL Server values Empty and Null, which is assumed
  132. // if no other type is declared.
  133. Variant =23,
  134. //
  135. // 摘要:
  136. // An XML value. Obtain the XML as a string using the System.Data.SqlClient.SqlDataReader.GetValue(System.Int32)
  137. // method or System.Data.SqlTypes.SqlXml.Value property, or as an System.Xml.XmlReader
  138. // by calling the System.Data.SqlTypes.SqlXml.CreateReader method.
  139. Xml =25,
  140. //
  141. // 摘要:
  142. // A SQL Server user-defined type(UDT).
  143. Udt =29,
  144. //
  145. // 摘要:
  146. // A special data typefor specifying structured data contained in table-valued
  147. // parameters.
  148. Structured =30,
  149. //
  150. // 摘要:
  151. // Date data ranging in value from January 1,1 AD through December 31, 9999 AD.
  152. Date =31,
  153. //
  154. // 摘要:
  155. // Time data based on a 24-hour clock. Time value range is 00:00:00 through 23:59:59.9999999
  156. // with an accuracy of 100 nanoseconds. Corresponds to a SQL Server time value.
  157. Time =32,
  158. //
  159. // 摘要:
  160. // Date and time data. Date value range is from January 1,1 AD through December
  161. // 31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999 with an accuracy
  162. // of 100 nanoseconds.
  163. DateTime2 =33,
  164. //
  165. // 摘要:
  166. // Date and time data with time zone awareness. Date value range is from January
  167. // 1,1 AD through December 31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999
  168. // with an accuracy of 100 nanoseconds. Time zone value range is -14:00 through
  169. // +14:00.
  170. DateTimeOffset =34}}

5、总结

🏆🏆对比不同数据库和C#之间的数据类型,可以发现,大同小异,大部分都是基本一致,有个别类型名称不一样但表示的C#数据类型是一致的。
实际项目中,其实C#与Mssql数据库一般都是两者配合使用,只有在第三方插件或者维护其他项目时才会用到。

标签: 数据库 c# mssql

本文转载自: https://blog.csdn.net/lmy_520/article/details/131271247
版权归原作者 小5聊 所有, 如有侵权,请联系我们删除。

“【数据类型】C#和Sql Server、Mysql、Oracle等常见数据库的数据类型对应关系”的评论:

还没有评论