[C #] Step-by-step development of your own Automatic Code Generation Tool 2: field and C # variable Conversion

Source: Internet
Author: User
Step 2: Data Table preprocessing
////// Obtain the conversion statement from the database field to the type based on the variable type //////Private Static void initconvertmethod (fieldmodel field) {Switch (field. vartype) {Case "string": field. tovarconvertmethod = "convert. tostring "; break; Case" int ": field. tovarconvertmethod = "convert. toint32 "; break; Case" datetime ": field. tovarconvertmethod = "convert. todatetime "; break; Case" bool ": field. tovarconvertmethod = "convert. toboolean "; break; Case" float ": field. tovarconvertmethod = "C Onvert. tosingle "; break; Case" decimal ": field. tovarconvertmethod = "convert. todecimal "; break; Case" byte [] ": field. tovarconvertmethod = "(byte [])"; break; Case "money": field. tovarconvertmethod = "convert. todecimal "; break; default: Throw new exception (string. the conversion method for format ("variable type ({0}) is not defined! ", Field. vartype ));}}////// Initialize the default value of the database object class attribute //////Database entity attributes Private Static void initdefavarvarvalue (fieldmodel field) {Switch (field. dbtype) {Case "char": field. defaultvaluevar = "string. empty "; break; Case" nchar ": field. defaultvaluevar = "string. empty "; break; Case" varchar ": field. defaultvaluevar = "string. empty "; break; Case" nvarchar ": field. defaultvaluevar = "string. empty "; break; Case" text ": field. defaultvaluevar = "string. empty "; break; Case "ntext": field. defaultvaluevar = "string. empty "; break; Case" int ": field. defaultvaluevar = "0"; break; Case "bit": field. defaultvaluevar = "false"; break; Case "datetime": field. defaultvaluevar = "datetime. minvalue "; break; Case" float ": field. defaultvaluevar = "0"; break; Case "decimal": field. defaultvaluevar = "0"; break; Case "image": field. defaultvaluevar = "null"; break; Case "money": F Ield. defaultvaluevar = "0"; break; default: Throw new exception (string. Format ("database field type ({0}) undefined! ", Field. dbtype ));}}////// Obtain the data access parameter type /////////
  Private Static string getsqlparamtypefromsqldbtype (string dbtype) {Switch (dbtype. tolower () {Case "char": Return "sqldbtype. char "; Case" nchar ": Return" sqldbtype. nchar "; Case" varchar ": Return" sqldbtype. varchar "; Case" nvarchar ": Return" sqldbtype. nvarchar "; Case" text ": Return" sqldbtype. text "; Case" ntext ": Return" sqldbtype. ntext "; Case" int ": Return" sqldbtype. INT "; Case" bit ": Return" SQL Dbtype. bit "; Case" datetime ": Return" sqldbtype. datetime "; Case" float ": Return" sqldbtype. float "; Case" image ": Return" sqldbtype. image "; Case" decimal ": Return" sqldbtype. decimal "; Case" money ": Return" sqldbtype. money "; default: Throw new exception (string. format ("database field type ({0}) undefined! ", Dbtype ));}}////// Obtain the property variable type /////////
  Private Static string getvartypefromsqldbtype (string dbtype) {Switch (dbtype. tolower () {Case "char": Return "string"; Case "nchar": Return "string"; Case "varchar": Return "string"; Case "nvarchar ": return "string"; Case "text": Return "string"; Case "ntext": Return "string"; Case "int": Return "int"; Case "bit ": return "bool"; Case "datetime": Return "datetime"; Case "float": Return "float"; CA Se "image": Return "byte []"; Case "decimal": Return "decimal"; Case "money": Return "decimal"; default: Throw new exception (string. format ("database field type ({0}) undefined! ", Dbtype ));}}

 

 

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.