using System; using System.Reflection; using System.Diagnostics; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; namespace Sample { class Class1 { [STAThread] static void Main(string[] args) { string ConStr="Data Source=localhost;User ID=youruser;Password=yourpassword;Initial Catalog=Northwind"; DataTable oTable; Class1 oClass = new Class1(); Sample.Employee oEmployee; System.DateTime sStartTime; TimeSpan elapsed; string Sql=""; int i=0; int Max=10000; try { Sql = "select EmployeeID,FirstName,LastName,BirthDate,HomePhone,"; Sql += "Address,City,PostalCode,Country "; Sql += " from Employees"; oTable = oClass.GetDataTable(Sql,ConStr); sStartTime = System.DateTime.Now; for(i=0;i<Max;i++) { foreach(DataRow oRow in oTable.Rows) { oEmployee = new Sample.Employee(); oEmployee.EmployeeID = Convert.ToInt32(oRow["EmployeeID"]); oEmployee.FirstName = oRow["FirstName"].ToString(); oEmployee.LastName = oRow["LastName"].ToString(); oEmployee.BirthDate = (DateTime)oRow["BirthDate"]; oEmployee.Address = oRow["Address"].ToString(); oEmployee.City = oRow["City"].ToString(); oEmployee.PostalCode = oRow["PostalCode"].ToString(); oEmployee.HomePhone = oRow["HomePhone"].ToString(); oEmployee.Country = oRow["Country"].ToString(); } } elapsed = System.DateTime.Now - sStartTime; Console.WriteLine("Old Method: " + elapsed.TotalMilliseconds.ToString()); oEmployee = new Sample.Employee(); object[,] oFields = oEmployee.GetFields(typeof(Sample.Employee)); sStartTime = System.DateTime.Now; for(i=0;i<Max;i++) { foreach(DataRow oRow in oTable.Rows) { oEmployee = new Sample.Employee(); oEmployee.SetFields(oFields,oRow); } } elapsed = System.DateTime.Now - sStartTime; Console.WriteLine("New Method: " + elapsed.TotalMilliseconds.ToString()); } catch (Exception e) { Debug.WriteLine(e.Message); } Console.ReadLine(); } public DataTable GetDataTable(string Sql,string ConnectionString) { SqlConnection oConn = new SqlConnection(); DataTable oTable = new DataTable(); try { oConn.ConnectionString = ConnectionString; oConn.Open(); SqlDataAdapter oDA = new SqlDataAdapter(Sql,oConn); oDA.Fill(oTable); oConn.Close(); } catch (Exception) { throw; } finally{ if (oConn.State == ConnectionState.Open) { oConn.Close(); } } return oTable; } } }
using System; namespace Sample { public class Employee : Sample.ColumnInfo { [ColumnAttributes("EmployeeID")] public int EmployeeID; [ColumnAttributes("FirstName")] public string FirstName; [ColumnAttributes("LastName")] public string LastName; [ColumnAttributes("Address")] public string Address; [ColumnAttributes("City")] public string City; [ColumnAttributes("PostalCode")] public string PostalCode; [ColumnAttributes("Country")] public string Country; [ColumnAttributes("HomePhone")] public string HomePhone; [ColumnAttributes("BirthDate")] public DateTime BirthDate; } }
using System; using System.Reflection; using System.Diagnostics; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; namespace Sample { public class ColumnInfo { public object[,] GetFields(Type t) { FieldInfo[] oFields = t.GetFields(); FieldInfo oField; Attribute[] attributes; object[,] StructureInfo = new object[oFields.Length,2]; try { for(int i =0;i<oFields.Length;i++) { oField = oFields[i]; attributes = Attribute.GetCustomAttributes(oField,typeof(Sample.ColumnAttributes),false); StructureInfo[i,0] = oField; StructureInfo[i,1] = attributes; } } catch (Exception e) { Debug.WriteLine(e.Message); } return StructureInfo; } public void SetFields(object[,] StructureInfo,DataRow oRow) { FieldInfo oField; Attribute[] attributes; try { for(int i =0;i<=StructureInfo.GetUpperBound(0);i++) { oField = (FieldInfo)StructureInfo[i,0]; attributes = (Attribute[])StructureInfo[i,1]; foreach(Attribute attribute in attributes) { Sample.ColumnAttributes oColumnAttributeName = (Sample.ColumnAttributes)attribute; if (oRow[oColumnAttributeName.ColumnName] != System.DBNull.Value) { oField.SetValue(this,oRow[oColumnAttributeName.ColumnName]); } break; } } } catch(Exception e) { Debug.WriteLine(e.Message); throw; } return; } } [AttributeUsage(AttributeTargets.Field,AllowMultiple = true)] public class ColumnAttributes : System.Attribute { public string ColumnName=""; public ColumnAttributes(string DataBaseColumnName) { this.ColumnName = DataBaseColumnName; } } }