2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlDataAdapter.cs
1 //
2 // System.Data.SqlClient.SqlDataAdapter.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //   Tim Coleman (tim@timcoleman.com)
8 //
9 // (C) Ximian, Inc 2002
10 // Copyright (C) 2002 Tim Coleman
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.ComponentModel;
38 using System.Data;
39 using System.Data.Common;
40
41 namespace System.Data.SqlClient {
42         [DefaultEvent ("RowUpdated")]
43         [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.SqlDataAdapterDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
44         [ToolboxItemAttribute ("Microsoft.VSDesigner.Data.VS.SqlDataAdapterToolboxItem, "+ Consts.AssemblyMicrosoft_VSDesigner)] 
45         public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter 
46         {
47                 #region Fields
48
49                 bool disposed = false;  
50                 SqlCommand deleteCommand;
51                 SqlCommand insertCommand;
52                 SqlCommand selectCommand;
53                 SqlCommand updateCommand;
54
55                 #endregion
56
57                 #region Constructors
58                 
59                 public SqlDataAdapter ()        
60                         : this (new SqlCommand ())
61                 {
62                 }
63
64                 public SqlDataAdapter (SqlCommand selectCommand) 
65                 {
66                         DeleteCommand = null;
67                         InsertCommand = null;
68                         SelectCommand = selectCommand;
69                         UpdateCommand = null;
70                 }
71
72                 public SqlDataAdapter (string selectCommandText, SqlConnection selectConnection) 
73                         : this (new SqlCommand (selectCommandText, selectConnection))
74                 { 
75                 }
76
77                 public SqlDataAdapter (string selectCommandText, string selectConnectionString)
78                         : this (selectCommandText, new SqlConnection (selectConnectionString))
79                 {
80                 }
81
82                 #endregion
83
84                 #region Properties
85
86                 [DataCategory ("Update")]
87                 [DataSysDescription ("Used during Update for deleted rows in DataSet.")]
88                 [DefaultValue (null)]
89                 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
90                 public SqlCommand DeleteCommand {
91                         get { return deleteCommand; }
92                         set { deleteCommand = value; }
93                 }
94
95                 [DataCategory ("Update")]
96                 [DataSysDescription ("Used during Update for new rows in DataSet.")]
97                 [DefaultValue (null)]
98                 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
99                 public SqlCommand InsertCommand {
100                         get { return insertCommand; }
101                         set { insertCommand = value; }
102                 }
103
104                 [DataCategory ("Fill")]
105                 [DataSysDescription ("Used during Fill/FillSchema.")]
106                 [DefaultValue (null)]
107                 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
108                 public SqlCommand SelectCommand {
109                         get { return selectCommand; }
110                         set { selectCommand = value; }
111                 }
112
113                 [DataCategory ("Update")]
114                 [DataSysDescription ("Used during Update for modified rows in DataSet.")]
115                 [DefaultValue (null)]
116                 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
117                 public SqlCommand UpdateCommand {
118                         get { return updateCommand; }
119                         set { updateCommand = value; }
120                 }
121
122                 IDbCommand IDbDataAdapter.DeleteCommand {
123                         get { return DeleteCommand; }
124                         set { 
125                                 if (!(value is SqlCommand)) 
126                                         throw new ArgumentException ();
127                                 DeleteCommand = (SqlCommand)value;
128                         }
129                 }
130
131                 IDbCommand IDbDataAdapter.InsertCommand {
132                         get { return InsertCommand; }
133                         set { 
134                                 if (!(value is SqlCommand)) 
135                                         throw new ArgumentException ();
136                                 InsertCommand = (SqlCommand)value;
137                         }
138                 }
139
140                 IDbCommand IDbDataAdapter.SelectCommand {
141                         get { return SelectCommand; }
142                         set { 
143                                 if (!(value is SqlCommand)) 
144                                         throw new ArgumentException ();
145                                 SelectCommand = (SqlCommand)value;
146                         }
147                 }
148
149                 IDbCommand IDbDataAdapter.UpdateCommand {
150                         get { return UpdateCommand; }
151                         set { 
152                                 if (!(value is SqlCommand)) 
153                                         throw new ArgumentException ();
154                                 UpdateCommand = (SqlCommand)value;
155                         }
156                 }
157
158
159                 ITableMappingCollection IDataAdapter.TableMappings {
160                         get { return TableMappings; }
161                 }
162
163                 #endregion // Properties
164
165                 #region Methods
166
167                 protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) 
168                 {
169                         return new SqlRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
170                 }
171
172
173                 protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) 
174                 {
175                         return new SqlRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
176                 }
177
178                 protected override void Dispose (bool disposing)
179                 {
180                         if (!disposed) {
181                                 if (disposing) {
182                                         // Release managed resources
183                                 }
184                                 // Release unmanaged resources
185                                 disposed = true;
186                         }
187                 }
188
189                 protected override void OnRowUpdated (RowUpdatedEventArgs value) 
190                 {
191                         if (RowUpdated != null)
192                                 RowUpdated (this, (SqlRowUpdatedEventArgs) value);
193                 }
194
195                 protected override void OnRowUpdating (RowUpdatingEventArgs value) 
196                 {
197                         if (RowUpdating != null)
198                                 RowUpdating (this, (SqlRowUpdatingEventArgs) value);
199                 }
200
201                 #endregion // Methods
202
203                 #region Events and Delegates
204
205                 [DataCategory ("Update")]
206                 [DataSysDescription ("Event triggered before every DataRow during Update.")]
207                 public event SqlRowUpdatedEventHandler RowUpdated;
208
209                 [DataCategory ("Update")]
210                 [DataSysDescription ("Event triggered after every DataRow during Update.")]
211                 public event SqlRowUpdatingEventHandler RowUpdating;
212
213                 #endregion // Events and Delegates
214
215         }
216 }