利用反射实现通用型DAL

利用反射实现通用型DAL(没什么用)

//调用DataRowToModel 获取对象
public T GetModel<T> (int id)
        {
			Type type = typeof(T);
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select  top 1 * from "+type.Name);
			strSql.Append(" where id="+id.ToString());
			DataSet ds = DbHelperSQL.Query(strSql.ToString(), constr);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel<T>(ds.Tables[0].Rows[0]);
            }
            else
            {
                return default;
            }
        }
public T DataRowToModel<T>(DataRow row)
        {
			//获取类型
			Type type = typeof(T);
			//创建对象
			T model= (T)Activator.CreateInstance(type);
			PropertyInfo[] plist = type.GetProperties();
			if (row != null)
			{
				for (int i=0; i<plist.Length;i++)
				{

					if (row[plist[i].Name] != null && row[plist[i].Name].ToString() != "")
					{
						Type type1 = row[i].GetType();
                        if (type1.Name == "Int32")
                        {
                            plist[i].SetValue(model, int.Parse(row[plist[i].Name].ToString()));
                        }
                        else if (type1.Name == "String")
                        {
                            plist[i].SetValue(model, row[plist[i].Name].ToString());
                        }
                        else if (type1.Name == "DateTime")
                        {
                            plist[i].SetValue(model, Convert.ToDateTime(row[plist[i].Name].ToString()));
                        }
                    }
				}
            }
			return model;
        }
//添加对象
public int Add<T>(T model)
		{
			Type type = typeof(T);
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into "+type.Name+"(");
			PropertyInfo[] plist = type.GetProperties();
			for (int i = 1; i < plist.Length; i++)
			{
				strSql.Append(plist[i].Name + ",");
			}
			strSql.Remove(strSql.Length - 1, 1);
			strSql.Append(") values (");
			for (int i = 1; i < plist.Length; i++)
			{
				strSql.Append("@"+plist[i].Name + ",");
			}
			strSql.Remove(strSql.Length - 1, 1);
			strSql.Append(")");
			SqlParameter[] parameters = new SqlParameter[plist.Length - 1];
			for (int i = 1; i < plist.Length; i++)
			{
				parameters[i - 1] = new SqlParameter("@" + plist[i].Name, plist[i].GetValue(model));
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString(), constr, parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		//更新对象
		public bool Update<T>(T model)
		{
			Type type = typeof(T);
			PropertyInfo[] plist = type.GetProperties();
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update "+type.Name+" set ");
			for (int i = 1; i < plist.Length; i++)
			{
				strSql.Append(plist[i].Name + "=@"+ plist[i].Name+",");
			}
			strSql.Remove(strSql.Length - 1, 1);
			strSql.Append(" where "+ plist[0].Name+ "=@"+ plist[0].Name);
			SqlParameter[] parameters = new SqlParameter[plist.Length];
			for (int i = 0; i < plist.Length; i++)
			{
				parameters[i] = new SqlParameter("@" + plist[i].Name, plist[i].GetValue(model));
			}
			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), constr, parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值