SQL statement added based on model-valued auto-generation

Source: Internet
Author: User

        Static stringtable_name =""; /// <summary>        ///field names in model entities fields that are added relative to database tables///such as:///The model has a field named Cm_tablename,///where TableName is the database table name///then the value of this variable is cm_/// </summary>        Static stringCommon_prefix =""; /// <summary>                 ///generic Get SQL Insert string/// </summary>                 /// <typeparam name= "T" >Object Type</typeparam>                 /// <param name= "MD" >Object Type Parameters</param>                 /// <returns>return SQL INSERT statement; Call method:</returns>                  Public StaticString tosqlinsertbuilder<t> ( ThisT MD) {            //the field to insert            stringSql_str_insert_fields =""; //the value to insert            stringSql_str_insert_values =""; stringSql_str =""; //gets the type of the current instanceType type =Md.            GetType (); //Get the entity name (that is, the table name)String TableName =type.            Name.tostring (); //If the table name has not been given a value, assign the table name to it//table_name = str; //Insert the head of the statementString Sql_str_insert_header ="Insert into"+ TableName +"("; //Create an empty entity model by type            Objectobj =activator.createinstance (type); //get all fields and instance members that are public (if any)propertyinfo[] props = type. GetProperties (BindingFlags.Public |bindingflags.instance); //iterate through each of the fields            foreach(PropertyInfo Pinchprops) {                if(P.getvalue (MD,NULL) !=NULL)                {                    if(P.name.tolower ()! ="ID")                    {                        //returns the value of this (P) field in the entity passed in, and if this value is not NULL, enter the If method body                        if(P.getvalue (MD,NULL). ToString (). Trim (). Length >0) {Sql_str_insert_fields=Sql_str_insert_fields//Replace the field name with more than the database field name//. Replace (Common_prefix.tolower (). ToString (), "")+ P.name.tolower (). ToString () +","; Sql_str_insert_values= Sql_str_insert_values +"', '"+ P.getvalue (MD,NULL). ToString ().                        Trim (); }                    }                }            }            //            if(Sql_str_insert_fields. ToString (). Trim (). Length >3) {Sql_str=Sql_str_insert_header+ Sql_str_insert_fields. Substring (0, Sql_str_insert_fields. Length-1)                    +") VALUES ("+ sql_str_insert_values. Substring (2, Sql_str_insert_values. Length-2)                    +"');"; }            returnSql_str; }         Public StaticString Sqlinsertbuilder (DataSet Ds) {DataTable dt=NewDataTable (); if(Ds! =NULL)            {                if(Ds.Tables.Count >0) {DT= ds.tables[0]; }} String Sql_str_insert_header="INSERT INTO Mytablename ("; stringSql_str_insert_fields =""; stringSql_str_insert_values =""; stringSql_str =""; if(dt. Rows.Count >0) {Sql_str=""; foreach(DataRow Drinchdt. Rows) {Sql_str_insert_header="INSERT INTO Mytablename ("; Sql_str_insert_header= Sql_str_insert_header. Replace ("Mytablename", table_name.tostring ()); Sql_str_insert_fields="";//Sql_str_insert_fields + DC. Columnname.tolower ().   ToString () + ","; Sql_str_insert_values ="";//sql_str_insert_values + "', '" + DR[DC. ColumnName]. ToString (). Trim ();                    foreach(DataColumn DCinchdt. Columns) {if(DR[DC. ColumnName]. ToString (). Trim (). Length >0) {Sql_str_insert_fields=Sql_str_insert_fields+ DC. Columnname.tolower (). ToString () +","; Sql_str_insert_values=sql_str_insert_values+"', '"+DR[DC. ColumnName]. ToString ().                        Trim (); }                    }                    if(Sql_str_insert_fields. ToString (). Trim (). Length >3) {Sql_str= Sql_str +Sql_str_insert_header+ Sql_str_insert_fields. Substring (0, Sql_str_insert_fields. Length-1)                            +") VALUES ("+ sql_str_insert_values. Substring (2, Sql_str_insert_values. Length-2)                            +"');"; }                }            }            returnSql_str; }         Public Staticstring Sqlinsertbuilder (DataTable dt) {string Sql_str_insert_header="INSERT INTO Mytablename ("; stringSql_str_insert_fields =""; stringSql_str_insert_values =""; stringSql_str =""; if(dt. Rows.Count >0) {Sql_str=""; foreach(DataRow Drinchdt. Rows) {Sql_str_insert_header="INSERT INTO Mytablename ("; Sql_str_insert_header= Sql_str_insert_header. Replace ("Mytablename", table_name.tostring ()); Sql_str_insert_fields=""; Sql_str_insert_values=""; foreach(DataColumn DCinchdt. Columns) {if(DR[DC. ColumnName]. ToString (). Trim (). Length >0) {Sql_str_insert_fields=Sql_str_insert_fields+ DC. Columnname.tolower (). ToString () +","; Sql_str_insert_values=sql_str_insert_values+"', '"+DR[DC. ColumnName]. ToString ().                        Trim (); }                    }                    if(Sql_str_insert_fields. ToString (). Trim (). Length >3) {Sql_str= Sql_str +Sql_str_insert_header+ Sql_str_insert_fields. Substring (0, Sql_str_insert_fields. Length-1)                            +") VALUES ("+ sql_str_insert_values. Substring (2, Sql_str_insert_values. Length-2)                            +"');"; }                }            }            returnSql_str; }

SQL statement added based on model-valued auto-generation

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.