. NET simple database Operations Framework 1.6

Source: Internet
Author: User
 due to the paging section I have not thought how to write, so temporarily do not write, the follow-up will be updated. After writing the class that generates the SQL statement, I write the database operation interface 
Public interface Dboperation {///<summary>///add///</summary>/// ; Typeparam name= "T" > Type </typeparam>///<param name= "T" > Object </param>///<returns>
        Self-adding </returns> object sava<t> (T-t); 
        <summary>///Modify///</summary>///<typeparam name= "T" > Type </typeparam> <param name= "T" > Objects </param>///<returns> affected number of rows </returns> int update&lt ;
        T> (T-t); 
        <summary>///Modify///</summary>///<typeparam name= "T" > Type </typeparam>  <param name= "SQL" > Modified statements </param>///<param name= "args" > Parameters </param>///
        <returns> number of affected rows </returns> int update<t> (String sql, params object[] args);
       <summary>///Query Single object///</summary> <typeparam name= "T" > Type </typeparam>///<param name= "obj" > Primary key value </param>///<
        Returns></returns> T query<t> (object obj); 
        <summary>///Query///</summary>///<typeparam name= "T" > Type </typeparam>  <param name= "SQL" > Query statements </param>///<param name= "args" > Parameters </param>///
        <returns></returns> list<t> query<t> (String sql, params object[] args); 
        <summary>///Delete///</summary>///<typeparam name= "T" > Type </typeparam>  <param name= "SQL" > deleted statements </param>///<param name= "args" > Parameters </param>///
    <returns> number of affected rows </returns> int delete<t> (String sql, params object[] args); }

Then implement the methods of SQL Server

 public class Sqlserveroperation:dboperation {public string consql;
        Public getsql get=new Getsqlserver ();
        Public Dbhelpercs Db=null; <summary>///Instantiation Database Operation Object///</summary>///<param name= "Consql" ></param&
        Gt
            Public sqlserveroperation (String consql) {this.consql = Consql;
        Db=new SqlHelper (Consql); The public object sava<t> (T t) {list<object> List = get.
            Sava<t> (T); Return DB. Singleline (List[1] as sqlparameter[], list[0].
        ToString ()); public int update<t> (T-t) {list<object> List = get.
            Update<t> (T); Return Db.exectue (List[1] as sqlparameter[], list[0].
        ToString ()); public int update<t> (String sql, params object[] args) {list<object> List = ge
      T.update<t> (Sql,args);      Return Db.exectue (List[1] as sqlparameter[], list[0].
        ToString ()); Public T query<t> (object obj) {list<object> List = get.
            Query<t> (obj); SqlDataReader reader = db. Query (List[1] as sqlparameter[], list[0].
            ToString ()) as SqlDataReader;
        return readertolist<t> (reader) [0]; Public list<t> query<t> (String sql, params object[] args) {list<object> List = Get.
            Query<t> (Sql,args); SqlDataReader reader = db. Query (List[1] as sqlparameter[], list[0].
            ToString ()) as SqlDataReader;
        return readertolist<t> (reader); public int delete<t> (String sql, params object[] args) {list<object> List = ge
            t.delete<t> (sql, args); Return Db.exectue (List[1] as sqlparameter[], list[0].
        ToString ()); }///<summary>///DataReader-Generic/</summary>///<typeparam name= "T" > Incoming entity class </typeparam>///<param name= "Objreader" & Gt;datareader Object </param>///<returns></returns> public list<t> READERTOLIST&LT;T&G t; (IDataReader objreader) {using (objreader) {list<t> List = new L

                Ist<t> ();

                Gets the passed-in data type type Modeltype = typeof (T); Traversal DataReader Object while (Objreader.read ()) {///use the highest constructor that matches the specified parameter to create the specified type
                    The instance T model = activator.createinstance<t> ();
                        for (int i = 0; i < Objreader.fieldcount i++) {//Determine whether the field value is empty or does not exist if (! Isnullordbnull (Objreader[i])) {//Match field name Pr Opertyinfo pi = modeltype.getproperty (objreader.geTname (i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance |
                            Bindingflags.ignorecase);  
                                if (pi!= null) {//Bind the field with the same name in the entity object Pi. SetValue (model, Checktype (objreader[i), Pi.
                            PropertyType), NULL); }} list.
                ADD (model);
            } return list; }///<summary>///to determine the nullable type (* otherwise it will error)///</summary>///<par
        Am Name= The value of >datareader field </param>///<param name= "Conversiontype" > The type of the field </param>
            <returns></returns> Private Object Checktype (object value, Type conversiontype) { if (Conversiontype.isgenerictype && conversiontype.getgenerictypedefinition ().
  Equals (typeof (Nullable<>)))          {if (value = = null) return null; System.ComponentModel.NullableConverter nullableconverter = new System.ComponentModel.NullableConverter (
                Conversiontype);
            Conversiontype = Nullableconverter.underlyingtype;
        return Convert.changetype (value, conversiontype); ///<summary>///To determine if the specified object is a valid value///</summary>///<param name= "obj" >&
            lt;/param>///<returns></returns> private bool Isnullordbnull (object obj) { return (obj = null | | (obj is DBNull)) ?
        True:false; ///<summary>///DataReader model///</summary>///<typeparam name= "T" & 
        gt;</typeparam>///<param name= "objreader" ></param>///<returns></returns>
Public T readertomodel<t> (IDataReader objreader) {
            using (objreader) {if (Objreader.read ()) {T
                    ype Modeltype = typeof (T);
                    int count = Objreader.fieldcount;
                    T model = activator.createinstance<t> (); for (int i = 0; i < count; i++) {if (! Isnullordbnull (Objreader[i])) {PropertyInfo pi = Modeltype.getpropert Y (Objreader.getname (i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance |
                            Bindingflags.ignorecase); if (pi!= null) {pi. SetValue (model, Checktype (objreader[i), Pi.
                            PropertyType), NULL);
                }} return model;
        } return default (T); }
    }

To this end, my database simple operation framework has been written.
First Blog, a lot of places do not understand, you are welcome to criticize correct, thank you. Contact qq:1831996117. Add Friend note is a blog. If the source code, please add Friends contact me.

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.