Tips

  1. html 标签中img对象可以直接使用data协议,例子:
    <IMG
    SRC="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw
    AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz
    ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp
    a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl
    ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis
    F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH
    hhx4dbgYKAAA7"
    ALT="Larry">

    参考:http://bbs.blueidea.com/forum.php?mod=viewthread&tid=986770

  2. 对于刚插入数据库中的数据,我们可以使用以下方法获取数据表示
    SCOPE_IDENTITY ()
    @@IDENTITY
    IDENT_CURRENT('TableName')
    三种方法适用情况不同,具体参考MSDN:
              https://msdn.microsoft.com/zh-cn/library/ms175098.aspx
              https://msdn.microsoft.com/zh-cn/library/ms187342(v=sql.120).aspx
              https://msdn.microsoft.com/zh-cn/library/ms190315(v=sql.120).aspx

  3. 两个开发辅助方法(增加开发速度)
<span style="white-space:pre">	</span>/// <summary>
        /// 将DataTable数据根据列名和属性名是否相同转换成List集合,属性名列名区分大小写
        /// </summary>
        /// <typeparam name="T">要返回的集合类型</typeparam>
        /// <param name="table">源数据</param>
        /// <returns>转换后的集合数据</returns>
        public static List<T> TableToModels<T>(this DataTable table) where T : new()
        {
            List<T> list = new List<T>();
            List<string> Properties = typeof(T).GetProperties().Select(p => p.Name).ToList();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    T t = new T();
                    foreach (DataColumn col in table.Columns)
                    {
                        if (Properties.Contains(col.ColumnName) && row[col.ColumnName] != DBNull.Value)
                        {
                            var prop = typeof(T).GetProperty(col.ColumnName);
                            prop.SetValue(t, row[col.ColumnName], null);
                        }
                    }
                    list.Add(t);
                }
            }
            return list;
        }
        /// <summary>
        /// 将T与F的同名属性赋值给F的一个实例并返回,属性名区分大小写
        /// </summary>
        /// <typeparam name="T">类型T</typeparam>
        /// <typeparam name="F">转换的目标类型F</typeparam>
        /// <param name="t">原来的类实例</param>
        /// <returns>新的类实例</returns>
        public static F ModelToModel<T, F>(this T t)
            where T : new()
            where F : new()
        {
            F f = new F();
            System.Reflection.PropertyInfo[] tProperties = typeof(T).GetProperties();
            List<string> fPropertiesName = typeof(F).GetProperties().Select(p => p.Name).ToList();
            foreach (var proper in tProperties)
            {
                if (fPropertiesName.Contains(proper.Name) && proper.GetValue(t, null) != DBNull.Value && proper.GetValue(t, null) != null)
                {
                    var prop = typeof(F).GetProperty(proper.Name);
                    prop.SetValue(f, proper.GetValue(t, null), null);
                }
            }
            return f;
        }

posted @ 2015-11-28 21:29  <码仔>  阅读(189)  评论(0编辑  收藏  举报