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;
        }
相關文章
相關標籤/搜索