国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 宁波市| 滦南县| 萍乡市| 瑞昌市| 大方县| 瑞昌市| 高尔夫| 张家界市| 黑河市| 平武县| 呼伦贝尔市| 三明市| 抚宁县| 龙游县| 禹州市| 德庆县| 怀化市| 济南市| 赤峰市| 独山县| 井冈山市| 沙田区| 苏尼特左旗| 玉林市| 汾西县| 辛集市| 禹州市| 卢氏县| 诸暨市| 华容县| 华亭县| 巴马| 阿图什市| 阜平县| 色达县| 湖北省| 鹿泉市| 香港 | 溧水县| 通城县| 萨嘎县|