DataReader Data Binding

Source: Internet
Author: User

Using system;using system.data;using system.reflection;using system.data.common;using System.Data.SqlClient;using        System.collections.generic;namespace datareaderlist{public partial class Readerlist {//////Read Entity list Entity to read//data source///Success (Entity list)/failure (Null) public static IList Readentitylistbyreader (Db            DataReader reader) where T:new () {IList ilist = null;                try {ilist = new List (); using (reader) {while (reader.                        Read ()) {T model = new T (); foreach (var item in typeof (T). GetProperties (BindingFlags.Instance |                            BindingFlags.Public)) {var obj = new Object (); try {obj = Reader[item.                            Name];                   }         catch (Exception ex) {continue;                            } if (obj = = DBNull.Value | | obj = = NULL) continue; var value = Item.                            GetSetMethod ();                            if (value = = null) continue; Item.                        SetValue (model, obj, null); } IList.                        ADD (model);                    Model = default (T);            }} return IList;            } finally {IList = null;        }}////////////////Data source///Success (Entity list)/failure (Null)            public static T Readentitybyreader (DbDataReader Reader) where t:new () {T model = default (T); try {using (reaDer) {if (reader.                        Read ()) {model = new T (); foreach (var item in typeof (T). GetProperties (BindingFlags.Instance |                            BindingFlags.Public)) {var obj = new Object (); try {obj = Reader[item.                            Name];                            } catch (Exception ex) {continue;                                } if (obj = = DBNull.Value | | obj = = NULL)                            Continue var value = Item.                            GetSetMethod ();                            if (value = = null) continue; Item.                        SetValue (model, obj, null); }}} RetuRN model;            } finally {model = default (T); }}///////Read entity list//////////////////////(Entity list)/fail (            Null) public static IList readentitylist (String cmdtext, dbparameter[] parameters) where t:new () { using (SqlConnection conn = new SqlConnection ("")) {using (SqlCommand cmd = new SqlCommand (                    ) {PrepareCommand (CMD, conn, cmdtext, parameters); SqlDataReader SDR = cmd.                    ExecuteReader ();                Return Readentitylistbyreader (SDR);        }}}////////////////////Data connection//EXECUTE data statement Statement parameters private static void PrepareCommand (SqlCommand cmd, SqlConnection conn, string Cmdtext, dbparameter[] Cmdpar MS) {if (conn.         State = ConnectionState.Open)       Conn.            Open (); Cmd.            Connection = conn;            Cmd.commandtext = Cmdtext;            Cmd.commandtype = Commandtype.text;//cmdtype;                    if (cmdparms! = null) {foreach (SqlParameter parameter in cmdparms) { if (parameter. Direction = = Parameterdirection.inputoutput | | Parameter. Direction = = ParameterDirection.Input) && (parameter. Value = = null) {parameter.                    Value = DBNull.Value; } cmd.                Parameters.Add (parameter); }            }        }    }}

DataReader Data binding

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.