·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 一些通用的代码
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> /// 添加数据Model /// </summary> /// <param name="model">Model:数据库model实体</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> /// 查询行数 /// </summary> /// <param name="strWhere">strWhere:根据strWhere查询行数</param> /// <returns>返回i行数值</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:根据strWhere删除行数</param> /// <returns></returns> public int DeleteList(string strWhere) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("DELETE FROM [Wooaimei].[dbo].[{0}]