"SQL Server data type, C # data type, SqlDbType" correspondence and conversions

Source: Internet
Author: User

SQL Server data type (for example: varchar)
Convert to SqlDbType type
public static SqlDbType Sqltypestring2sqltype (String sqltypestring)
{
SqlDbType DbType = sqldbtype.variant;//Default to 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 conversion to C # data type
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://a custom data type
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;
}
}
Code:


data types in SQL Server, converted to type types in C #
public static Type Sqltypestring2csharptype (String sqltypestring)
{
SqlDbType Dbtpe = Sqltypestring2sqltype (sqltypestring);
Return Sqltype2csharptype (DBTPE);
}
Converts a data type in SQL Server into a string of type in C #
public static string Sqltypestring2csharptypestring (String sqltypestring)
{
Type type = Sqltypestring2csharptype (sqltypestring);
return type. Name;
}

"SQL Server data type, C # data type, SqlDbType" correspondence and conversions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.