2 // System.Web.UI.WebControls.SqlDataSource
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Sanjay Gupta (gsanjay@novell.com)
7 // Chris Toshok (toshok@ximian.com)
10 // (C) 2004-2006 Novell, Inc. (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.Configuration;
39 using System.Web.Configuration;
40 using System.Data.Common;
41 using System.Data.SqlClient;
43 using System.ComponentModel;
45 namespace System.Web.UI.WebControls {
47 [ParseChildrenAttribute (true)]
48 [PersistChildrenAttribute (false)]
49 [DefaultPropertyAttribute ("SelectQuery")]
50 [DesignerAttribute ("System.Web.UI.Design.WebControls.SqlDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
51 [DefaultEventAttribute ("Selecting")]
52 public class SqlDataSource : DataSourceControl {
54 static readonly string [] emptyNames = new string [] { "DefaultView" };
56 public SqlDataSource ()
60 public SqlDataSource (string connectionString, string selectCommand)
62 ConnectionString = connectionString;
63 SelectCommand = selectCommand;
66 public SqlDataSource (string providerName, string connectionString, string selectCommand)
68 ProviderName = providerName;
69 ConnectionString = connectionString;
70 SelectCommand = selectCommand;
73 protected override DataSourceView GetView (string viewName)
75 if (String.IsNullOrEmpty (viewName) || (String.Compare (viewName, emptyNames [0], StringComparison.InvariantCultureIgnoreCase) == 0))
78 throw new ArgumentException ("viewName");
81 protected virtual SqlDataSourceView CreateDataSourceView (string viewName)
83 SqlDataSourceView view = new SqlDataSourceView (this, viewName, this.Context);
84 if (IsTrackingViewState)
85 ((IStateManager) view).TrackViewState ();
89 protected virtual DbProviderFactory GetDbProviderFactory ()
91 DbProviderFactory f = null;
93 if (!String.IsNullOrEmpty (ProviderName)) {
94 f = DbProviderFactories.GetFactory(ProviderName);
98 return SqlClientFactory.Instance;
101 internal DbProviderFactory GetDbProviderFactoryInternal ()
103 return GetDbProviderFactory ();
106 protected override ICollection GetViewNames ()
113 return View.Insert (null);
118 return View.Delete (null, null);
121 public IEnumerable Select (DataSourceSelectArguments args)
123 return View.Select (args);
128 return View.Update (null, null, null);
131 protected internal override void OnInit (EventArgs e)
133 Page.LoadComplete += OnPageLoadComplete;
136 void OnPageLoadComplete (object sender, EventArgs e)
138 FilterParameters.UpdateValues (Context, this);
139 SelectParameters.UpdateValues (Context, this);
142 protected override void LoadViewState (object savedState)
144 Pair p = savedState as Pair;
146 base.LoadViewState (p.First);
147 ((IStateManager) View).LoadViewState (p.Second);
151 protected override object SaveViewState ()
153 object me = base.SaveViewState (), view = ((IStateManager) View).SaveViewState ();
154 if (me != null || view != null)
155 return new Pair (me, view);
160 protected override void TrackViewState ()
162 base.TrackViewState ();
164 ((IStateManager) view).TrackViewState ();
167 [DefaultValue (true)]
168 public virtual bool CancelSelectOnNullParameter {
169 get { return View.CancelSelectOnNullParameter; }
170 set { View.CancelSelectOnNullParameter = value; }
173 [DefaultValue (ConflictOptions.OverwriteChanges)]
174 public ConflictOptions ConflictDetection {
175 get { return View.ConflictDetection; }
176 set { View.ConflictDetection = value; }
179 [DefaultValue (SqlDataSourceCommandType.Text)]
180 public SqlDataSourceCommandType DeleteCommandType {
181 get { return View.DeleteCommandType; }
182 set { View.DeleteCommandType = value; }
185 [DefaultValue (SqlDataSourceCommandType.Text)]
186 public SqlDataSourceCommandType InsertCommandType {
187 get { return View.InsertCommandType; }
188 set { View.InsertCommandType = value; }
191 [DefaultValue (SqlDataSourceCommandType.Text)]
192 public SqlDataSourceCommandType SelectCommandType {
193 get { return View.SelectCommandType; }
194 set { View.SelectCommandType = value; }
197 [DefaultValue (SqlDataSourceCommandType.Text)]
198 public SqlDataSourceCommandType UpdateCommandType {
199 get { return View.UpdateCommandType; }
200 set { View.UpdateCommandType = value; }
203 [DefaultValue ("{0}")]
204 public string OldValuesParameterFormatString {
205 get { return View.OldValuesParameterFormatString; }
206 set { View.OldValuesParameterFormatString = value; }
210 public string SortParameterName
212 get { return View.SortParameterName; }
213 set { View.SortParameterName = value; }
216 [DefaultValueAttribute ("")]
217 public string FilterExpression
219 get { return View.FilterExpression; }
220 set { View.FilterExpression = value; }
223 private string providerName = "";
224 [DefaultValueAttribute ("")]
225 [TypeConverterAttribute ("System.Web.UI.Design.WebControls.DataProviderNameConverter, " + Consts.AssemblySystem_Design)]
226 public virtual string ProviderName {
227 get { return providerName; }
230 providerName = value;
231 RaiseDataSourceChangedEvent (EventArgs.Empty);
235 private string connectionString = "";
236 [EditorAttribute ("System.Web.UI.Design.WebControls.SqlDataSourceConnectionStringEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
237 [DefaultValueAttribute ("")]
238 public virtual string ConnectionString {
239 get { return connectionString; }
243 connectionString = value;
244 RaiseDataSourceChangedEvent (EventArgs.Empty);
248 // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
249 private SqlDataSourceMode dataSourceMode = SqlDataSourceMode.DataSet;
250 [DefaultValueAttribute (SqlDataSourceMode.DataSet)]
251 public SqlDataSourceMode DataSourceMode {
252 get { return dataSourceMode; }
255 dataSourceMode = value;
256 RaiseDataSourceChangedEvent (EventArgs.Empty);
260 [DefaultValueAttribute ("")]
261 public string DeleteCommand {
262 get { return View.DeleteCommand; }
263 set { View.DeleteCommand = value; }
266 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
267 [MergablePropertyAttribute (false)]
268 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
269 [DefaultValueAttribute (null)]
270 public ParameterCollection DeleteParameters {
271 get { return View.DeleteParameters; }
274 [DefaultValueAttribute (null)]
275 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
276 [MergablePropertyAttribute (false)]
277 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
278 public ParameterCollection FilterParameters {
279 get { return View.FilterParameters; }
282 [DefaultValueAttribute ("")]
283 public string InsertCommand {
284 get { return View.InsertCommand; }
285 set { View.InsertCommand = value; }
288 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
289 [DefaultValueAttribute (null)]
290 [MergablePropertyAttribute (false)]
291 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
292 public ParameterCollection InsertParameters {
293 get { return View.InsertParameters; }
296 [DefaultValueAttribute ("")]
297 public string SelectCommand {
298 get { return View.SelectCommand; }
299 set { View.SelectCommand = value; }
302 [DefaultValueAttribute (null)]
303 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
304 [MergablePropertyAttribute (false)]
305 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
306 public ParameterCollection SelectParameters {
307 get { return View.SelectParameters; }
310 [DefaultValueAttribute ("")]
311 public string UpdateCommand {
312 get { return View.UpdateCommand; }
313 set { View.UpdateCommand = value; }
316 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
317 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
318 [MergablePropertyAttribute (false)]
319 [DefaultValueAttribute (null)]
320 public ParameterCollection UpdateParameters {
321 get { return View.UpdateParameters; }
326 int cacheDuration = 0;
327 bool enableCaching = false;
328 string cacheKeyDependency = null;
329 string sqlCacheDependency = null;
330 DataSourceCacheManager cache = null;
331 DataSourceCacheExpiry cacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
333 internal DataSourceCacheManager Cache
338 cache = new DataSourceCacheManager (CacheDuration, CacheKeyDependency, CacheExpirationPolicy, this, Context);
344 public virtual string CacheKeyDependency
346 get { return cacheKeyDependency != null ? cacheKeyDependency : string.Empty; }
347 set { cacheKeyDependency = value; }
350 [MonoTODO ("SQLServer specific")]
352 public virtual string SqlCacheDependency {
353 get { return sqlCacheDependency != null ? sqlCacheDependency : string.Empty; }
354 set { sqlCacheDependency = value; }
358 public virtual int CacheDuration {
359 get { return cacheDuration; }
363 throw new ArgumentOutOfRangeException ("value", "The duration must be non-negative");
365 cacheDuration = value;
369 [DefaultValue (DataSourceCacheExpiry.Absolute)]
370 public virtual DataSourceCacheExpiry CacheExpirationPolicy {
371 get { return cacheExpirationPolicy; ; }
372 set { cacheExpirationPolicy = value; }
375 [DefaultValue (false)]
376 public virtual bool EnableCaching {
377 get { return enableCaching; }
380 if (DataSourceMode == SqlDataSourceMode.DataReader && value == true)
381 throw new NotSupportedException ();
382 enableCaching = value;
388 public event SqlDataSourceStatusEventHandler Deleted {
389 add { View.Deleted += value; }
390 remove { View.Deleted -= value; }
393 public event SqlDataSourceCommandEventHandler Deleting {
394 add { View.Deleting += value; }
395 remove { View.Deleting -= value; }
398 public event SqlDataSourceStatusEventHandler Inserted {
399 add { View.Inserted += value; }
400 remove { View.Inserted -= value; }
403 public event SqlDataSourceFilteringEventHandler Filtering {
404 add { View.Filtering += value; }
405 remove { View.Filtering -= value; }
408 public event SqlDataSourceCommandEventHandler Inserting {
409 add { View.Inserting += value; }
410 remove { View.Inserting -= value; }
413 public event SqlDataSourceStatusEventHandler Selected {
414 add { View.Selected += value; }
415 remove { View.Selected -= value; }
418 public event SqlDataSourceSelectingEventHandler Selecting {
419 add { View.Selecting += value; }
420 remove { View.Selecting -= value; }
423 public event SqlDataSourceStatusEventHandler Updated {
424 add { View.Updated += value; }
425 remove { View.Updated -= value; }
428 public event SqlDataSourceCommandEventHandler Updating {
429 add { View.Updating += value; }
430 remove { View.Updating -= value; }
433 SqlDataSourceView view;
434 SqlDataSourceView View {
437 view = CreateDataSourceView ("DefaultView");
438 if (IsTrackingViewState)
439 ((IStateManager) view).TrackViewState ();