C # Discovery Tour 11th talk about using reflection and attributes to construct your own ORM framework (bottom)

Source: Internet
Author: User

Iterate through all the property binding information in the Innerreadvalue function. Call the Idatareader.getvalue function to get the raw data from the database, and then call the Fieldbindinfo.fromdatabase function to do some processing of the raw data, mainly for formatting and data type conversions, and then calling Propertyinfo.setval The UE function sets the property value of an object instance based on the data being read. This iterates through all the binding information and populates the property values of the object instance completely.

In the Readobjects function, you iterate through all the query data, create an object instance for each record, and after you traverse the database, we make an array of all the created object instances as the function return value, and then exit the function.

We can derive a series of functions to read objects from the database on top of this readobjects function. The Readobjects function realizes one of the core functions of the framework program reading data.

In addition, we have defined a contains function to determine whether a database record for an instance of an Application object exists.

New data

The Insertobjects function of a framework program can insert several objects into a database table. Its main code is

///<summary> 


///inserts several objects into the database


///</summary>


///<param name= "Objects" > Object list </param>


///<param name= "tablename" > Data table, if unspecified, use the default data table name </param>


///<returns> Number of database records inserted </returns>


public int insertobjects (System.Collections.IEnumerable Objects, string tablename)


{


if (Objects = null)


{


throw new ArgumentNullException ("Objects");


}


this. Checkbindinfo (Objects, false);


System.Collections.ArrayList list = new System.Collections.ArrayList ();


foreach (Object obj in Objects)


{


list. ADD (obj);


}


if (list. Count = = 0) {


return 0;


}


this. Checkconnetion ();


//Last executed SQL statement


string strlastsql = null;


int insertcount = 0;


using (System.Data.IDbCommand cmd = Myconnection.createcommand ())


{


foreach (Object obj in list)


{


tablebindinfo table = this. Getbindinfo (obj. GetType ());


string TableName2 = tablename;


if (TableName2 = null | | Tablename.trim (). Length = = 0)


{


TableName2 = table. TableName;


}


System.Collections.ArrayList values = new System.Collections.ArrayList ();


//Piecing together SQL statements


System.Text.StringBuilder mystr = new System.Text.StringBuilder ();


System.Text.StringBuilder myfields = new System.Text.StringBuilder ();


foreach (fieldbindinfo field in table). Fields)


{


if (field. Property.canread = = False)


{


throw new Exception ("Properties" + field. Property.Name + "is not writable");


}


Object v = field. Property.getvalue (obj, null);


if (v = null | | DBNull.Value.Equals (v))


{


continue;


}


values. ADD (field. Todatabase (v));


if (mystr.length > 0)


{


mystr.append (",");


myfields.append (",");


}


mystr.append ("?");


myfields.append (fixfieldname field. FieldName));


}//foreach


mystr.insert (0, Insert into) + fixtablename (TableName2)


+ "(" + myfields.tostring () + ") Values (");


Mystr.append (")");


string strSQL = Mystr.tostring ();


if (strSQL!= strlastsql)


{


//Reset SQL Command object


strlastsql = strSQL;


cmd. Parameters.clear ();


cmd.commandtext = strSQL;


for (int icount = 0; icount < values. Count; Icount + +)


{


cmd. Parameters.Add (cmd. CreateParameter ());


}


}


//Populating SQL command parameter values


for (int icount = 0; icount < values. Count; Icount + +)


{


((System.Data.IDbDataParameter) cmd. Parameters[icount]). Value = values[Icount];


}


//Execute SQL command to add records to datasheet


insertcount = cmd. ExecuteNonQuery ();


}//foreach


}//using


return insertcount;


}

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.