. NET simple database Operations Framework 1.3

Source: Internet
Author: User
 the properties that are based on the library operations, entity classes, and the section writing SQL statement generation first I wrote an interface generated by SQL statements 
Public interface GetSQL {///<summary>///add///</summary>///<type param name= "T" > class name </typeparam>///<param name= "T" > entity class </param>///<returns><
        /returns> list<object> sava<t> (T-t); 
        <summary>///modified///</summary>///<typeparam name= "T" > class name </typeparam> <param name= "T" > entity class </param>///<returns></returns> list<object&gt ;
        Update<t> (T-t); 
        <summary>///modified///</summary>///<typeparam name= "T" > class name </typeparam> <param name= "SQL" > Execute statement </param>///<param name= "args" > Parameters </param>///
        <returns></returns> list<object> update<t> (String sql, params object[] args); <summary>///Query///</summary>///<typeparam name= "T" > class name </typeparam>///<param "obj" > Primary key value Name=
        Gt
        <returns></returns> list<object> query<t> (object obj); 
        <summary>///Query///</summary>///<typeparam name= "T" > class name </typeparam> <param name= "SQL" > Execute statement </param>///<param name= "args" > Parameters </param>///
        <returns></returns> list<object> query<t> (String sql, params object[] args); 
        <summary>///Delete///</summary>///<typeparam name= "T" > class name </typeparam> <param name= "SQL" > Execute statement </param>///<param name= "args" > Parameters </param>///
        <returns></returns> list<object> delete<t> (String sql, params object[] args);
       <summary>///Paging Query </summary>///<typeparam name= "T" ></typeparam>///<param name= "PageCount" > Line Number </param>///<param name= "PageIndex" > Current page number </param>///<param name= "SQL" > execution statement </ param>///<param name= "args" > Parameters </param>///<returns></returns> list&l
    t;object> page<t> (int pagecount,int PageIndex, String sql, params object[] args); }

Inside the method everyone can add according to their own needs, and then I implemented the SQL Server class, because of the large amount of code I did not send up a one-time, the first implementation of the new generated SQL statement code

 <summary>///generate new SQL and SqlParameter///</summary>///<typeparam name= "T" >
        Object Type </typeparam>///<param name= "T" > Object </param>///<returns></returns>  Public list<object> sava<t> (T t) {try {object[] Objattrs
                = NULL;
                list<string> listcolumnname = new list<string> ();
                Type ObjType = typeof (T);
                Objattrs = Objtype.getcustomattributes (typeof (Dbattribute), true); StringBuilder sb = new StringBuilder ();//generated SQL statement sb.
                Append ("INSERT into"); Read stitching table name if (Objattrs.length > 0) {dbattribute attr = objattrs[0]
                    As Dbattribute; Sb. Append (attr!= null? attr.
                FiledName:objType.Name); } else {sb. ApPend (Objtype.name); } list<propertyinfo> Pro = Objtype.getproperties (). ToList ()///Get all public properties of entity class//Get Required property for (int i = 0; i < Pro. Count; i++) {objattrs = Pro[i].
                    GetCustomAttributes (typeof (Dbattribute), true);
                        if (Objattrs.length > 0) {dbattribute attr = objattrs[0] as Dbattribute; if (attr!= null) {if (attr). Fieldprops = = "2" | | attr. Fieldprops = = "3") {Pro.
                            Remove (Pro[i]); Removes the need for new property i--; }}} sb.
                Append ("("); List<string> list = (from S in Pro select S.name).
                ToList (); Sb. Append (String. Join (",", list)); Sb.
                Append (") values"); Sb.
                Append ("("); List = (from S in Pro select @ "+ s.name)."
                ToList (); Sb. Append (String.
                Join (",", list)); Sb.
                Append (")"); sqlparameter[] par = new Sqlparameter[pro.
                Count ()]; for (int i = 0; i < par. Length; i++) {Par[i] = new SqlParameter ("@" + pro[i]. Name, (pro[i). GetValue (t, null) = = null? "": Pro[i]. GetValue (t, null)).
                ToString ()); } sb.
                Append ("SELECT @ @IDENTITY"); return new list<object> () {sb.
            ToString (), par};
            catch (Exception ex) {throw; }

        }

First blog, a lot of places do not understand, you are welcome to criticize correct, thank you. Contact qq:1831996117. Add Friend note is blog

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.