Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // not using any Params for this example but left it in
- List<Parameter> Params = new List<Parameter>();
- // Get our first list of data
- List<TestClass_1> testlist1 = new List<TestClass_1>();
- testlist1 = ExecuteSqlDataReader<TestClass_1>("<your SQL query here>", "<your SQL connection here>", Params);
- // Get our second list of data
- List<TestClass_2> testlist2 = new List<TestClass_2>();
- testlist2 = ExecuteSqlDataReader<TestClass_2>("<your SQL query here>", "<your SQL connection here>", Params);
- }
- /// <summary>
- /// Generic function that uses SqlClient to connect and retrieve data from a DB
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="SQLText"></param>
- /// <param name="SQLConnectionStrong"></param>
- /// <param name="Params"></param>
- /// <returns>Return a collection of T </returns>
- /// <remarks></remarks>
- public static List<T> ExecuteSqlDataReader<T>(string SQLText, string SQLConnectionStrong, List<Parameter> Params)
- {
- List<T> ListOfT = new List<T>();
- using (SqlConnection SQLcon =new SqlConnection(SQLConnectionStrong))
- {
- using (SqlCommand SQLCMD =new SqlCommand(SQLText, SQLcon))
- {
- SQLCMD.CommandText = SQLText; // set the SQL command (SP or in line SQL)
- SQLCMD.CommandType = CommandType.Text; // set the command type (StoredProcedure or Text)
- //SQLCMD.CommandTimeout = mySQLSection.CommandTimeout; // set the command time out
- foreach (Parameter p in Params)
- {
- SQLCMD.Parameters.AddWithValue(p.Name, p.Value);
- }
- if(SQLcon.State == ConnectionState.Closed)
- {
- SQLcon.Open(); // open the connection
- }
- using (SqlDataReader myReader = SQLCMD.ExecuteReader()) // create the Data Reader and get the Data
- {
- DataTable dt = myReader.GetSchemaTable(); // Get the Table Schema (Needed to identify each column type)
- while(myReader.Read()) // for each record in the data set
- {
- T tmp = CreateInstanceOf_T<T>(); // create an instance of T
- int index = 0; // reset the column indexer
- foreach(DataRow row in dt.Rows) // for each column in the table
- {
- var v = typeof(T).GetProperty(row[0].ToString()); // get the type
- try
- {
- if (v != null)
- {
- switch (v.PropertyType.ToString())
- {
- case "System.Int32":
- typeof(T).GetProperty(row[0].ToString()).SetValue(tmp, myReader.GetInt32(index)); // to Int32
- break;
- case "System.Guid":
- typeof(T).GetProperty(row[0].ToString()).SetValue(tmp, myReader.GetGuid(index)); // to Guid
- break;
- case "System.String":
- if(!myReader.IsDBNull(index))
- typeof(T).GetProperty(row[0].ToString()).SetValue(tmp, myReader.GetString(index)); // to String
- break;
- case "System.Boolean":
- typeof(T).GetProperty(row[0].ToString()).SetValue(tmp, myReader.GetBoolean(index)); // to Boolean
- break;
- case "System.Decimal":
- typeof(T).GetProperty(row[0].ToString()).SetValue(tmp, myReader.GetDecimal(index)); // to Decimal
- break;
- // todo :: complete the convertion for oter types
- default:
- break;
- }
- }
- }
- catch (Exception)
- {
- // todo :: should log any errors here
- }
- index++; // increment the column indexer
- }
- ListOfT.Add(tmp); // add the temp object (T) to the ListOfT
- }
- }
- }
- }
- return ListOfT;
- }
- public static T CreateInstanceOf_T<T>()
- {
- T tmp = (T)typeof(T).GetConstructor(new System.Type[] { }).Invoke(new object[] { });
- return tmp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement