这是小5写的第二篇城市领跑者文章,一起为所在城市领跑助力吧!
在实际项目中,不管是用C#后端编程语言也好,还是Java后端编程语言,都可能会用到不同端的数据类型转换和对应关系
目录
- 1、C#与Sql Server
- 1.1、对应关系
- 1.2、关系代码
- 2、C#与Mysql
- 2.1、对应关系
- 2.2、关系代码
- 3、C#与Oracle
- 3.1、对应关系
- 3.2、关系代码
- 4、SqlDbType数据类型枚举
- 4.1、如下图片
- 4.2、代码如下
- 5、总结
1、C#与Sql Server
1.1、对应关系
在 C# 和 SQL Server 之间,以下是一些最常用的数据类型对应关系:
编号 | C#数据类型 | Mssql数据类型 |
---|---|---|
1 | bigint | long 或 Int64 |
2 | int | int 或 Int32 |
3 | smallint | short 或 Int16 |
4 | tinyint | byte 或者 SByte |
5 | decimal或 numeric | decimal 或 Decimal |
6 | money 或 smallmoney | decimal 或 Decimal |
7 | float | double 或 Double |
8 | real | float 或 Single |
9 | date | DateTime 或 DateTime2 |
10 | datetime | DateTime 或 DateTime2 |
11 | datetime | DateTime 或 DateTime2 |
12 | datetime2 | DateTime 或 DateTime2 |
13 | datetimeoffset | DateTimeOffset 或 DateTimeOffset |
14 | time | TimeSpan 或 TimeSpan |
15 | char 或 nchar | string 或 String |
16 | varchar 或 nvarchar | string 或 String |
17 | text 或 ntext | string 或 String |
18 | binary | byte[] 或 Byte[] |
19 | varbinary | byte[] 或 Byte[] |
20 | image | byte[] 或 Byte[] |
需要注意的是,这些数据类型对应关系仅适用于大多数情况,具体的实现可能会因需要和其他因素而有所不同。此外,SqlDbType 枚举也提供了一些有用的成员,可用于根据数据库中使用的数据类型限制转换。因此在编写应用程序时请根据实际情况考虑使用哪种数据类型对应关系。
1.2、关系代码
根据上面对应的关系,可以编写如下代码,并非是全部类型转换代码,小伙伴们可以根据自己业务情况补齐
private static string DataTypeMssql(string dType){string dataType = string.Empty;if (dType.ToLower() == "int".ToLower()){dataType = "int";}else if (dType.ToLower() == "varchar".ToLower() || dType.ToLower() == "nvarchar".ToLower() || dType.ToLower() == "char".ToLower() || dType.ToLower() == "nchar".ToLower()|| dType.ToLower() == "text".ToLower() || dType.ToLower() == "ntext".ToLower()){dataType = "string";}else if (dType.ToLower() == "bigint".ToLower()){dataType = "long";}else if (dType.ToLower() == "smallint".ToLower()){dataType = "short";}else if (dType.ToLower() == "tinyint".ToLower()){dataType = "byte";}else if (dType.ToLower() == "decimal".ToLower()|| dType.ToLower() == "numeric".ToLower()){dataType = "decimal";}else if (dType.ToLower() == "money".ToLower() || dType.ToLower() == "smallmoney".ToLower()){dataType = "decimal";}else if (dType.ToLower() == "float".ToLower()){dataType = "double";}else if (dType.ToLower() == "real".ToLower()){dataType = "float";}else if (dType.ToLower() == "date".ToLower() || dType.ToLower() == "datetime".ToLower() || dType.ToLower() == "datetime2".ToLower()){dataType = "DateTime";}else if (dType.ToLower() == "datetimeoffset".ToLower()){dataType = "DateTimeOffset";}else if (dType.ToLower() == "time".ToLower()){dataType = "TimeSpan";}else if (dType.ToLower() == "char".ToLower()){dataType = "TimeSpan";}else if (dType.ToLower() == "binary".ToLower() || dType.ToLower() == "varbinary".ToLower() || dType.ToLower() == "image".ToLower()){dataType = "byte[]";}return dataType;}
2、C#与Mysql
2.1、对应关系
在 C# 和 Mysql 之间,以下是一些最常用的数据类型对应关系:
编号 | C#数据类型 | Mysql数据类型 |
---|---|---|
1 | bigint | long 或 Int64 |
2 | int | int 或 Int32 |
3 | smallint | short 或 Int16 |
4 | tinyint | byte 或者 SByte |
5 | decimal或 numeric | decimal 或 Decimal |
6 | float | double 或 Double |
7 | double | float 或 Single |
8 | date | DateTime 或 DateTime2 |
9 | datetime | DateTime 或 DateTime2 |
10 | time | TimeSpan 或 TimeSpan |
11 | char | string 或 String |
12 | varchar | string 或 String |
13 | text | string 或 String |
14 | binary | byte[] 或 Byte[] |
15 | varbinary | byte[] 或 Byte[] |
16 | blob | byte[] 或 Byte[] |
需要注意的是,在 C# 中,MySQL 的一些数据类型名称与 SQL Server 或 Oracle 等其他数据库不同,需要使用不同的对应关系来匹配这些名称。此外,这些数据类型对应关系仅适用于大多数情况,具体的实现可能会因需要和其他因素而有所不同。因此,在编写应用程序时请根据实际情况考虑使用哪种数据类型对应关系
2.2、关系代码
根据上面对应的关系,可以编写如下代码,并非是全部类型转换代码,小伙伴们可以根据自己业务情况补齐
private static string DataTypeMysql(string dType){string dataType = string.Empty;if (dType.ToLower() == "int".ToLower()){dataType = "int";}else if (dType.ToLower() == "bigint".ToLower()){dataType = "long";}else if (dType.ToLower() == "smallint".ToLower()){dataType = "short";}else if (dType.ToLower() == "tinyint".ToLower()){dataType = "byte";}else if (dType.ToLower() == "decimal".ToLower() || dType.ToLower() == "numeric".ToLower()){dataType = "decimal";}else if (dType.ToLower() == "float".ToLower()){dataType = "double";}else if (dType.ToLower() == "double".ToLower()){dataType = "float";}else if (dType.ToLower() == "date".ToLower() || dType.ToLower() == "datetime".ToLower()){dataType = "DateTime";}else if (dType.ToLower() == "time".ToLower()){dataType = "TimeSpan";}else if (dType.ToLower() == "varchar".ToLower() || dType.ToLower() == "char".ToLower() || dType.ToLower() == "text".ToLower()){dataType = "string";}else if (dType.ToLower() == "binary".ToLower() || dType.ToLower() == "varbinary".ToLower() || dType.ToLower() == "blob".ToLower()){dataType = "byte[]";}return dataType;}
3、C#与Oracle
3.1、对应关系
在 C# 和 Oracle 之间,以下是一些最常用的数据类型对应关系:
编号 | C#数据类型 | Mysql数据类型 |
---|---|---|
1 | bigint | long 或 Int64 |
2 | interval year to month | int 或 Int32 |
3 | number, int, smallint或 numeric | decimal 或 Decimal |
4 | binary_double | double 或 Double |
5 | binary_float | float 或 Single |
6 | date | DateTime 或 DateTime2 |
7 | interval day to second | TimeSpan 或 TimeSpan |
8 | char | string 或 String |
9 | clob | string 或 String |
10 | blob | byte[] 或 Byte[] |
3.2、关系代码
private static string DataTypeOracle(string dType){string dataType = string.Empty;if (dType.ToLower() == "interval year to month".ToLower()){dataType = "int";}else if (dType.ToLower() == "bigint".ToLower()){dataType = "long";}else if (dType.ToLower() == "int".ToLower() || dType.ToLower() == "smallint".ToLower() || dType.ToLower() == "numeric".ToLower()){dataType = "decimal";}else if (dType.ToLower() == "binary_double".ToLower()){dataType = "double";}else if (dType.ToLower() == "binary_float".ToLower()){dataType = "float";}else if (dType.ToLower() == "date".ToLower()){dataType = "DateTime";}else if (dType.ToLower() == "interval day to second".ToLower()){dataType = "TimeSpan";}else if (dType.ToLower() == "char".ToLower() || dType.ToLower() == "clob".ToLower() || dType.ToLower() == "blob".ToLower()){dataType = "string";}return dataType;}
原则:Write Less Do More!
简介:一只喜欢全栈方向的程序员,专注基础和实战分享,欢迎咨询,尽绵薄之力答疑解惑!
4、SqlDbType数据类型枚举
在C#开发语言里,也有一个数据类型枚举
4.1、如下图片
4.2、代码如下
代码里有数据类型的详细说明,包括一些取值范围
#region 程序集 netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51// C:\Users\15633\.nuget\packages\netstandard.library\2.0.3\build\netstandard2.0\ref\netstandard.dll#endregionnamespace System.Data{//// 摘要:// Specifies SQL Server-specific data type of a field, property, for use in a System.Data.SqlClient.SqlParameter.public enum SqlDbType{//// 摘要:// System.Int64. A 64-bit signed integer.BigInt = 0,//// 摘要:// System.Array of type System.Byte. A fixed-length stream of binary data ranging// between 1 and 8,000 bytes.Binary = 1,//// 摘要:// System.Boolean. An unsigned numeric value that can be 0, 1, or null.Bit = 2,//// 摘要:// System.String. A fixed-length stream of non-Unicode characters ranging between// 1 and 8,000 characters.Char = 3,//// 摘要:// System.DateTime. Date and time data ranging in value from January 1, 1753 to// December 31, 9999 to an accuracy of 3.33 milliseconds.DateTime = 4,//// 摘要:// System.Decimal. A fixed precision and scale numeric value between -10 38 -1 and// 10 38 -1.Decimal = 5,//// 摘要:// System.Double. A floating point number within the range of -1.79E +308 through// 1.79E +308.Float = 6,//// 摘要:// System.Array of type System.Byte. A variable-length stream of binary data ranging// from 0 to 2 31 -1 (or 2,147,483,647) bytes.Image = 7,//// 摘要:// System.Int32. A 32-bit signed integer.Int = 8,//// 摘要:// System.Decimal. A currency value ranging from -2 63 (or -9,223,372,036,854,775,808)// to 2 63 -1 (or +9,223,372,036,854,775,807) with an accuracy to a ten-thousandth// of a currency unit.Money = 9,//// 摘要:// System.String. A fixed-length stream of Unicode characters ranging between 1// and 4,000 characters.NChar = 10,//// 摘要:// System.String. A variable-length stream of Unicode data with a maximum length// of 2 30 - 1 (or 1,073,741,823) characters.NText = 11,//// 摘要:// System.String. A variable-length stream of Unicode characters ranging between// 1 and 4,000 characters. Implicit conversion fails if the string is greater than// 4,000 characters. Explicitly set the object when working with strings longer// than 4,000 characters. Use System.Data.SqlDbType.NVarChar when the database column// is nvarchar(max).NVarChar = 12,//// 摘要:// System.Single. A floating point number within the range of -3.40E +38 through// 3.40E +38.Real = 13,//// 摘要:// System.Guid. A globally unique identifier (or GUID).UniqueIdentifier = 14,//// 摘要:// System.DateTime. Date and time data ranging in value from January 1, 1900 to// June 6, 2079 to an accuracy of one minute.SmallDateTime = 15,//// 摘要:// System.Int16. A 16-bit signed integer.SmallInt = 16,//// 摘要:// System.Decimal. A currency value ranging from -214,748.3648 to +214,748.3647// with an accuracy to a ten-thousandth of a currency unit.SmallMoney = 17,//// 摘要:// System.String. A variable-length stream of non-Unicode data with a maximum length// of 2 31 -1 (or 2,147,483,647) characters.Text = 18,//// 摘要:// System.Array of type System.Byte. Automatically generated binary numbers, which// are guaranteed to be unique within a database. timestamp is used typically as// a mechanism for version-stamping table rows. The storage size is 8 bytes.Timestamp = 19,//// 摘要:// System.Byte. An 8-bit unsigned integer.TinyInt = 20,//// 摘要:// System.Array of type System.Byte. A variable-length stream of binary data ranging// between 1 and 8,000 bytes. Implicit conversion fails if the byte array is greater// than 8,000 bytes. Explicitly set the object when working with byte arrays larger// than 8,000 bytes.VarBinary = 21,//// 摘要:// System.String. A variable-length stream of non-Unicode characters ranging between// 1 and 8,000 characters. Use System.Data.SqlDbType.VarChar when the database column// is varchar(max).VarChar = 22,//// 摘要:// System.Object. A special data type that can contain numeric, string, binary,// or date data as well as the SQL Server values Empty and Null, which is assumed// if no other type is declared.Variant = 23,//// 摘要:// An XML value. Obtain the XML as a string using the System.Data.SqlClient.SqlDataReader.GetValue(System.Int32)// method or System.Data.SqlTypes.SqlXml.Value property, or as an System.Xml.XmlReader// by calling the System.Data.SqlTypes.SqlXml.CreateReader method.Xml = 25,//// 摘要:// A SQL Server user-defined type (UDT).Udt = 29,//// 摘要:// A special data type for specifying structured data contained in table-valued// parameters.Structured = 30,//// 摘要:// Date data ranging in value from January 1,1 AD through December 31, 9999 AD.Date = 31,//// 摘要:// Time data based on a 24-hour clock. Time value range is 00:00:00 through 23:59:59.9999999// with an accuracy of 100 nanoseconds. Corresponds to a SQL Server time value.Time = 32,//// 摘要:// Date and time data. Date value range is from January 1,1 AD through December// 31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999 with an accuracy// of 100 nanoseconds.DateTime2 = 33,//// 摘要:// Date and time data with time zone awareness. Date value range is from January// 1,1 AD through December 31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999// with an accuracy of 100 nanoseconds. Time zone value range is -14:00 through// +14:00.DateTimeOffset = 34}}
5、总结
对比不同数据库和C#之间的数据类型,可以发现,大同小异,大部分都是基本一致,有个别类型名称不一样但表示的C#数据类型是一致的。
实际项目中,其实C#与Mssql数据库一般都是两者配合使用,只有在第三方插件或者维护其他项目时才会用到。