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