利用反射实现通用型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;
			}
		}

你可能感兴趣的:(.net,c#,.net,反射)