类user包含userid和name属性。其他类类似然后期望用以下方法,实现对数据库数据的更新操作
public void SetModels<T>(SqlCommand cmd,CommandType ct,string[] paramsname)
        {
            List<T> list = new List<T>();
            T obj;            obj = (T)Activator.CreateInstance(typeof(T));
            foreach (PropertyInfo fi in typeof(T).GetProperties())
            {
                foreach (string s in paramsname)
                {
                    if (fi.Name == s)
                    {
                        cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(obj, null));
                    }
                }
            }            DB.ExecuteNonQuery(cmd);
        }然后发现一个比较傻的问题,我不知道如何能把user类的实体(或者其他类的实体)的属性值给传进上边的方法里边。如果增加参数,比如是增加一个object[]是应该能实现,但总觉得怪怪的?以前一直是1.1所以不太清楚怎么写比较好了。谢谢各位的帮忙

解决方案 »

  1.   

    这样吗?
    user ur =new user();
    SetModels<u>(...)
      

  2.   

    那样是不行的。我现在改的用法是
    public void SetModels<T>(SqlCommand cmd,CommandType ct,string[] paramsname,T post)
    然后反射出post的属性值但感觉这样写好像对编程量的减轻作用不大...新接触,有错误请见谅...
      

  3.   

    来另一个问题,这样反射带来的性能消耗,有什么办法能够补救吗?
    -----------------------------------
    一般来说没有!
    ----------------------------------
    另一方面可能是因为我用得不熟练吧,数据层的代码看起来写少了,但每次这样调用的话业务的代码好像又增多了 - - 怎么去平衡呢?
    ----------------------------
    这个要看你的实际应用,不过我个人是不喜欢这样去做的!我一般把所有的SQL相关代码都写在数据层里面。而不是由业务层来生成SQLCommand或其它的数据相关操作!
      

  4.   

    public static List<T> CreateLinkedReportList<T>(IList itemEntitys, Bas_Report currentPaintingReport) where T : ReportEntity, new()
    {
    List<T> bindingData = new List<T>();
    ReportEntity preEntity = null; int index = 1;
    foreach (ItemEntity var in itemEntitys)
    {
    T entity = null;
    if (bindingData.Count == 0)
    {
    T firstItem = new T();
    firstItem.itemEntity = null;
    firstItem.report = currentPaintingReport; bindingData.Add(firstItem);
    }
    entity = new T();
    entity.itemEntity = var;
    entity.report = currentPaintingReport; bindingData.Add(entity);
    entity.index = index++;
    if (preEntity != null)
    {
    preEntity.NextItem = entity.Item;
    }
    preEntity = entity;
    }
    return bindingData;
    }这个是我写的一段,就是要把所有Entity串起来,希望对你有帮助
      

  5.   

    CommandType ct 这个参数可以省略的。基类里面判断一下是否合法的SQL语句,如果不就是就当存储过程处理。这样才节约劳动量。
      

  6.   

    #region FillParameter
    public static void FillParameter(DbCommand command, object model, params string[] inParameterNames)
    {
    FillParameter(command, model, new DbType(), null, inParameterNames);
    } public static void FillParameter(DbCommand command, object model, DbType outType, string outParameterName, params string[] inParameterNames)
    {
    Type type = model.GetType();
    PropertyInfo pi; if(outParameterName != null)
    {
    pi = type.GetProperty(outParameterName);
    AddParameter(command, outParameterName, outType, ParameterDirection.Output);
    } foreach(string inParameterName in inParameterNames)
    {
    pi = type.GetProperty(inParameterName);
    AddParameter(command, inParameterName, pi.GetValue(model, null));
    }
    }
    #endregion