只修改数据表某条记录的部分字段(究极进化):
public class TableHelper
{
private Dictionary<string, object> temp;
public Dictionary<string, object> Temp { get { return temp; } } private SqlSugarClient db;
private HttpContext context;
public TableHelper(SqlSugarClient db)
{
this.db = db;
}
public TableHelper(SqlSugarClient db, HttpContext context)
{
this.db = db;
this.context = context;
} /// <summary>
/// 将对象类型的json数据转换成字典
/// </summary>
/// <typeparam name="T">数据表的模型类</typeparam>
/// <param name="data">json数据</param>
public void ToDictionary<T>(string data) where T : class
{
temp = new Dictionary<string, object>(); //重新实例化对象 T tableobj = JsonConvert.DeserializeObject<T>(data); //先反序列化
Type t = tableobj.GetType(); //获取对象的类型 foreach (PropertyInfo pi in t.GetProperties()) //遍历数据表对象的属性,将属性名和属性值存进字典里
{
if (pi.Name == "id")
{ }
else
{
Object value = pi.GetValue(tableobj);
if (value != null)
{
temp.Add(pi.Name, pi.GetValue(tableobj));
}
}
}
} /// <summary>
/// 额外添加要修改的字段(如果转换来的对象中已经存在该字段则不添加)
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddField(string key,object value)
{
foreach (string s in temp.Keys)
{
if (s == key)
{
}
else
{
temp.Add(key, value);
}
}
} public bool Update<T>(Dictionary<string, object> tableobj, int rowId) where T : class
{
return db.Update<T, int>(tableobj, rowId);
}
}
以下是以前写的(还是上边的写的更好):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using Newtonsoft.Json;
using SqlSugar;
using EQD.variable; namespace EQD.variable
{
/// <summary>
/// 这是一个数据表的基类,但要使用的前提是其他数据表要继承于它
/// </summary>
public class Table
{
private SqlSugarClient db = new SqlSugarClient(DBHelper.connectionString);
//public SqlSugarClient DB { get; set; } /// <summary>
/// 反序列化一下
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static Table DeserializeObject(string data)
{
Table table = new Table();
try
{
table = JsonConvert.DeserializeObject<Table>(data); //将要修改的内容全部转换成 dt
}
catch
{
}
return table;
} /// <summary>
/// 这是修改之后的更新方法(tableobj:反序列化之后包含要修改的内容的对象。contactsid:要修改的数据记录的id)
/// </summary>
/// <param name="tableobj">反序列化之后包含要修改的内容的对象</param>
/// <param name="contactsid">要修改的数据记录的id</param>
/// <returns></returns>
public bool Alter1(Table tableobj, int contactsid)
{
#region 将反序列化的对象中要修改的属性转换成字典集合
List<string> namelist = new List<string>();
List<Object> valuelist = new List<object>(); Type t = tableobj.GetType(); //获取对象的类型
foreach (PropertyInfo pi in t.GetProperties()) //遍历数据表对象的属性,分别将属性名和属性值存进两个集合里面
{
if (pi.Name == "id")
{ }
else
{
Object value = pi.GetValue(tableobj);
if (value != null)
{
namelist.Add(pi.Name);
valuelist.Add(value);
}
}
} Dictionary<string, object> temp = new Dictionary<string, object>(); for (int i = ; i < namelist.Count; i++)
{
temp.Add(namelist[i], valuelist[i]);
}
temp.Add("updateTime", DateTime.Now);
#endregion
bool result = db.Update<Table, int>(temp, contactsid); return result;
} #region 以下是原来写的内容
/// <summary>
/// 修改数据表中某条数据的内容
/// </summary>
/// <param name="tableobj">数据表的对象——一条数据</param>
/// <returns></returns>
public static Object Alter(Table tableobj)
{
List<string> namelist = new List<string>();
List<Object> valuelist = new List<object>(); Type t = tableobj.GetType(); //获取对象的类型
foreach (PropertyInfo pi in t.GetProperties()) //遍历数据表对象的属性,分别将属性名和属性值存进两个集合里面
{
if (pi.Name == "id")
{ }
else
{
Object value = pi.GetValue(tableobj);
if (value != null)
{
namelist.Add(pi.Name);
valuelist.Add(value);
}
}
} Dictionary<string, object> temp = new Dictionary<string, object>(); for (int i = ; i < namelist.Count; i++)
{
temp.Add(namelist[i], valuelist[i]);
}
temp.Add("updateTime", DateTime.Now);
return temp;
} //方法重载,只有第二个参数里包含的字段才能进行修改
public static Object AlterDate(Object tabledataobj, List<string> modifiableField)
{
List<string> namelist = new List<string>();
List<Object> valuelist = new List<object>(); Type t = tabledataobj.GetType();
foreach (PropertyInfo pi in t.GetProperties())
{
if (pi.Name == "id")
{ }
else
{
if (modifiableField.Contains(pi.Name))
{
Object value = pi.GetValue(tabledataobj);
if (value != null)
{
namelist.Add(pi.Name);
valuelist.Add(value);
}
}
}
} Dictionary<string, object> temp = new Dictionary<string, object>(); for (int i = ; i < namelist.Count; i++)
{
temp.Add(namelist[i], valuelist[i]);
}
temp.Add("updateTime", DateTime.Now);
return temp;
}
#endregion
}
}
调用:
//要传入的参数
string owner = context.Request.Form["owner"]; //修改人
int contactsid = Convert.ToInt32(context.Request.Form["contactsid"]); //要修改的联系人的id
//要修改的信息
string data = context.Request.Form["data"]; //这是一条json数据字符串,里面包含了要修改的信息的内容 Table table = new t_user();
table = Table.DeserializeObject(data); //如果传入的数据不对,则返回的 table 对象的属性全部为 null using (var db = new SqlSugarClient(DBHelper.connectionString))
{
try
{
var result = table.Alter1(table, contactsid);
}
catch (Exception e)
{ }
}
对于原来的代码里其实还是有一些很好的东西,比如动态生成对象且动态增加属性。但对于这个类,因为 SqlSuger 其实有对应的只修改某些列的方法,所以以上的代码还可以再调整,进行优化。
只数据部分字段:
var result = db.Queryable<t_user>().Where(u => u.CompanyId == comid && u.Isdel == false).OrderBy(u => u.addTime, OrderByType.Desc).ToPageList(page + , ); //先全部查出来,然后再用 tuple
List<Tuple<int, DateTime?>> list = new List<Tuple<int, DateTime?>>();
foreach (t_user u in result)
{
list.Add(new Tuple<int, DateTime?>(u.id,u.addTime));
}