Lightweight dynamic generation of updated SQL even with ADO

Source: Internet
Author: User

For whatever reason, sometimes the framework staff abandons NH or EF and uses the native database to access the object.

For graceful programming, use the lightweight dynamic generation I wrote to update the SQL extension method.

Remember that when you update only the modified fields in EF, we write:

                        var e = db. Entry (d);                         = entitystate.unchanged;                         = DateTime.Now;                        E.property ("uploadtime"true;                        Db. SaveChanges ();

The goal is to have the generated update SQL update only the Uploadtime field, otherwise all fields will be updated, and then the query must be updated again.
Since the mention of data warehousing, we have to update an entity in the data layer is best only one method, but an updated SQL can meet the requirements?

For these purposes, I have written this lightweight extension method:

     Public Static classSqlstringex {/// <summary>        ///dynamically generate updated SQL based on the assignment of the entity model. Avoid having to get re-saved first, or avoid writing multiple update SQL on Demand/// </summary>        /// <typeparam name= "T" ></typeparam>        /// <param name= "T" ></param>        /// <param name= "TableName" >data table name</param>        /// <param name= "Conditioncolumnname" >Update Condition column name, which must be included in Parameters</param>        /// <param name= "parameters" ></param>        /// <returns></returns>         Public Static stringGenerateupdatesql<t> ( ThisT T,stringTableName,stringConditioncolumnname, outList<sqlparameter>parameters)whereT:New() {Parameters=NewList<sqlparameter>(); StringBuilder Res=NewStringBuilder (); Res. AppendFormat ("Update {0} set", TableName); varProps =T.gettype ().            GetProperties (); stringWherepart =NULL; foreach(PropertyInfo Iteminchprops) {                varPropVal = Item. GetValue (T,NULL); varHasValue = propval! =NULL&&!propval.equals (0); if(hasValue) {varFiledname =item.                    GetFieldName (); Parameters. ADD (NewSqlParameter {Value = propval, parametername =filedname}); if(Filedname. Equals (Conditioncolumnname.tolower ())) {Wherepart=string. Format ("where {0}[email protected]{0}", Filedname); }                    Else{Res. AppendFormat ("{0}[email protected]{0},", Filedname); }}} res. Remove (Res. Length-1,1); Res.            Append (Wherepart); returnRes.        ToString (); }    }

The GetFieldName () method is shown in the previous article

With this method, the data-tier entity requires only an updated method, and does not need to splice the updated SQL itself.

list<sqlparameter> parameters =null;             var savesql = model. Generateupdatesql ("TableName""ID" out parameters);

Note that the default value of themodel property is two, the value type defaults to 0, the reference type and the default value of the non-null value type is NULL, and the value is updated when the value of the model property is not the default value .
Savesql is the sql,parameters that we are updating is the parameter that corresponds to the update. The ID parameter can also be a different column, but the value of the column as a condition in model cannot be the default value.

There is no very convenient, welcome to throw bricks.

Although the wheel is repeated, the convenience is not to make up for everything.

High performance requirements, please consider carefully, because the internal use of reflection.

Lightweight dynamic generation of updated SQL even with ADO

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.