반사를 이용한 범용 DAL

27946 단어 .netc#리플렉스

반사를 이용한 범용 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;
			}
		}

좋은 웹페이지 즐겨찾기