1. /// <summary>
  2. /// Performs an SQL update
  3. /// </summary>
  4. /// <param name="tableName">The name of the table to update</param>
  5. /// <param name="primaryKeyName">The name of the primary key column of the table</param>
  6. /// <param name="poco">The POCO object that specifies the column values to be updated</param>
  7. /// <param name="primaryKeyValue">The primary key of the record to be updated</param>
  8. /// <returns>The number of affected records</returns>
  9. public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue)
  10. {
  11. if (string.IsNullOrEmpty(tableName))
  12. throw new ArgumentNullException("tableName");
  13.  
  14. if (string.IsNullOrEmpty(primaryKeyName))
  15. throw new ArgumentNullException("primaryKeyName");
  16.  
  17. if (poco == null)
  18. throw new ArgumentNullException("poco");
  19.  
  20. return ExecuteUpdate(tableName, primaryKeyName, poco, primaryKeyValue, null);
  21. }
  22.  
  23. /// <summary>
  24. /// Performs an SQL update
  25. /// </summary>
  26. /// <param name="tableName">The name of the table to update</param>
  27. /// <param name="primaryKeyName">The name of the primary key column of the table</param>
  28. /// <param name="poco">The POCO object that specifies the column values to be updated</param>
  29. /// <param name="primaryKeyValue">The primary key of the record to be updated</param>
  30. /// <param name="columns">The column names of the columns to be updated, or null for all</param>
  31. /// <returns>The number of affected rows</returns>
  32. public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)
  33. {
  34. if (string.IsNullOrEmpty(tableName))
  35. throw new ArgumentNullException("tableName");
  36.  
  37. if (string.IsNullOrEmpty(primaryKeyName))
  38. throw new ArgumentNullException("primaryKeyName");
  39.  
  40. if (poco == null)
  41. throw new ArgumentNullException("poco");
  42.  
  43. return ExecuteUpdate(tableName, primaryKeyName, poco, primaryKeyValue, columns);
  44. }
  45.  
  46. /// <summary>
  47. /// Performs an SQL update
  48. /// </summary>
  49. /// <param name="tableName">The name of the table to update</param>
  50. /// <param name="primaryKeyName">The name of the primary key column of the table</param>
  51. /// <param name="poco">The POCO object that specifies the column values to be updated</param>
  52. /// <returns>The number of affected rows</returns>
  53. public int Update(string tableName, string primaryKeyName, object poco)
  54. {
  55. return Update(tableName, primaryKeyName, poco, null);
  56. }
  57.  
  58. /// <summary>
  59. /// Performs an SQL update
  60. /// </summary>
  61. /// <param name="tableName">The name of the table to update</param>
  62. /// <param name="primaryKeyName">The name of the primary key column of the table</param>
  63. /// <param name="poco">The POCO object that specifies the column values to be updated</param>
  64. /// <param name="columns">The column names of the columns to be updated, or null for all</param>
  65. /// <returns>The number of affected rows</returns>
  66. public int Update(string tableName, string primaryKeyName, object poco, IEnumerable<string> columns)
  67. {
  68. if (string.IsNullOrEmpty(tableName))
  69. throw new ArgumentNullException("tableName");
  70.  
  71. if (string.IsNullOrEmpty(primaryKeyName))
  72. throw new ArgumentNullException("primaryKeyName");
  73.  
  74. if (poco == null)
  75. throw new ArgumentNullException("poco");
  76.  
  77. return ExecuteUpdate(tableName, primaryKeyName, poco, null, columns);
  78. }
  79.  
  80. /// <summary>
  81. /// Performs an SQL update
  82. /// </summary>
  83. /// <param name="poco">The POCO object that specifies the column values to be updated</param>
  84. /// <param name="columns">The column names of the columns to be updated, or null for all</param>
  85. /// <returns>The number of affected rows</returns>
  86. public int Update(object poco, IEnumerable<string> columns)
  87. {
  88. return Update(poco, null, columns);
  89. }
  90.  
  91. /// <summary>
  92. /// Performs an SQL update
  93. /// </summary>
  94. /// <param name="poco">The POCO object that specifies the column values to be updated</param>
  95. /// <returns>The number of affected rows</returns>
  96. public int Update(object poco)
  97. {
  98. return Update(poco, null, null);
  99. }
  100.  
  101. /// <summary>
  102. /// Performs an SQL update
  103. /// </summary>
  104. /// <param name="poco">The POCO object that specifies the column values to be updated</param>
  105. /// <param name="primaryKeyValue">The primary key of the record to be updated</param>
  106. /// <returns>The number of affected rows</returns>
  107. public int Update(object poco, object primaryKeyValue)
  108. {
  109. return Update(poco, primaryKeyValue, null);
  110. }
  111.  
  112. /// <summary>
  113. /// Performs an SQL update
  114. /// </summary>
  115. /// <param name="poco">The POCO object that specifies the column values to be updated</param>
  116. /// <param name="primaryKeyValue">The primary key of the record to be updated</param>
  117. /// <param name="columns">The column names of the columns to be updated, or null for all</param>
  118. /// <returns>The number of affected rows</returns>
  119. public int Update(object poco, object primaryKeyValue, IEnumerable<string> columns)
  120. {
  121. if (poco == null)
  122. throw new ArgumentNullException("poco");
  123.  
  124. var pd = PocoData.ForType(poco.GetType(), _defaultMapper);
  125. return ExecuteUpdate(pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, poco, primaryKeyValue, columns);
  126. }
  127.  
  128. /// <summary>
  129. /// Performs an SQL update
  130. /// </summary>
  131. /// <typeparam name="T">The POCO class who's attributes specify the name of the table to update</typeparam>
  132. /// <param name="sql">The SQL update and condition clause (ie: everything after "UPDATE tablename"</param>
  133. /// <param name="args">Arguments to any embedded parameters in the SQL</param>
  134. /// <returns>The number of affected rows</returns>
  135. public int Update<T>(string sql, params object[] args)
  136. {
  137. if (string.IsNullOrEmpty(sql))
  138. throw new ArgumentNullException("sql");
  139.  
  140. var pd = PocoData.ForType(typeof(T), _defaultMapper);
  141. return Execute(string.Format("UPDATE {0} {1}", _provider.EscapeTableName(pd.TableInfo.TableName), sql), args);
  142. }
  143.  
  144. /// <summary>
  145. /// Performs an SQL update
  146. /// </summary>
  147. /// <typeparam name="T">The POCO class who's attributes specify the name of the table to update</typeparam>
  148. /// <param name="sql">
  149. /// An SQL builder object representing the SQL update and condition clause (ie: everything after "UPDATE
  150. /// tablename"
  151. /// </param>
  152. /// <returns>The number of affected rows</returns>
  153. public int Update<T>(Sql sql)
  154. {
  155. if (sql == null)
  156. throw new ArgumentNullException("sql");
  157.  
  158. var pd = PocoData.ForType(typeof(T), _defaultMapper);
  159. return Execute(new Sql(string.Format("UPDATE {0}", _provider.EscapeTableName(pd.TableInfo.TableName))).Append(sql));
  160. }
  161.  
  162. private int ExecuteUpdate(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)
  163. {
  164. try
  165. {
  166. OpenSharedConnection();
  167. try
  168. {
  169. using (var cmd = CreateCommand(_sharedConnection, ""))
  170. {
  171. var sb = new StringBuilder();
  172. var index = ;
  173. var pd = PocoData.ForObject(poco, primaryKeyName, _defaultMapper);
  174. if (columns == null)
  175. {
  176. foreach (var i in pd.Columns)
  177. {
  178. // Don't update the primary key, but grab the value if we don't have it
  179. if (string.Compare(i.Key, primaryKeyName, true) == )
  180. {
  181. if (primaryKeyValue == null)
  182. primaryKeyValue = i.Value.GetValue(poco);
  183. continue;
  184. }
  185.  
  186. // Dont update result only columns
  187. if (i.Value.ResultColumn)
  188. continue;
  189.  
  190. // Build the sql
  191. if (index > )
  192. sb.Append(", ");
  193. sb.AppendFormat("{0} = {1}{2}", _provider.EscapeSqlIdentifier(i.Key), _paramPrefix, index++);
  194.  
  195. // Store the parameter in the command
  196. AddParam(cmd, i.Value.GetValue(poco), i.Value.PropertyInfo);
  197. }
  198. }
  199. else
  200. {
  201. foreach (var colname in columns)
  202. {
  203. var pc = pd.Columns[colname];
  204.  
  205. // Build the sql
  206. if (index > )
  207. sb.Append(", ");
  208. sb.AppendFormat("{0} = {1}{2}", _provider.EscapeSqlIdentifier(colname), _paramPrefix, index++);
  209.  
  210. // Store the parameter in the command
  211. AddParam(cmd, pc.GetValue(poco), pc.PropertyInfo);
  212. }
  213.  
  214. // Grab primary key value
  215. if (primaryKeyValue == null)
  216. {
  217. var pc = pd.Columns[primaryKeyName];
  218. primaryKeyValue = pc.GetValue(poco);
  219. }
  220. }
  221.  
  222. // Find the property info for the primary key
  223. PropertyInfo pkpi = null;
  224. if (primaryKeyName != null)
  225. {
  226. PocoColumn col;
  227. pkpi = pd.Columns.TryGetValue(primaryKeyName, out col)
  228. ? col.PropertyInfo
  229. : new { Id = primaryKeyValue }.GetType().GetProperties()[];
  230. }
  231.  
  232. cmd.CommandText = string.Format("UPDATE {0} SET {1} WHERE {2} = {3}{4}",
  233. _provider.EscapeTableName(tableName), sb.ToString(), _provider.EscapeSqlIdentifier(primaryKeyName), _paramPrefix, index++);
  234. AddParam(cmd, primaryKeyValue, pkpi);
  235.  
  236. DoPreExecute(cmd);
  237.  
  238. // Do it
  239. var retv = cmd.ExecuteNonQuery();
  240. OnExecutedCommand(cmd);
  241. return retv;
  242. }
  243. }
  244. finally
  245. {
  246. CloseSharedConnection();
  247. }
  248. }
  249. catch (Exception x)
  250. {
  251. if (OnException(x))
  252. throw;
  253. return -;
  254. }
  255. }

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. 问题-关于SizeOf在Delphi7和Delphi2009下结果分别是16/32

    问题:同样的代码在Delphi7和Delphi2009下结果分别是16/32,为什么?var   LWindCode : array [0..15] of char; begin   showmess ...

  2. hdoj 2046 骨牌铺方格

    骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. 转载 asp.net的Request.ServerVariables参数说明

    转载原地址: http://blog.csdn.net/vincent_void/article/details/7739338 当讨论Request对象内容时,要研究的集合之一就是ServerVar ...

  4. 推荐《C Primer Plus(第五版)中文版》【worldsing笔记】

      老外写的C书,看了你会有一种哇塞的感觉,这里提供PDF扫描版的下在,包含数内的例程,请大家支持原版!! C Primer Plus(第五版)中文版.pdf  下载地址:http://pan.bai ...

  5. iOS多线程拾贝------操作巨人编程

    iOS多线程拾贝------操作巨人编程 多线程 基本 实现方案:pthread - NSThread - GCD - NSOperation Pthread 多平台,可移植 c语言,要程序员管理生命 ...

  6. Python Django manage.py提供的命令及用法

    λpython manage.pyType'manage.py help <subcommand>'for help on a specific subcommand. Available ...

  7. js-弹出一个新窗口 新窗口自动转接到一个页面然后自动关闭

    这个问题.好. 在百度问问找到的:他的做法是打开一个后通过实例对象,再进行一将跳转,最后再将JS里定时将实例对象关闭... 这个问题其实不需要两个页面,只要三行JS代码就能实现,除非你在2.php里面 ...

  8. ThinkPHP Volist标签

    Volist标签主要用于在模板中循环输出数据集或者多维数组. volist标签(循环输出数据) 闭合 非闭合标签 属性 name(必须):要输出的数据模板变量 id(必须):循环变量 offset(可 ...

  9. 让DataGridView显示行号

          http://www.cnblogs.com/JuneZhang/archive/2011/11/21/2257630.html 为了表示行号,我们可以在DataGridView的RowP ...

  10. Android中文乱码彻底解决

    以下是我研究的成果,希望对您有帮助: sb = new StringBuffer(); HttpEntity entity = response.getEntity(); InputStream is ...