Universal INSERT or update database through model

Source: Internet
Author: User

 Public classDalinsertupdate<t>whereT:New()    {        /// <summary>        ///inserting non-(keyattribute) attributes into the specified table by model/// </summary>        /// <param name= "TableName" >the name of the table to insert into</param>        /// <param name= "Model" >the model to insert</param>        /// <returns></returns>         Public intInsert (stringTableName, T Model) {            varName =NewStringBuilder (); varValue =NewStringBuilder (); varParame =NewList<sqlparameter>(); Propertyinfo[] Propertys=Model.gettype ().            GetProperties (); foreach(varIteminchpropertys) {                if(NULL==item. GetCustomAttribute (typeof(Keyattribute))) {Name.append (item. Name). Append (","); Value.append ("@"). Append (item. Name). Append (","); Parame.add (NewSqlParameter ("@"+item. Name,item. GetValue (Model,NULL))); }} name.remove (Name.length-1,1); Value.remove (Value.length-1,1); stringSQL =string. Format ("INSERT into {0} ({1}) VALUES ({2})", TableName, name.tostring (), value.tostring ()); returnSqlhelper.executenonquery (sqlhelper.getconnsting (), CommandType.Text, sql.tostring (), Parame.ToArray ()); }        /// <summary>        ///the record that the specified (Keyattribute) attribute points to by the model update/// </summary>        /// <param name= "TableName" >the name of the table to update</param>        /// <param name= "Model" >the model to update</param>        /// <returns></returns>         Public intUpdate (stringTableName, T Model) {propertyinfo[] propertys=Model.gettype ().            GetProperties (); varstr =NewStringBuilder (); var where=NewStringBuilder (""); varParame =NewList<sqlparameter>(); foreach(varIteminchpropertys) {                if(NULL!=item. GetCustomAttribute (typeof(Keyattribute))) {                    where. Append (item. Name). Append ("="). Append ("@"). Append (item.                    Name); Parame.add (NewSqlParameter ("@"+ Item. Name, item. GetValue (Model,NULL))); }                Else{str. Append (item. Name). Append ("[email protected]"). Append (item. Name). Append (","); Parame.add (NewSqlParameter ("@"+ Item. Name, item. GetValue (Model,NULL))); }} str. Remove (str. Length-1,1); if(string. IsNullOrEmpty (where. ToString ()))Throw NewException ("Unable to update database without PrimaryKey found!"); stringsql =string. Format ("UPDATE {0} SET {1} WHERE {2}", TableName, str. ToString (),where.            ToString ()); returnSqlhelper.executenonquery (sqlhelper.getconnsting (), CommandType.Text, SQL.        ToString (), Parame.toarray ()); }    }

Universal INSERT or update database through model

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.