/// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="tableName">The name of the table to update</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="primaryKeyValue">The primary key of the record to be updated</param>
/// <returns>The number of affected records</returns>
public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException("tableName"); if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException("primaryKeyName"); if (poco == null)
throw new ArgumentNullException("poco"); return ExecuteUpdate(tableName, primaryKeyName, poco, primaryKeyValue, null);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="tableName">The name of the table to update</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="primaryKeyValue">The primary key of the record to be updated</param>
/// <param name="columns">The column names of the columns to be updated, or null for all</param>
/// <returns>The number of affected rows</returns>
public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException("tableName"); if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException("primaryKeyName"); if (poco == null)
throw new ArgumentNullException("poco"); return ExecuteUpdate(tableName, primaryKeyName, poco, primaryKeyValue, columns);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="tableName">The name of the table to update</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <returns>The number of affected rows</returns>
public int Update(string tableName, string primaryKeyName, object poco)
{
return Update(tableName, primaryKeyName, poco, null);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="tableName">The name of the table to update</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="columns">The column names of the columns to be updated, or null for all</param>
/// <returns>The number of affected rows</returns>
public int Update(string tableName, string primaryKeyName, object poco, IEnumerable<string> columns)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException("tableName"); if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException("primaryKeyName"); if (poco == null)
throw new ArgumentNullException("poco"); return ExecuteUpdate(tableName, primaryKeyName, poco, null, columns);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="columns">The column names of the columns to be updated, or null for all</param>
/// <returns>The number of affected rows</returns>
public int Update(object poco, IEnumerable<string> columns)
{
return Update(poco, null, columns);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <returns>The number of affected rows</returns>
public int Update(object poco)
{
return Update(poco, null, null);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="primaryKeyValue">The primary key of the record to be updated</param>
/// <returns>The number of affected rows</returns>
public int Update(object poco, object primaryKeyValue)
{
return Update(poco, primaryKeyValue, null);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="primaryKeyValue">The primary key of the record to be updated</param>
/// <param name="columns">The column names of the columns to be updated, or null for all</param>
/// <returns>The number of affected rows</returns>
public int Update(object poco, object primaryKeyValue, IEnumerable<string> columns)
{
if (poco == null)
throw new ArgumentNullException("poco"); var pd = PocoData.ForType(poco.GetType(), _defaultMapper);
return ExecuteUpdate(pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, poco, primaryKeyValue, columns);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <typeparam name="T">The POCO class who's attributes specify the name of the table to update</typeparam>
/// <param name="sql">The SQL update and condition clause (ie: everything after "UPDATE tablename"</param>
/// <param name="args">Arguments to any embedded parameters in the SQL</param>
/// <returns>The number of affected rows</returns>
public int Update<T>(string sql, params object[] args)
{
if (string.IsNullOrEmpty(sql))
throw new ArgumentNullException("sql"); var pd = PocoData.ForType(typeof(T), _defaultMapper);
return Execute(string.Format("UPDATE {0} {1}", _provider.EscapeTableName(pd.TableInfo.TableName), sql), args);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <typeparam name="T">The POCO class who's attributes specify the name of the table to update</typeparam>
/// <param name="sql">
/// An SQL builder object representing the SQL update and condition clause (ie: everything after "UPDATE
/// tablename"
/// </param>
/// <returns>The number of affected rows</returns>
public int Update<T>(Sql sql)
{
if (sql == null)
throw new ArgumentNullException("sql"); var pd = PocoData.ForType(typeof(T), _defaultMapper);
return Execute(new Sql(string.Format("UPDATE {0}", _provider.EscapeTableName(pd.TableInfo.TableName))).Append(sql));
} private int ExecuteUpdate(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)
{
try
{
OpenSharedConnection();
try
{
using (var cmd = CreateCommand(_sharedConnection, ""))
{
var sb = new StringBuilder();
var index = ;
var pd = PocoData.ForObject(poco, primaryKeyName, _defaultMapper);
if (columns == null)
{
foreach (var i in pd.Columns)
{
// Don't update the primary key, but grab the value if we don't have it
if (string.Compare(i.Key, primaryKeyName, true) == )
{
if (primaryKeyValue == null)
primaryKeyValue = i.Value.GetValue(poco);
continue;
} // Dont update result only columns
if (i.Value.ResultColumn)
continue; // Build the sql
if (index > )
sb.Append(", ");
sb.AppendFormat("{0} = {1}{2}", _provider.EscapeSqlIdentifier(i.Key), _paramPrefix, index++); // Store the parameter in the command
AddParam(cmd, i.Value.GetValue(poco), i.Value.PropertyInfo);
}
}
else
{
foreach (var colname in columns)
{
var pc = pd.Columns[colname]; // Build the sql
if (index > )
sb.Append(", ");
sb.AppendFormat("{0} = {1}{2}", _provider.EscapeSqlIdentifier(colname), _paramPrefix, index++); // Store the parameter in the command
AddParam(cmd, pc.GetValue(poco), pc.PropertyInfo);
} // Grab primary key value
if (primaryKeyValue == null)
{
var pc = pd.Columns[primaryKeyName];
primaryKeyValue = pc.GetValue(poco);
}
} // Find the property info for the primary key
PropertyInfo pkpi = null;
if (primaryKeyName != null)
{
PocoColumn col;
pkpi = pd.Columns.TryGetValue(primaryKeyName, out col)
? col.PropertyInfo
: new { Id = primaryKeyValue }.GetType().GetProperties()[];
} cmd.CommandText = string.Format("UPDATE {0} SET {1} WHERE {2} = {3}{4}",
_provider.EscapeTableName(tableName), sb.ToString(), _provider.EscapeSqlIdentifier(primaryKeyName), _paramPrefix, index++);
AddParam(cmd, primaryKeyValue, pkpi); DoPreExecute(cmd); // Do it
var retv = cmd.ExecuteNonQuery();
OnExecutedCommand(cmd);
return retv;
}
}
finally
{
CloseSharedConnection();
}
}
catch (Exception x)
{
if (OnException(x))
throw;
return -;
}
}

PetaPoco更新记录方法的更多相关文章

  1. mysql防止重复插入记录方法总结

    mysql防止重复插入记录方法总结 防止mysql重复插入记录的方法有很多种,常用的是ignore,Replace,ON DUPLICATE KEY UPDATE,当然我们也可以在php中加以判断了. ...

  2. 如何更方便的查看Linux内核代码的更新记录【转】

    转自:http://blog.csdn.net/lee244868149/article/details/44302819 Linux内核的更新非常的快,如何快速的了解这些更新呢?最一般的办法就是把新 ...

  3. Git 删除所有历史提交记录方法

    Git 删除所有历史提交记录方法 切换分支 git checkout --orphan latest_branch 添加所有文件 git add -A 提交更改 git commit -am &quo ...

  4. MySql中4种批量更新的方法update table2,table1,批量更新用insert into ...on duplicate key update, 慎用replace into.

    mysql 批量更新记录 MySql中4种批量更新的方法最近在完成MySql项目集成的情况下,需要增加批量更新的功能,根据网上的资料整理了一下,很好用,都测试过,可以直接使用. mysql 批量更新共 ...

  5. MicroERP软件更新记录1.1

    MicroERP软件更新记录 最新版本:1.1 1.增加固定资产检修.租赁.转移记录 2.增加产品质检单 3.增加零售单(收银台) 4.支持各种主流关系型数据库 5.完善了数据字典,如加入原材料材质. ...

  6. 更新记录后关闭子窗口并刷新父窗口的Javascript

    有时我们需要在新打开的窗口里面编辑信息,等编辑完了,需要将当前窗口关闭并且刷新父窗口,以使修改生效,本文就是介绍用 javascript 来实现"更新记录后关闭子窗口并刷新父窗口" ...

  7. Atitit  记录方法调用参数上下文arguments

    Atitit  记录方法调用参数上下文arguments 1.1. java  java8  新的对象Parameter LocalVariableTable 本地变量表 MethodParamete ...

  8. Dynamics CRM2016 Web API之更新记录

    本篇继续探索web api,介绍如何通过web api更新记录. 下面是一段简单的更新代码,更新了几个不同类型的字段,entity的赋值和前篇创建时候的一样的. var entity = {}; en ...

  9. Dynamics CRM 通过Odata创建及更新记录各类型字段的赋值方式

    CRM中通过Odata方式去创建或者更新记录时,各种类型的字段的赋值方式各不相同,这里转载一篇博文很详细的列出了各类型字段赋值方式,以供后期如有遗忘再次查询使用. http://luoyong0201 ...

随机推荐

  1. RDMA编程实例

    1,RDMA verbs Multicast Code for Multicast Using RDMA_CM(Remote directory memory access_connect manag ...

  2. Android实例-闪光灯的控制(XE8+小米2)

    unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Var ...

  3. Google的IP地址一览表,加上代理服务器

    Bulgaria 93.123.23.1 93.123.23.2 93.123.23.3 93.123.23.4 93.123.23.5 93.123.23.6 93.123.23.7 93.123. ...

  4. hdoj 2151 Worm【动态规划】

    Worm Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. Qt QDebug :Cannot retrieve debugging output!

    调试Qt程序时用Qdebug类输出调试信息:     qDebug("read My Com"); 这个问题是个小问题,其实跟程序没关系.当你同时开多个Qt程序(Creator编程 ...

  6. Outlook账户迁移帮助

    Outlook账户迁移指南 1. 介绍 开贴聊聊如何迁移Outlook用户账户到另外一台电脑. 相信许多Outlook用户都遇到过这样的情况:买来一台新电脑,安装完Outlook后,想把旧电脑里面的O ...

  7. iOS containsString与rangeOfString

    rangeOfString是在 containsString没出来之前 用于查找字符串中是否包含某字符,iOS <8.0 NSString *str1 = @"can you \n s ...

  8. BZOJ [ZJOI2008]泡泡堂BNB 贪心

    [ZJOI2008]泡泡堂BNB Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/proble ...

  9. Web版RSS阅读器(三)——解析在线Rss订阅

    上篇博客<Web版RSS阅读器(二)——使用dTree树形加载rss订阅分组列表>已经写到读取rss订阅列表了,今天就说一下,当获取一条在线rss订阅的信息,怎么去解析它,从而获取文章或资 ...

  10. node.js在windows下的学习笔记(4)---同步,异步,回调的概念

    Node.js是使用事件驱动的,非阻塞的I/O模型,用于构建快速的,可扩展的网络应用程序. Node.js想解决的问题是:处理输入,输入,高并发 1.阻塞与非阻塞 阻塞也叫同步,是指每一次执行一个操作 ...