SqlDataAdapter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // System.Data.SqlClient.SqlDataAdapter.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) Ximian, Inc 2002
  10. // Copyright (C) 2002 Tim Coleman
  11. //
  12. using System;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Data.Common;
  16. namespace System.Data.SqlClient
  17. {
  18. /// <summary>
  19. /// Represents a set of command-related properties that are used
  20. /// to fill the DataSet and update a data source, all this
  21. /// from a SQL database.
  22. /// </summary>
  23. public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter
  24. {
  25. #region Fields
  26. SqlCommand deleteCommand;
  27. SqlCommand insertCommand;
  28. SqlCommand selectCommand;
  29. SqlCommand updateCommand;
  30. static readonly object EventRowUpdated = new object();
  31. static readonly object EventRowUpdating = new object();
  32. #endregion
  33. #region Constructors
  34. public SqlDataAdapter ()
  35. : this (new SqlCommand ())
  36. {
  37. }
  38. public SqlDataAdapter (SqlCommand selectCommand)
  39. {
  40. DeleteCommand = new SqlCommand ();
  41. InsertCommand = new SqlCommand ();
  42. SelectCommand = selectCommand;
  43. UpdateCommand = new SqlCommand ();
  44. }
  45. public SqlDataAdapter (string selectCommandText, SqlConnection selectConnection)
  46. : this (new SqlCommand (selectCommandText, selectConnection))
  47. {
  48. }
  49. public SqlDataAdapter (string selectCommandText, string selectConnectionString)
  50. : this (selectCommandText, new SqlConnection (selectConnectionString))
  51. {
  52. }
  53. #endregion
  54. #region Properties
  55. public SqlCommand DeleteCommand {
  56. get { return deleteCommand; }
  57. set { deleteCommand = value; }
  58. }
  59. public SqlCommand InsertCommand {
  60. get { return insertCommand; }
  61. set { insertCommand = value; }
  62. }
  63. public SqlCommand SelectCommand {
  64. get { return selectCommand; }
  65. set { selectCommand = value; }
  66. }
  67. public SqlCommand UpdateCommand {
  68. get { return updateCommand; }
  69. set { updateCommand = value; }
  70. }
  71. IDbCommand IDbDataAdapter.DeleteCommand {
  72. get { return DeleteCommand; }
  73. set {
  74. if (!(value is SqlCommand))
  75. throw new ArgumentException ();
  76. DeleteCommand = (SqlCommand)value;
  77. }
  78. }
  79. IDbCommand IDbDataAdapter.InsertCommand {
  80. get { return InsertCommand; }
  81. set {
  82. if (!(value is SqlCommand))
  83. throw new ArgumentException ();
  84. InsertCommand = (SqlCommand)value;
  85. }
  86. }
  87. IDbCommand IDbDataAdapter.SelectCommand {
  88. get { return SelectCommand; }
  89. set {
  90. if (!(value is SqlCommand))
  91. throw new ArgumentException ();
  92. SelectCommand = (SqlCommand)value;
  93. }
  94. }
  95. IDbCommand IDbDataAdapter.UpdateCommand {
  96. get { return UpdateCommand; }
  97. set {
  98. if (!(value is SqlCommand))
  99. throw new ArgumentException ();
  100. UpdateCommand = (SqlCommand)value;
  101. }
  102. }
  103. ITableMappingCollection IDataAdapter.TableMappings {
  104. get { return TableMappings; }
  105. }
  106. #endregion // Properties
  107. #region Methods
  108. protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  109. {
  110. return new SqlRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
  111. }
  112. protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  113. {
  114. return new SqlRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
  115. }
  116. protected override void OnRowUpdated (RowUpdatedEventArgs value)
  117. {
  118. SqlRowUpdatedEventHandler handler = (SqlRowUpdatedEventHandler) Events[EventRowUpdated];
  119. if ((handler != null) && (value is SqlRowUpdatedEventArgs))
  120. handler(this, (SqlRowUpdatedEventArgs) value);
  121. }
  122. protected override void OnRowUpdating (RowUpdatingEventArgs value)
  123. {
  124. SqlRowUpdatingEventHandler handler = (SqlRowUpdatingEventHandler) Events[EventRowUpdating];
  125. if ((handler != null) && (value is SqlRowUpdatingEventArgs))
  126. handler(this, (SqlRowUpdatingEventArgs) value);
  127. }
  128. #endregion // Methods
  129. #region Events and Delegates
  130. public event SqlRowUpdatedEventHandler RowUpdated {
  131. add { Events.AddHandler (EventRowUpdated, value); }
  132. remove { Events.RemoveHandler (EventRowUpdated, value); }
  133. }
  134. public event SqlRowUpdatingEventHandler RowUpdating {
  135. add { Events.AddHandler (EventRowUpdating, value); }
  136. remove { Events.RemoveHandler (EventRowUpdating, value); }
  137. }
  138. #endregion // Events and Delegates
  139. }
  140. }