C # Using Reflection to obtain values in DbDataReader

Source: Internet
Author: User
Tags file url

Overview
In my previous project, when I Read values in DbDataReder, I used Reader. Read () to Read values one by one based on the name. Since I learned to use reflection to Read data, everything became easy.
Previous practices
Define an Entity
Public class FileInformationModel
{
# Region Public Property
 
/// <Summary>
/// Gets and sets the file ID
/// </Summary>
Public string FileID {get; set ;}
 
/// <Summary>
/// Gets and sets the file name
/// </Summary>
Public string FileName {get; set ;}
 
/// <Summary>
/// Gets and sets the file save type
/// </Summary>
Public int? FileSaveType {get; set ;}
 
/// <Summary>
/// Gets and sets the file url
/// </Summary>
Public string FileUrl {get; set ;}
 
/// <Summary>
/// Gets and sets the file is new
/// </Summary>
Public bool? IsNew {get; set ;}
 
/// <Summary>
/// Gets and sets the file last access time
/// </Summary>
Public DateTime? LastAccessTime {get; set ;}
 
/// <Summary>
/// Gets and sets the file modity time
/// </Summary>
Public DateTime? ModifyTime {get; set ;}
 
/// <Summary>
/// Gets and sets the file version
/// </Summary>
Public int? Version {get; set ;}
 
/// <Summary>
/// Gets and sets the file content owner
/// </Summary>
Public string ContentOwner {get; set ;}
 
/// <Summary>
/// Gets and sets the file content type
/// </Summary>
Public string ContentType {get; set ;}
 
/// <Summary>
/// Gets and sets the file create date time
/// </Summary>
Public DateTime? CreateTime {get; set ;}
 
/// <Summary>
/// Gets and sets the file access control
/// </Summary>
Public string FileAccessControl {get; set ;}
 
/// <Summary>
/// Gets and sets the file from
/// </Summary>
Public string FileFrom {get; set ;}
 
# Endregion
}
 
Then read the DbDataReader
 
/// <Summary>
/// Execute reader by store procedure and parameter list
/// </Summary>
/// <Param name = "plain text"> store procedure </param>
/// <Param name = "parameters"> parameter list </param>
/// <Returns> data reader </returns>
Public DbDataReader ExecuteReader (string plain text, List <DbParameter> parameters, out DbConnection conn)
{
Lock (lockObject)
{
Conn = new MySqlConnection (ConnectionString );
MySqlCommand command = new MySqlCommand ();
PrepareCommand (command, conn, plain text, parameters );
MySqlDataReader mySqlDataReader = command. ExecuteReader ();
Return mySqlDataReader;
}
}
 
 
Then read it like this.
/// <Summary>
/// Query FileInformationModel entity list by FileInformationModel entity
/// </Summary>
/// <Param name = "entity"> FileInformationModel entity </param>
/// <Returns> FileInformationModel entity list </returns>
Public List <FileInformationModel> Query (FileInformationModel entity)
{
DbConnection conn;
Var result =
ConvertDataReaderToList (DBHelp. ExecuteReader (Constants. spSelectFileInformationByCondition,
GetParameters (entity), out conn ));
ContentHelp. CloseConnection (conn );
Return result;
}
 
/// <Summary>
/// Convert data reader to FileInformationModel entity list
/// </Summary>
/// <Param name = "reader"> Db DataReader </param>
/// <Returns> FileInformationModel entity list </returns>
Private List <FileInformationModel> ConvertDataReaderToList (DbDataReader reader)
{
List <FileInformationModel> fileInformationList = new List <FileInformationModel> ();
Using (reader)
{
While (reader. Read ())
{
FileInformationModel entity = new FileInformationModel ();
Entity. ContentType = ContentHelp. GetObjectToString (reader ["ConntentType"]);
Entity. ContentOwner = ContentHelp. GetObjectToString (reader ["ContentOwner"]);
Entity. CreateTime = ContentHelp. GetObjectToDateTime (reader ["CreateTime"]);
Entity. FileAccessControl = ContentHelp. GetObjectToString (reader ["FileAccessControl"]);
Entity. FileFrom = ContentHelp. GetObjectToString (reader ["FileFrom"]);
If (ContentHelp. GetObjectToString (reader ["IsNew"])! = Null)
Entity. IsNew = Convert. ToBoolean (ContentHelp. GetObjectToInt (reader ["IsNew"]);
Entity. FileName = ContentHelp. GetObjectToString (reader ["FileName"]);
Entity. FileSaveType = ContentHelp. GetObjectToInt (reader ["FileSaveType"]);
Entity. FileUrl = ContentHelp. GetObjectToString (reader ["FileUrl"]);
Entity. FileID = ContentHelp. GetObjectToString (reader ["FileID"]);
Entity. LastAccessTime = ContentHelp. GetObjectToDateTime (reader ["LastAccessTime"]);
Entity. ModifyTime = ContentHelp. GetObjectToDateTime (reader ["ModifyTime"]);
Entity. Version = ContentHelp. GetObjectToInt (reader ["Version"]);
FileInformationList. Add (entity );
}
}
Return fileInformationList;
}
 
 
After reflection, everything becomes easy.
/// <Summary>
/// Execute reader by store procedure and parameter list
/// </Summary>
/// <Param name = "plain text"> store procedure </param>
/// <Param name = "parameters"> parameter list </param>
/// <Returns> data reader </returns>
Public List <T> ReadEntityList <T> (string plain text, List <DbParameter> parameters) where T: new ()
{
Lock (lockObject)
{
Using (MySqlConnection conn = new MySqlConnection (ConnectionString ))
{
Using (MySqlCommand command = new MySqlCommand ())
{
PrepareCommand (command, conn, plain text, parameters );
MySqlDataReader mySqlDataReader = command. ExecuteReader ();
Return ReadEntityListByReader <T> (mySqlDataReader );
}
}
}
}
 
/// <Summary>
/// Read entity list by reader
/// </Summary>
/// <Typeparam name = "T"> entity </typeparam>
/// <Param name = "reader"> data reader </param>
/// <Returns> entity </returns>
Private List <T> ReadEntityListByReader <T> (MySqlDataReader reader) where T: new ()
{
List <T> listT = new List <T> ();
Using (reader)
{
While (reader. Read ())
{
T inst = new T ();
Foreach (var pi in typeof (T). GetProperties (BindingFlags. Instance | BindingFlags. Public ))
{
Var obj = new object ();
Try
{
Obj = reader [pi. Name];
}
Catch (Exception ex)
{
Continue;
}
 
If (obj = DBNull. Value | obj = null)
Continue;
Var si = pi. GetSetMethod ();
If (si = null)
Continue;
Pi. SetValue (inst, obj, null );
}
ListT. Add (inst );
}
}
Return listT;
}
 
 
And then
/// <Summary>
/// Query FileInformationModel entity list by FileInformationModel entity
/// </Summary>
/// <Param name = "entity"> FileInformationModel entity </param>
/// <Returns> FileInformationModel entity list </returns>
Public List <FileInformationModel> Query (FileInformationModel entity)
{
Return DBHelp. ReadEntityList <FileInformationModel> (Constants. spSelectFileInformationByCondition,
GetParameters (entity ));
}
 
 
Using Reflection makes Coding easier.

 

From spring yang

Related Article

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.