国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

一些通用的代碼

2019-11-17 02:05:12
字體:
供稿:網(wǎng)友

一些通用的代碼

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using Maticsoft.DBUtility;using System.Reflection;using Page;using Common;using System.Data.SqlClient;namespace Test{
public class BaseDAL<T>    {        public string TableName { get; set; }        /// <summary>        /// 添加數(shù)據(jù)Model        /// </summary>        /// <param name="model">Model:數(shù)據(jù)庫model實(shí)體</param>        /// <returns></returns>        public int Add(T model)        {            #region            Type type = model.GetType();            PRopertyInfo[] pro = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);            StringBuilder st = new StringBuilder();            st.AppendFormat("INSERT INTO [Wooaimei].[dbo].[{0}] (", TableName);            for (int i = 0; i < pro.Length; i++)            {                if (i < pro.Length - 1)                {                    if (pro[i].Name != "Id")                    {                        st.AppendFormat("{0},", pro[i].Name);                    }                }                else                {                    if (pro[i].Name != "Id")                    {                        st.AppendFormat("{0}", pro[i].Name);                    }                }            }            st.Append(")  VALUES (");            for (int i = 0; i < pro.Length; i++)            {                if (i < pro.Length - 1)                {                    if (pro[i].Name != "Id")                    {                        if (pro[i].PropertyType == typeof(string))                        {                            st.AppendFormat("/'{0}/',", pro[i].GetValue(model, null) ?? "");                        }                        else if (pro[i].PropertyType == typeof(DateTime))                        {                            st.AppendFormat("CONVERT(varchar(300),'{0}', 120),", pro[i].GetValue(model, null) ?? "");                        }                        else if (pro[i].PropertyType == typeof(bool))                        {                            st.AppendFormat("{0},", (bool)pro[i].GetValue(model, null) == false ? 0 : 1);                        }                        else                        {                            st.AppendFormat("{0},", pro[i].GetValue(model, null) ?? "");                        }                    }                }                else                {                    if (pro[i].Name != "Id")                    {                        if (pro[i].PropertyType == typeof(string))                        {                            st.AppendFormat("/'{0}/'", pro[i].GetValue(model, null) ?? "");                        }                        else if (pro[i].PropertyType == typeof(DateTime))                        {                            st.AppendFormat("CONVERT(varchar(300),'{0}', 120)", pro[i].GetValue(model, null) ?? "");                        }                        else if (pro[i].PropertyType == typeof(bool))                        {                            st.AppendFormat("{0}", (bool)pro[i].GetValue(model, null) == false ? 0 : 1);                        }                        else                        {                            st.AppendFormat("{0}", pro[i].GetValue(model, null) ?? "");                        }                    }                }            }            st.Append(") ");            return DbHelperSQL.ExecuteSql(st.ToString());            #endregion        }        /// <summary>        ///         /// </summary>        /// <param name="model"></param>        /// <param name="i"></param>        public void Add(T model, out int a)        {            #region            Type type = model.GetType();            PropertyInfo[] pro = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);            StringBuilder st = new StringBuilder();            st.AppendFormat("INSERT INTO [Wooaimei].[dbo].[{0}] (", TableName);            for (int i = 0; i < pro.Length; i++)            {                if (i < pro.Length - 1)                {                    if (pro[i].Name != "Id")                    {                        st.AppendFormat("{0},", pro[i].Name);                    }                }                else                {                    if (pro[i].Name != "Id")                    {                        st.AppendFormat("{0}", pro[i].Name);                    }                }            }            st.Append(")  VALUES (");            for (int i = 0; i < pro.Length; i++)            {                if (i < pro.Length - 1)                {                    if (pro[i].Name != "Id")                    {                        if (pro[i].PropertyType == typeof(string))                        {                            st.AppendFormat("/'{0}/',", pro[i].GetValue(model, null) ?? "");                        }                        else if (pro[i].PropertyType == typeof(DateTime))                        {                            st.AppendFormat("CONVERT(varchar(300),'{0}', 120),", pro[i].GetValue(model, null) ?? "");                        }                        else if (pro[i].PropertyType == typeof(bool))                        {                            st.AppendFormat("{0},", (bool)pro[i].GetValue(model, null) == false ? 0 : 1);                        }                        else                        {                            st.AppendFormat("{0},", pro[i].GetValue(model, null) ?? "");                        }                    }                }                else                {                    if (pro[i].Name != "Id")                    {                        if (pro[i].PropertyType == typeof(string))                        {                            st.AppendFormat("/'{0}/'", pro[i].GetValue(model, null) ?? "");                        }                        else if (pro[i].PropertyType == typeof(DateTime))                        {                            st.AppendFormat("CONVERT(varchar(300),'{0}', 120)", pro[i].GetValue(model, null) ?? "");                        }                        else if (pro[i].PropertyType == typeof(bool))                        {                            st.AppendFormat("{0}", (bool)pro[i].GetValue(model, null) == false ? 0 : 1);                        }                        else                        {                            st.AppendFormat("{0}", pro[i].GetValue(model, null) ?? "");                        }                    }                }            }            st.Append(");SELECT @@IDENTITY ");            object obje = DbHelperSQL.GetSingle(st.ToString());            if (obje != null)            {                a = Convert.ToInt32(obje);            }            else            {                a = 0;            }            #endregion        }        /// <summary>        /// 查詢行數(shù)        /// </summary>        /// <param name="strWhere">strWhere:根據(jù)strWhere查詢行數(shù)</param>        /// <returns>返回i行數(shù)值</returns>        public int Count(string strWhere)        {            StringBuilder sbstr = new StringBuilder();            sbstr.AppendFormat("SELECT COUNT(0) FROM [Wooaimei].[dbo].[{0}]", TableName);            sbstr.AppendFormat(" Where {0}", strWhere);            //return DbHelperSQL.ExecuteSql(sbstr.ToString());            object obj = DbHelperSQL.GetSingle(sbstr.ToString());            if (obj!=null)            {                return Convert.ToInt32(obj);            }            else            {                return 0;            }        }        public List<T> DataTableToList(DataTable dt)        {            throw new NotImplementedException();        }        /// <summary>        /// 刪除        /// </summary>        /// <param name="strWhere">strWhere:根據(jù)strWhere刪除行數(shù)</param>        /// <returns></returns>        public int DeleteList(string strWhere)        {            StringBuilder sb = new StringBuilder();            sb.AppendFormat("DELETE FROM [Wooaimei].[dbo].[{0}]
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汉寿县| 永川市| 海原县| 江北区| 平江县| 新宾| 湖口县| 巴青县| 巴里| 杨浦区| 四川省| 花垣县| 石城县| 涿州市| 达尔| 焦作市| 长丰县| 交口县| 安国市| 乌什县| 乐至县| 南陵县| 清远市| 新营市| 八宿县| 肇东市| 贵阳市| 满城县| 车致| 阜新市| 绩溪县| 兴山县| 梅州市| 洛南县| 晴隆县| 南昌市| 台东市| 安吉县| 普格县| 嘉峪关市| 宜昌市|