当前位置:编程学习 > C#/ASP.NET >>

C#利用反射方便取得DbDataReader里的值

概述
在我以前做项目时,读DbDataReder里的值时都会用Reader.Read()然后根据名字来逐个读出.自从学会利用反射来读后,一切变得很容易.
以前的做法
定义一个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
}
 
然后读取DbDataReader
 
/// <summary>
/// Execute reader by store procedure and parameter list
/// </summary>
/// <param name="cmdText">store procedure</param>
/// <param name="parameters">parameter list</param>
/// <returns>data reader</returns>
public DbDataReader ExecuteReader(string cmdText, List<DbParameter> parameters,out DbConnection conn)
{
    lock (lockObject)
    {
          conn = new MySqlConnection(ConnectionString);
        MySqlCommand command = new MySqlCommand();
        PrepareCommand(command, conn, cmdText, parameters);
        MySqlDataReader mySqlDataReader = command.ExecuteReader();
        return mySqlDataReader;
    }
}
 
 
然后再如此读出
/// <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"]);
         &nbs

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,