Translate

Home > November 2014

November 2014

Entity Framework Connection Use As SqlConnection

Thursday, November 13, 2014 Category : , 0

        public static string GetConnectionString()
        {
            string entityConnectionString = ConfigurationManager.ConnectionStrings["ERPEntities"].ConnectionString;
            string providerConnectionString = new EntityConnectionStringBuilder(entityConnectionString).ProviderConnectionString;
            return providerConnectionString;
        }

C# DataTable to List Convert

Wednesday, November 12, 2014 Category : , 0


public static List DataTableToList(this DataTable table) where T : class, new()

        {
           try
            {
                List list = new List();
                foreach (var row in table.AsEnumerable())
               {
                    T obj = new T();
                    foreach (var prop in obj.GetType().GetProperties())
                    {
                        try
                        {
                            PropertyInfo propertyInfo = obj.GetType().GetProperty(prop.Name);

                            propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);

                        }
                        catch
                        {
                            continue;
                        }
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch
            {
                return null;
            }
        }

Powered by Blogger.