SqlDataAdapter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SqlDataAdapter.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.SqlClient {
  9. using System;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Data.Common;
  13. using System.Diagnostics;
  14. [
  15. DefaultEvent("RowUpdated"),
  16. ToolboxItem("Microsoft.VSDesigner.Data.VS.SqlDataAdapterToolboxItem, " + AssemblyRef.MicrosoftVSDesigner),
  17. Designer("Microsoft.VSDesigner.Data.VS.SqlDataAdapterDesigner, " + AssemblyRef.MicrosoftVSDesigner)
  18. ]
  19. public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter, ICloneable {
  20. static private readonly object EventRowUpdated = new object();
  21. static private readonly object EventRowUpdating = new object();
  22. private SqlCommand _deleteCommand, _insertCommand, _selectCommand, _updateCommand;
  23. private SqlCommandSet _commandSet;
  24. private int _updateBatchSize = 1;
  25. public SqlDataAdapter() : base() {
  26. GC.SuppressFinalize(this);
  27. }
  28. public SqlDataAdapter(SqlCommand selectCommand) : this() {
  29. SelectCommand = selectCommand;
  30. }
  31. public SqlDataAdapter(string selectCommandText, string selectConnectionString) : this() {
  32. SqlConnection connection = new SqlConnection(selectConnectionString);
  33. SelectCommand = new SqlCommand(selectCommandText, connection);
  34. }
  35. public SqlDataAdapter(string selectCommandText, SqlConnection selectConnection) : this() {
  36. SelectCommand = new SqlCommand(selectCommandText, selectConnection);
  37. }
  38. private SqlDataAdapter(SqlDataAdapter from) : base(from) { // Clone
  39. GC.SuppressFinalize(this);
  40. }
  41. [
  42. DefaultValue(null),
  43. Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
  44. ResCategoryAttribute(Res.DataCategory_Update),
  45. ResDescriptionAttribute(Res.DbDataAdapter_DeleteCommand),
  46. ]
  47. new public SqlCommand DeleteCommand {
  48. get { return _deleteCommand; }
  49. set { _deleteCommand = value; }
  50. }
  51. IDbCommand IDbDataAdapter.DeleteCommand {
  52. get { return _deleteCommand; }
  53. set { _deleteCommand = (SqlCommand)value; }
  54. }
  55. [
  56. DefaultValue(null),
  57. Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
  58. ResCategoryAttribute(Res.DataCategory_Update),
  59. ResDescriptionAttribute(Res.DbDataAdapter_InsertCommand),
  60. ]
  61. new public SqlCommand InsertCommand {
  62. get { return _insertCommand; }
  63. set { _insertCommand = value; }
  64. }
  65. IDbCommand IDbDataAdapter.InsertCommand {
  66. get { return _insertCommand; }
  67. set { _insertCommand = (SqlCommand)value; }
  68. }
  69. [
  70. DefaultValue(null),
  71. Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
  72. ResCategoryAttribute(Res.DataCategory_Fill),
  73. ResDescriptionAttribute(Res.DbDataAdapter_SelectCommand),
  74. ]
  75. new public SqlCommand SelectCommand {
  76. get { return _selectCommand; }
  77. set { _selectCommand = value; }
  78. }
  79. IDbCommand IDbDataAdapter.SelectCommand {
  80. get { return _selectCommand; }
  81. set { _selectCommand = (SqlCommand)value; }
  82. }
  83. override public int UpdateBatchSize {
  84. get {
  85. return _updateBatchSize;
  86. }
  87. set {
  88. if (0 > value) { // WebData 98157
  89. throw ADP.ArgumentOutOfRange("UpdateBatchSize");
  90. }
  91. _updateBatchSize = value;
  92. Bid.Trace("<sc.SqlDataAdapter.set_UpdateBatchSize|API> %d#, %d\n", ObjectID, value);
  93. }
  94. }
  95. [
  96. DefaultValue(null),
  97. Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
  98. ResCategoryAttribute(Res.DataCategory_Update),
  99. ResDescriptionAttribute(Res.DbDataAdapter_UpdateCommand),
  100. ]
  101. new public SqlCommand UpdateCommand {
  102. get { return _updateCommand; }
  103. set { _updateCommand = value; }
  104. }
  105. IDbCommand IDbDataAdapter.UpdateCommand {
  106. get { return _updateCommand; }
  107. set { _updateCommand = (SqlCommand)value; }
  108. }
  109. [
  110. ResCategoryAttribute(Res.DataCategory_Update),
  111. ResDescriptionAttribute(Res.DbDataAdapter_RowUpdated),
  112. ]
  113. public event SqlRowUpdatedEventHandler RowUpdated {
  114. add {
  115. Events.AddHandler(EventRowUpdated, value);
  116. }
  117. remove {
  118. Events.RemoveHandler(EventRowUpdated, value);
  119. }
  120. }
  121. [
  122. ResCategoryAttribute(Res.DataCategory_Update),
  123. ResDescriptionAttribute(Res.DbDataAdapter_RowUpdating),
  124. ]
  125. public event SqlRowUpdatingEventHandler RowUpdating {
  126. add {
  127. SqlRowUpdatingEventHandler handler = (SqlRowUpdatingEventHandler) Events[EventRowUpdating];
  128. // MDAC 58177, 64513
  129. // prevent someone from registering two different command builders on the adapter by
  130. // silently removing the old one
  131. if ((null != handler) && (value.Target is DbCommandBuilder)) {
  132. SqlRowUpdatingEventHandler d = (SqlRowUpdatingEventHandler) ADP.FindBuilder(handler);
  133. if (null != d) {
  134. Events.RemoveHandler(EventRowUpdating, d);
  135. }
  136. }
  137. Events.AddHandler(EventRowUpdating, value);
  138. }
  139. remove {
  140. Events.RemoveHandler(EventRowUpdating, value);
  141. }
  142. }
  143. override protected int AddToBatch(IDbCommand command) {
  144. int commandIdentifier = _commandSet.CommandCount;
  145. _commandSet.Append((SqlCommand)command);
  146. return commandIdentifier;
  147. }
  148. override protected void ClearBatch() {
  149. _commandSet.Clear();
  150. }
  151. object ICloneable.Clone() {
  152. return new SqlDataAdapter(this);
  153. }
  154. override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) {
  155. return new SqlRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
  156. }
  157. override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) {
  158. return new SqlRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
  159. }
  160. override protected int ExecuteBatch() {
  161. Debug.Assert(null != _commandSet && (0 < _commandSet.CommandCount), "no commands");
  162. Bid.CorrelationTrace("<sc.SqlDataAdapter.ExecuteBatch|Info|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
  163. return _commandSet.ExecuteNonQuery();
  164. }
  165. override protected IDataParameter GetBatchedParameter(int commandIdentifier, int parameterIndex) {
  166. Debug.Assert(commandIdentifier < _commandSet.CommandCount, "commandIdentifier out of range");
  167. Debug.Assert(parameterIndex < _commandSet.GetParameterCount(commandIdentifier), "parameter out of range");
  168. IDataParameter parameter = _commandSet.GetParameter(commandIdentifier, parameterIndex);
  169. return parameter;
  170. }
  171. override protected bool GetBatchedRecordsAffected(int commandIdentifier, out int recordsAffected, out Exception error) {
  172. Debug.Assert(commandIdentifier < _commandSet.CommandCount, "commandIdentifier out of range");
  173. return _commandSet.GetBatchedAffected(commandIdentifier, out recordsAffected, out error);
  174. }
  175. override protected void InitializeBatching() {
  176. Bid.Trace("<sc.SqlDataAdapter.InitializeBatching|API> %d#\n", ObjectID);
  177. _commandSet = new SqlCommandSet();
  178. SqlCommand command = SelectCommand;
  179. if (null == command) {
  180. command = InsertCommand;
  181. if (null == command) {
  182. command = UpdateCommand;
  183. if (null == command) {
  184. command = DeleteCommand;
  185. }
  186. }
  187. }
  188. if (null != command) {
  189. _commandSet.Connection = command.Connection;
  190. _commandSet.Transaction = command.Transaction;
  191. _commandSet.CommandTimeout = command.CommandTimeout;
  192. }
  193. }
  194. override protected void OnRowUpdated(RowUpdatedEventArgs value) {
  195. SqlRowUpdatedEventHandler handler = (SqlRowUpdatedEventHandler) Events[EventRowUpdated];
  196. if ((null != handler) && (value is SqlRowUpdatedEventArgs)) {
  197. handler(this, (SqlRowUpdatedEventArgs) value);
  198. }
  199. base.OnRowUpdated(value);
  200. }
  201. override protected void OnRowUpdating(RowUpdatingEventArgs value) {
  202. SqlRowUpdatingEventHandler handler = (SqlRowUpdatingEventHandler) Events[EventRowUpdating];
  203. if ((null != handler) && (value is SqlRowUpdatingEventArgs)) {
  204. handler(this, (SqlRowUpdatingEventArgs) value);
  205. }
  206. base.OnRowUpdating(value);
  207. }
  208. override protected void TerminateBatching() {
  209. if (null != _commandSet) {
  210. _commandSet.Dispose();
  211. _commandSet = null;
  212. }
  213. }
  214. }
  215. }