2 // System.Data.SqlClient.SqlDataAdapter.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
9 // (C) Ximian, Inc 2002
10 // Copyright (C) 2002 Tim Coleman
14 using System.ComponentModel;
16 using System.Data.Common;
18 namespace System.Data.SqlClient
21 /// Represents a set of command-related properties that are used
22 /// to fill the DataSet and update a data source, all this
23 /// from a SQL database.
25 public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter
29 SqlCommand deleteCommand;
30 SqlCommand insertCommand;
31 SqlCommand selectCommand;
32 SqlCommand updateCommand;
34 static readonly object EventRowUpdated = new object();
35 static readonly object EventRowUpdating = new object();
41 public SqlDataAdapter ()
42 : this (new SqlCommand ())
46 public SqlDataAdapter (SqlCommand selectCommand)
48 DeleteCommand = new SqlCommand ();
49 InsertCommand = new SqlCommand ();
50 SelectCommand = selectCommand;
51 UpdateCommand = new SqlCommand ();
54 public SqlDataAdapter (string selectCommandText, SqlConnection selectConnection)
55 : this (new SqlCommand (selectCommandText, selectConnection))
59 public SqlDataAdapter (string selectCommandText, string selectConnectionString)
60 : this (selectCommandText, new SqlConnection (selectConnectionString))
68 public SqlCommand DeleteCommand {
73 deleteCommand = value;
77 public SqlCommand InsertCommand {
82 insertCommand = value;
86 public SqlCommand SelectCommand {
91 selectCommand = value;
95 public SqlCommand UpdateCommand {
100 updateCommand = value;
104 IDbCommand IDbDataAdapter.DeleteCommand {
105 get { return DeleteCommand; }
107 if (!(value is SqlCommand))
108 throw new ArgumentException ();
109 DeleteCommand = (SqlCommand)value;
113 IDbCommand IDbDataAdapter.InsertCommand {
114 get { return InsertCommand; }
116 if (!(value is SqlCommand))
117 throw new ArgumentException ();
118 InsertCommand = (SqlCommand)value;
122 IDbCommand IDbDataAdapter.SelectCommand {
123 get { return SelectCommand; }
125 if (!(value is SqlCommand))
126 throw new ArgumentException ();
127 SelectCommand = (SqlCommand)value;
131 IDbCommand IDbDataAdapter.UpdateCommand {
132 get { return UpdateCommand; }
134 if (!(value is SqlCommand))
135 throw new ArgumentException ();
136 UpdateCommand = (SqlCommand)value;
141 ITableMappingCollection IDataAdapter.TableMappings {
142 get { return TableMappings; }
145 #endregion // Properties
149 protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
151 return new SqlRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
155 protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
157 return new SqlRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
160 protected override void OnRowUpdated (RowUpdatedEventArgs value)
162 SqlRowUpdatedEventHandler handler = (SqlRowUpdatedEventHandler) Events[EventRowUpdated];
163 if ((handler != null) && (value is SqlRowUpdatedEventArgs))
164 handler(this, (SqlRowUpdatedEventArgs) value);
167 protected override void OnRowUpdating (RowUpdatingEventArgs value)
169 SqlRowUpdatingEventHandler handler = (SqlRowUpdatingEventHandler) Events[EventRowUpdating];
170 if ((handler != null) && (value is SqlRowUpdatingEventArgs))
171 handler(this, (SqlRowUpdatingEventArgs) value);
174 #endregion // Methods
176 #region Events and Delegates
178 public event SqlRowUpdatedEventHandler RowUpdated {
179 add { Events.AddHandler (EventRowUpdated, value); }
180 remove { Events.RemoveHandler (EventRowUpdated, value); }
183 public event SqlRowUpdatingEventHandler RowUpdating {
184 add { Events.AddHandler (EventRowUpdating, value); }
185 remove { Events.RemoveHandler (EventRowUpdating, value); }
188 #endregion // Events and Delegates