C#对象转DataRow,主键值自动生成
作者:C/S框架网|www.cscode.ne  发布日期:2019-12-31 10:44:59
  C#对象转DataRow,主键值自动生成



C# Code:

public static class DataConverter
{
   public static List<T> ToObject<T>(DataTable dt) where T : new()
   {
      List<T> list = new List<T>();
      foreach (DataRow R in dt.Rows)
      list.Add(ToObject<T>(R));
      return list;
   }
   
   public static T ToObject<T>(DataRow row) where T : new()
   {
      T obj = new T();
      Type type = typeof(T);
      
      string fname; object tv; object fvalue;
      
      for (int i = 0; i < row.Table.Columns.Count; i++)
      {
         fname = row.Table.Columns[i].ColumnName;
         fvalue = row[fname];
         
         PropertyInfo p = type.GetProperty(fname, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.IgnoreCase);
         
         if (p != null)
         {
            if (p.PropertyType.IsArray)//数组类型,单独处理
            {
               p.SetValue(obj, fvalue, null);
            }
            else
            {
               if (String.IsNullOrEmpty(fvalue.ToString()))//空值
               tv = p.PropertyType.IsValueType ? Activator.CreateInstance(p.PropertyType) : null;//值类型
               else
               tv = System.ComponentModel.TypeDescriptor.GetConverter(p.PropertyType).ConvertFromString(fvalue.ToString());//创建对象
               
               p.SetValue(obj, tv, null);
            }
         }
      }
      return obj;
   }
   
   public static void ToRow(object obj, DataRow row, bool removeInjection = true)
   {
      Type type = obj.GetType();
      string fname; object fvalue;
      for (int i = 0; i < row.Table.Columns.Count; i++)
      {
         fname = row.Table.Columns[i].ColumnName;
         PropertyInfo p = type.GetProperty(fname,
         BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.IgnoreCase);
         if (p != null)
         {
            fvalue = p.GetValue(obj);
            
            //移除注入攻击关键词
            if (p.PropertyType == typeof(string) && fvalue != null && !String.IsNullOrWhiteSpace(fvalue.ToString()))
            fvalue = SQLInjection.RemoveKeywords(fvalue.ToString());
            
            row[fname] = fvalue;
         }
      }
   }
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



生成流水单号,记录唯一主键,如:201912311028033450859061


C# Code:

public static string GetRowID()
{
   string sn = new Random().Next(1, 1000000).ToString().PadLeft(7, '0');
   return DateTime.Now.ToString("yyyyMMddHHmmssfff") + sn;
}

//来源:C/S框架网(www.csframework.com) QQ:23404761




测试:


C# Code:


public static void PostData(ModelRequest data)
{
   //获取表结构
   DataTable dt = dalHelper.GetEmptyTableCache(tb_DocList.__TableName);
   
   //对象转换为记录DataRow
   DataConverter.ToRow(data, dt.Rows.Add());
   dt.Rows[0]["RowID"] = WebApiGlobals.GetRowID();//记录主键
   
   //DAL层实例
   dalBaseDataDict dal = new dalBaseDataDict(typeof(tb_DocList));
   dal.Database = _Database;
   
   bool ok = dal.Update(dt);//提交数据
}

//来源:C/S框架网(www.csframework.com) QQ:23404761




上一篇 下一篇