Re: creating ExecuteQuery method



You just love to set challenges ;-p

How about the following (which should work on 2.0)... if you are doing
this lots, then look for HyperDescriptor to get significantly better
reflection performance (~100 x faster) - the only thing you'd need to
change in the code would be to add the
TypeDescriptionProviderAttribute, or call
HyperTypeDescriptionProvider.Add... the latter works well if added in
a static ctor (perhaps as part of Read<T>)

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

class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int EmployeeID { get; set; }
public DateTime BirthDate { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
foreach (Employee emp in Read<Employee>("SELECT FirstName,
LastName FROM Employees"))
{
Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName);
}
}
const string CS = @"Data Source=datachange;Initial
Catalog=Northwind;Integrated Security=True";

static IEnumerable<T> Read<T>(string command) where T : new()
{
using (SqlConnection conn = new SqlConnection(CS))
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = command;
conn.Open();
using (SqlDataReader reader =
cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
if (reader.Read())
{
// prepare a buffer and look at the properties
object[] values = new object[reader.FieldCount];
PropertyDescriptor[] props = new
PropertyDescriptor[values.Length];
PropertyDescriptorCollection allProps =
TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < props.Length; i++)
{
props[i] = allProps.Find(reader.GetName(i),
true);
}
do
{ // walk the data
reader.GetValues(values);
T t = new T();
for (int i = 0; i < props.Length; i++)
{
if (props[i] != null) props[i].SetValue(t,
values[i]);
}

yield return t;
} while (reader.Read());
}
while (reader.NextResult()) { } // ensure any trailing
errors caught
}
}
}

}

.



Relevant Pages

  • Re: Tag & Comboox
    ... public string FirstName; ... Employee emp1 = new Employee; ... > Is there a solution for the missing Tag property when using Comboboxes. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: A simple employee class : OOP Study
    ... > | Basically I am envisioning a Class called Employee: ... > private string lastName; ... > public string LastName ... > private int quantity; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A simple employee class : OOP Study
    ... incorrectly...making a database wrapper. ... > | Basically I am envisioning a Class called Employee: ... > private string lastName; ... > public string LastName ...
    (microsoft.public.dotnet.languages.csharp)
  • Databinding composite objects
    ... How can i bind a custom collection of my business objects which in turn contain other custom objects to datagrid. ... I have a collection of employee objects. ... private string _empNo; ... public string EmployeeNo{ ...
    (microsoft.public.dotnet.framework.windowsforms.databinding)

Loading