Skip to main content

Generic Method for Class DB table mapping


·      Code for mapping db selection to the class

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace DataSourceLib
{
    public class DataSource
    {
        private DataSource() { }
        public DataSource(DataSet data) {
            if (data == null) { data = new DataSet(); }
            dataSet = data;
        }
        private DataSet dataSet { set; get; }


        public IList<T> GetList<T>()
        {
            List<T> list = new List<T>();
            try
            {
                DataTable dt = dataSet.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    T obj = (T)Activator.CreateInstance(typeof(T));
                    PropertyInfo[] propertyInfos;
                    propertyInfos = obj.GetType().GetProperties();
                    for (int i = 0; i < dataSet.Tables[0].Columns.Count; i++)
                    {
                        foreach (PropertyInfo propertyInfo in propertyInfos)
                        {
if (dt.Columns[i].ColumnName.ToLower() ==   propertyInfo.Name.ToLower())
                            {
var val = Convert.ChangeType(dr[dt.Columns[i].ColumnName], propertyInfo.PropertyType);
                            propertyInfo.SetValue(obj, val);
                            }
                        }
                    }
                    list.Add(obj);
                }
            }
            catch (Exception)
            {
                T obj = (T)Activator.CreateInstance(typeof(T));
                list.Add(obj);
            }
            return list;
        }
    }
}

·       Client code
       class Program
    {
        static void Main()
        {
            string connectionString = GetConnectionString();
            DataSet dataSet = ConnectToData(connectionString);
            DataSourceLib.DataSource dataSource = new DataSourceLib.DataSource(dataSet);
            IList< User > users = dataSource.GetList< User >();

        }
    }
    public class User
    {
        public string ClientName { get; set; }
        public int ClientCode { get; set; }
    }


Comments

Popular posts from this blog

NHibernate QueryOver Class And Projection....

Introduction The ICriteria API is NHibernate's implementation of Query Object . NHibernate 3.0 introduces the QueryOver api, which combines the use of Extension Methods and Lambda Expressions (both new in .Net 3.5) to provide a statically typesafe wrapper round the ICriteria API. QueryOver uses Lambda Expressions to provide some extra syntax to remove the 'magic strings' from your ICriteria queries. So, for example: .Add(Expression.Eq("Name", "Smith")) becomes: .Where<Person>(p => p.Name == "Smith") With this kind of syntax there are no 'magic strings', and refactoring tools like 'Find All References', and 'Refactor->Rename' work perfectly. Note: QueryOver is intended to remove the references to 'magic strings' from the ICriteria API while maintaining it's opaqueness. It is not a LINQ provider; NHibernate 3.0 has a built-in

Passing Data from View to Controller Using Ajax Example Jquery

Jquery       $ ( '#btnSaveComments' ). click ( function () { var comments = $ ( '#txtComments' ). val (); var selectedId = $ ( '#hdnSelectedId' ). val (); $ . ajax ({ url : '<%: Url.Action("SaveComments")%>' , data : { 'id' : selectedId , 'comments' : comments }, type : "post" , cache : false , success : function ( savingStatus ) { $ ( "#hdnOrigComments" ). val ( $ ( '#txtComments' ). val ()); $ ( '#lblCommentsNotification' ). text ( savingStatus ); }, error : function ( xhr , ajaxOptions , thrownError ) { $ ( '#lblCommentsNotification' ). text ( "Error encountered while saving the comments." ); } }); });     Controller    [ HttpPost ] public ActionResult SaveComments ( int id , string com

The Core Concepts of Angular -- Jithin CJ

I started to learn angular from 2016, I was very curious about the celibacy of this super hero. From my initial understanding is like, the power of angular is only limited on " html decoration "  But this JavaScript framework has the potential to re-define conventional html-css patterns . Modern browsers support for things like modules, classes, lambdas, generators, etc. These features fundamentally transform the JavaScript programming experience. But big changes aren't constrained merely to JavaScript. Web Components are on the horizon. The term Web Components usually refers to a collection of four related W3C specifications: Custom Elements - Enables the extension of HTML through custom tags.  HTML Imports - Enables packaging of various resources (HTML, CSS, JS, etc.).  Template Element - Enables the inclusion of inert HTML in a document.  Shadow DOM - Enables encapsulation of DOM and CSS.  Developers can create fully encapsulated (Shadow DOM) declar