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 new SqlCommand DeleteCommand {
69 get { return deleteCommand; }
70 set { deleteCommand = value; }
73 public new SqlCommand InsertCommand {
74 get { return insertCommand; }
75 set { insertCommand = value; }
78 public new SqlCommand SelectCommand {
79 get { return selectCommand; }
80 set { selectCommand = value; }
83 public new SqlCommand UpdateCommand {
84 get { return updateCommand; }
85 set { updateCommand = value; }
88 IDbCommand IDbDataAdapter.DeleteCommand {
89 get { return DeleteCommand; }
91 if (!(value is SqlCommand))
92 throw new ArgumentException ();
93 DeleteCommand = (SqlCommand)value;
97 IDbCommand IDbDataAdapter.InsertCommand {
98 get { return InsertCommand; }
100 if (!(value is SqlCommand))
101 throw new ArgumentException ();
102 InsertCommand = (SqlCommand)value;
106 IDbCommand IDbDataAdapter.SelectCommand {
107 get { return SelectCommand; }
109 if (!(value is SqlCommand))
110 throw new ArgumentException ();
111 SelectCommand = (SqlCommand)value;
115 IDbCommand IDbDataAdapter.UpdateCommand {
116 get { return UpdateCommand; }
118 if (!(value is SqlCommand))
119 throw new ArgumentException ();
120 UpdateCommand = (SqlCommand)value;
125 ITableMappingCollection IDataAdapter.TableMappings {
126 get { return TableMappings; }
129 #endregion // Properties
133 protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
135 return new SqlRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
139 protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
141 return new SqlRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
144 protected override void OnRowUpdated (RowUpdatedEventArgs value)
146 SqlRowUpdatedEventHandler handler = (SqlRowUpdatedEventHandler) Events[EventRowUpdated];
147 if ((handler != null) && (value is SqlRowUpdatedEventArgs))
148 handler(this, (SqlRowUpdatedEventArgs) value);
151 protected override void OnRowUpdating (RowUpdatingEventArgs value)
153 SqlRowUpdatingEventHandler handler = (SqlRowUpdatingEventHandler) Events[EventRowUpdating];
154 if ((handler != null) && (value is SqlRowUpdatingEventArgs))
155 handler(this, (SqlRowUpdatingEventArgs) value);
158 #endregion // Methods
160 #region Events and Delegates
162 public event SqlRowUpdatedEventHandler RowUpdated {
163 add { Events.AddHandler (EventRowUpdated, value); }
164 remove { Events.RemoveHandler (EventRowUpdated, value); }
167 public event SqlRowUpdatingEventHandler RowUpdating {
168 add { Events.AddHandler (EventRowUpdating, value); }
169 remove { Events.RemoveHandler (EventRowUpdating, value); }
172 #endregion // Events and Delegates