栀子花开

追求完美

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace ClassLibrary1
{
   public  class Class1
    {
       SqlConnection conn;
       public Class1()
       {
          

       }

       public IList<Authors>  getAuthors()
       {
           conn = new SqlConnection(str);
           conn.Open();
           SqlCommand comm = new SqlCommand("select au_lname as Au_lname, contract as Contract,au_fname as Au_fname ,zip from authors ", conn);
           SqlDataReader reader = comm.ExecuteReader();
           IList<Authors> authorsColl = new List<Authors>();
           while (reader.Read())
           {
               Authors authors=new Authors();
               ReaderToObject(reader, authors);
               authorsColl.Add(authors);
           }
         
         
           return authorsColl;
       }
       private static string str = "server=.;uid=sa;pwd=sa;database=pubs;Max Pool Size = 512;";
       private void ReaderToObject(IDataReader reader, object targetObj)
       {
          
           for (int i = 0; i < reader.FieldCount; i++)
           {
               System.Reflection.PropertyInfo propertyInfo = targetObj.GetType().GetProperty(reader.GetName(i));
               if (propertyInfo != null)
               {
                   if (reader.GetValue(i) != DBNull.Value)
                   {
                       if (propertyInfo.PropertyType.IsEnum)
                       {
                           propertyInfo.SetValue(targetObj, Enum.ToObject(propertyInfo.PropertyType, reader.GetValue(i)), null);
                       }
                       else
                       {
                           propertyInfo.SetValue(targetObj, reader.GetValue(i), null);
                       }
                   }
               }
           }
       }

 


    }
}

posted on 2007-07-05 23:29  杨林  阅读(218)  评论(0)    收藏  举报