我们通常使用update语句更新数据库记录,例如使用update user set username='001', nickname='Tom', age=18 where id = 1语句更新username、nickname或age字段的值。假设,我们只修改了username,并没有修改nickname和age,那么上面的 sql就显得多余了,改成update user set username='001' where id = 1才算完美,即哪些字段发生了变化就更新哪些字段。

此外,SQL Server数据库中有触发器,可监控到字段值的变更,例如在表user上创建触发器

create trigger [dbo].[tr_user_update]
on [dbo].[user]
After
update
as
declare @id int;
select @id = id from inserted;
if update(nickname) begin
--some code
end;

如果使用update user set username='001', nickname='Tom', age=18 where id = 1语句,即便nickname和age的值与数据库中完全一样,也会触发 some code,但这并不是我们期望的。

所以执行update更新前,有必要检查哪些字段需发生了修改,尤其是需要记录表变更历史的情形。本例中,笔者使用System.Reflection.PropertyInfo和DataRow检查发生更新的字段,并拼接要更新的Update SQL语句。

首先,按照表user创建User.cs类

class User
{
// 参照用户表User的字段定义属性
// 不包括系统字段
// 仅包括用户会修改的字段
// 属性名必须和字段名一致
public string UserName { get; set; }
public string NickName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public int Age { get; set; }
}

其次,创建赋值函数InitEntity(DataRow, Obj)

public static Obj InitEntity<Obj>(System.Data.DataRow row, Obj entity) where Obj : new()
{
if (entity == null)
entity = new Obj();
// 取得entity的类型
Type type = typeof(Obj);
// 取得entity的属性
System.Reflection.PropertyInfo[] props = type.GetProperties();
// 遍历entity属性集合,按照属性类型给其赋值。通过entity属性名从row中取得对应的值。
foreach (System.Reflection.PropertyInfo prop in props)
{
if (prop.PropertyType.FullName.Equals("System.Int32"))
{
prop.SetValue(entity
, Convert.ChangeType(MyFuncLib.dtv(row, prop.Name, "")
, prop.PropertyType), null);
}
else if (prop.PropertyType.FullName.Equals("System.Decimal"))
{
prop.SetValue(entity
, Convert.ChangeType(MyFuncLib.dtv(row, prop.Name, "")
, prop.PropertyType), null);
}
else if (prop.PropertyType.FullName.Equals("System.Double"))
{
prop.SetValue(entity
, Convert.ChangeType(MyFuncLib.dtv(row, prop.Name, "")
, prop.PropertyType), null);
}
else if (prop.PropertyType.FullName.Equals("System.Boolean"))
{
prop.SetValue(entity
, Convert.ChangeType(MyFuncLib.dtv(row, prop.Name, "false")
, prop.PropertyType), null);
}
else if (prop.PropertyType.FullName.Equals("System.DateTime"))
{
if (MyFuncLib.dtv(row, prop.Name, null) != null)
{
prop.SetValue(entity
, Convert.ChangeType(MyFuncLib.dtv(row, prop.Name, null)
, prop.PropertyType), null);
}
}
else
{
prop.SetValue(entity
, MyFuncLib.dtv(row, prop.Name, string.Empty), null);
}
}
return entity;
}

显示用户数据时,将数据保存在一个DataTable dt中

private void Form1_Load(object sender, EventArgs e)
{
// 初始化表时,读取数据
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "";
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand.CommandText = "select * from user where id = 1";
adapter.Fill(dt);
adapter = null;
conn.Close();
}

修改数据后,将变更存入dt的第一条记录newRow中。保存数据前从数据库中读取记录存入oldRow,然后比较oldRow和newRow差异,遇到差异时拼接Update SQL语句。

private void btnSave_Click(object sender, EventArgs e)
{
// 理论上只有一条记录值
if (dt.Rows.Count > )
{
// 模拟数据修改,直接修改dt.Rows[0]
#region update row
dt.Rows[]["UserName"] = "";
dt.Rows[]["NickName"] = "Tom";
dt.Rows[]["Password"] = "";
#endregion // 打开数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "";
conn.Open(); // 修改前读取数据库中的记录
DataTable dtTemp = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand.CommandText = "select * from user where id = 1";
adapter.Fill(dtTemp);
DataRow oldRow = dtTemp.Rows[];
adapter = null;
// 当前数据库中的值
User oldItem = MyFuncLib.InitEntity(oldRow, new User());
// 可能已经发生修改的值
User newItem = MyFuncLib.InitEntity(dt.Rows[], new User()); // 标识当前记录是否发生了修改
bool amended = false;
// Update Sql
StringBuilder sql = new StringBuilder();
sql.AppendLine("update user set modifiedDate = getDate()"); // 定义Update Command
SqlCommand comdUpdate = new SqlCommand();
// 遍历User类属性
System.Reflection.PropertyInfo[] props = typeof(User).GetProperties();
foreach (System.Reflection.PropertyInfo prop in props)
{
// 排除id等系统字段
if (!prop.Name.Equals("id"))
{
// 仅当值发生修改时才拼接SQL语句
if (!prop.GetValue(oldItem, null).Equals(prop.GetValue(newItem, null)))
{
// 拼接Update语句
sql.AppendLine(string.Format(",[{0}] = @{0}", prop.Name));
// 同时添加参数
comdUpdate.Parameters.AddWithValue(
string.Format("@{0}", prop.Name)
, prop.GetValue(newItem, null).ToString());
// 只要有一个字段值发生了变化,就设置amended = true
amended = true;
// 此处可插入日志代码,用于对当前表变更历史的记录
}
}
}
if (amended)
{
// 执行拼接的Update Sql
comdUpdate.CommandText = sql.ToString();
comdUpdate.Connection = conn;
comdUpdate.ExecuteNonQuery();
}
// 关闭SQL连接
conn.Close();
}
}

演示示例:下载

如何自动拼接 Update语句,仅Update已修改的字段的更多相关文章

  1. C#的自动拼接Sql语句Insert方法及思路

    思路: 1.想想插入语句,大概是这样的一个框架:INSERT INTO 表名 (数据库列名) values (值) 2.这里要3个变量是不固定的,分别是:表名.数据库列名.值: a.表名我们这里很容易 ...

  2. 带WHERE子句的UPDATE语句

    目前演示的几个UPDATE语句都是一次性更新所有行的数据,这无法满足只更新符合特定条件的行的需求,比如“将Tom 的年龄修改为12 岁”.要实现这样的功能只要使用WHERE 子句就可以了,在WHERE ...

  3. SQL基础语法—update语句

    1 update语句介绍 update语句用来修改表中的数据内容 Single-table syntax: UPDATE [LOW_PRIORITY] [IGNORE] table_reference ...

  4. MySQL update语句和insert插入语句写法完全不一样啊,不要搞混

    1.mysql update 语句: update user set name = 'xiaoming',age = 18 where uid = 3000; 更新记录时update操作也不需要写ta ...

  5. SQL Server UPDATE语句的用法详解

    SQL Server UPDATE语句用于更新数据,下面就为您详细介绍SQL Server UPDATE语句语法方面的知识,希望可以让您对SQL Server UPDATE语句有更多的了解. 现实应用 ...

  6. Oracle 增删改(INSERT、DELETE、UPDATE)语句

    Ø  简介 本文介绍 Oracle 中的增删改语句,即 INSERT.DELETE.UPDATE 语句的使用.是时候展现真正的技术了,快上车: 1.   插入数据(INSERT) 2.   修改数据( ...

  7. sql update语句

    如果要更新数据库表中的记录,我们就必须使用UPDATE语句. UPDATE语句的基本语法是: UPDATE <表名> SET 字段1=值1, 字段2=值2, ... WHERE ...; ...

  8. 独特的deadlock(仅update语句也能造成死锁)

    最近遇到了一个看上去很奇怪,分析起来很有意思的死锁问题.这个死锁看上去难以理解.而分析过程中,又使用了很多分析SQL Server死锁的典型方法.记录下来整个分析过程还是很有意义的. 问题重现步骤: ...

  9. 不恰当的update语句使用主键和索引导致mysql死锁

    背景知识: 截至目前,MySQL一共向用户提供了包括DBD.HEAP.ISAM.MERGE.MyIAS.InnoDB以及Gemeni这7种Mysql表类型.其中DBD.InnoDB属于事务安全类表,而 ...

随机推荐

  1. JBoss7快速入门

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  2. Postfix 电子邮件系统精要

    来源: http://sery.blog.51cto.com/10037/45500 Postfix 电子邮件系统精要 作者:田逸(sery@163.com)  from [url]http://ww ...

  3. iOS开发笔记系列-基础3(多态、动态类型和动态绑定)

    多态:相同的名称,不同的类 使不同的类共享相同方法名称的能力成为多态.它让你可以开发一组类,这组类中的每一个类都能响应相同的方法名.每个类的定义都封装了响应特定方法所需要的代码,这使得它独立于其他的类 ...

  4. Aizu 2456 Usoperanto 贪心 拓扑排序

    Usoperanto Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/contest_show.php?cid= ...

  5. windows环境下搭建ffmpeg开发环境

           ffmpeg是一个开源.跨平台的程序库,能够使用在windows.linux等平台下,本文将简单解说windows环境下ffmpeg开发环境搭建过程,本人使用的操作系统为windows ...

  6. 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)

    1.导入jar watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400 ...

  7. [Embed(source="asset.swf")] 使用其中的所有资源

    在AS3中,我们可以使用 [Embed(source="asset.swf", symbol="symbol")] private var symbolClas ...

  8. android131 360 04 手机安全页面

    ## Root权限 ## > 什么是Root权限? Root权限相当于系统管理员权限, 有了root权限,就可以随意修改和删除手机内部的文件. > 一般手机购买之后, 都没有root权限. ...

  9. mvc ajax_返回数据

    假设cshtml文件中是这样的: <script type="text/javascript"> $(document).ready(function(){ $(&qu ...

  10. Spring中Bean的生命中期与InitializingBean和DisposableBean接口

    Spring提供了一些标志接口,用来改变BeanFactory中的bean的行为.它们包括InitializingBean和DisposableBean.实现这些接口将会导致BeanFactory调用 ...