Ing between SQL Server Type and C # type

Source: Internet
Author: User
SQL Server Type C # type
Bit Bool
Tinyint Byte
Smallint Short
Int Int
Bigint Long
Real Float
Float Double
Money Decimal
Datetime Datetime
Char String
Varchar String
Nchar String
Nvarchar String
Text String
Ntext String
Image Byte []
Binary Byte []
Uniqueidentifier Guid

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

Copy content to clipboard

Code :

// SQL server data type (for example, varchar)
// Convert to sqldbtype
Public static sqldbtype sqltypestring2sqltype (string sqltypestring)
{
Sqldbtype dbtype = sqldbtype. Variant; // The default value is 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;
}
Copy content to clipboard

Code:

// Convert the Data Type in SQL Server to the Data Type in C #
Public static type sqltypestring2csharptype (string sqltypestring)
{
Sqldbtype dbtpe = sqltypestring2sqltype (sqltypestring );

Return sqltype2csharptype (dbtpe );
}

// Convert the Data Type in SQL Server to a string of the type in C #
Public static string sqltypestring2csharptypestring (string sqltypestring)
{
Type type = sqltypestring2csharptype (sqltypestring );

Return type. Name;
}

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.