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);
}
}
}
}
}
}
}

浙公网安备 33010602011771号