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.
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Configuration;
38 using System.Web.Configuration;
39 using System.Data.Common;
40 using System.Data.SqlClient;
42 using System.ComponentModel;
44 namespace System.Web.UI.WebControls {
46 [ParseChildrenAttribute (true)]
47 [PersistChildrenAttribute (false)]
48 [DefaultPropertyAttribute ("SelectQuery")]
49 [DesignerAttribute ("System.Web.UI.Design.WebControls.SqlDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
50 [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)
134 Page.LoadComplete += OnPageLoadComplete;
137 void OnPageLoadComplete (object sender, EventArgs e)
139 FilterParameters.UpdateValues (Context, this);
140 SelectParameters.UpdateValues (Context, this);
143 protected override void LoadViewState (object savedState)
145 Pair p = savedState as Pair;
147 base.LoadViewState (p.First);
148 ((IStateManager) View).LoadViewState (p.Second);
152 protected override object SaveViewState ()
154 object me = base.SaveViewState (), view = ((IStateManager) View).SaveViewState ();
155 if (me != null || view != null)
156 return new Pair (me, view);
161 protected override void TrackViewState ()
163 base.TrackViewState ();
165 ((IStateManager) view).TrackViewState ();
168 [DefaultValue (true)]
169 public virtual bool CancelSelectOnNullParameter {
170 get { return View.CancelSelectOnNullParameter; }
171 set { View.CancelSelectOnNullParameter = value; }
174 [DefaultValue (ConflictOptions.OverwriteChanges)]
175 public ConflictOptions ConflictDetection {
176 get { return View.ConflictDetection; }
177 set { View.ConflictDetection = value; }
180 [DefaultValue (SqlDataSourceCommandType.Text)]
181 public SqlDataSourceCommandType DeleteCommandType {
182 get { return View.DeleteCommandType; }
183 set { View.DeleteCommandType = value; }
186 [DefaultValue (SqlDataSourceCommandType.Text)]
187 public SqlDataSourceCommandType InsertCommandType {
188 get { return View.InsertCommandType; }
189 set { View.InsertCommandType = value; }
192 [DefaultValue (SqlDataSourceCommandType.Text)]
193 public SqlDataSourceCommandType SelectCommandType {
194 get { return View.SelectCommandType; }
195 set { View.SelectCommandType = value; }
198 [DefaultValue (SqlDataSourceCommandType.Text)]
199 public SqlDataSourceCommandType UpdateCommandType {
200 get { return View.UpdateCommandType; }
201 set { View.UpdateCommandType = value; }
204 [DefaultValue ("{0}")]
205 public string OldValuesParameterFormatString {
206 get { return View.OldValuesParameterFormatString; }
207 set { View.OldValuesParameterFormatString = value; }
211 public string SortParameterName
213 get { return View.SortParameterName; }
214 set { View.SortParameterName = value; }
217 [DefaultValueAttribute ("")]
218 public string FilterExpression
220 get { return View.FilterExpression; }
221 set { View.FilterExpression = value; }
224 // LAME SPEC: the event is raised on setting only when the old value is different
226 string providerName = String.Empty;
227 [DefaultValueAttribute ("")]
228 [TypeConverterAttribute ("System.Web.UI.Design.WebControls.DataProviderNameConverter, " + Consts.AssemblySystem_Design)]
229 public virtual string ProviderName {
230 get { return providerName; }
233 if (providerName != value) {
234 providerName = value;
235 RaiseDataSourceChangedEvent (EventArgs.Empty);
240 // LAME SPEC: the event is raised on setting only when the old value is different
242 string connectionString = String.Empty;
243 [MergableProperty (false)]
244 [EditorAttribute ("System.Web.UI.Design.WebControls.SqlDataSourceConnectionStringEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
245 [DefaultValueAttribute ("")]
246 public virtual string ConnectionString {
247 get { return connectionString; }
250 if (connectionString != value) {
251 connectionString = value;
252 RaiseDataSourceChangedEvent (EventArgs.Empty);
257 // LAME SPEC: the event is raised on setting only when the old value is different
259 // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
260 SqlDataSourceMode dataSourceMode = SqlDataSourceMode.DataSet;
261 [DefaultValueAttribute (SqlDataSourceMode.DataSet)]
262 public SqlDataSourceMode DataSourceMode {
263 get { return dataSourceMode; }
265 if (dataSourceMode != value) {
266 dataSourceMode = value;
267 RaiseDataSourceChangedEvent (EventArgs.Empty);
272 [DefaultValueAttribute ("")]
273 public string DeleteCommand {
274 get { return View.DeleteCommand; }
275 set { View.DeleteCommand = value; }
278 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
279 [MergablePropertyAttribute (false)]
280 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
281 [DefaultValueAttribute (null)]
282 public ParameterCollection DeleteParameters {
283 get { return View.DeleteParameters; }
286 [DefaultValueAttribute (null)]
287 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
288 [MergablePropertyAttribute (false)]
289 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
290 public ParameterCollection FilterParameters {
291 get { return View.FilterParameters; }
294 [DefaultValueAttribute ("")]
295 public string InsertCommand {
296 get { return View.InsertCommand; }
297 set { View.InsertCommand = value; }
300 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
301 [DefaultValueAttribute (null)]
302 [MergablePropertyAttribute (false)]
303 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
304 public ParameterCollection InsertParameters {
305 get { return View.InsertParameters; }
308 [DefaultValueAttribute ("")]
309 public string SelectCommand {
310 get { return View.SelectCommand; }
311 set { View.SelectCommand = value; }
314 [DefaultValueAttribute (null)]
315 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
316 [MergablePropertyAttribute (false)]
317 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
318 public ParameterCollection SelectParameters {
319 get { return View.SelectParameters; }
322 [DefaultValueAttribute ("")]
323 public string UpdateCommand {
324 get { return View.UpdateCommand; }
325 set { View.UpdateCommand = value; }
328 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
329 [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
330 [MergablePropertyAttribute (false)]
331 [DefaultValueAttribute (null)]
332 public ParameterCollection UpdateParameters {
333 get { return View.UpdateParameters; }
338 int cacheDuration = 0;
339 bool enableCaching = false;
340 string cacheKeyDependency = null;
341 string sqlCacheDependency = null;
342 DataSourceCacheManager cache = null;
343 DataSourceCacheExpiry cacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
345 internal DataSourceCacheManager Cache
350 cache = new DataSourceCacheManager (CacheDuration, CacheKeyDependency, CacheExpirationPolicy, this, Context);
356 public virtual string CacheKeyDependency
358 get { return cacheKeyDependency != null ? cacheKeyDependency : string.Empty; }
359 set { cacheKeyDependency = value; }
362 [MonoTODO ("SQLServer specific")]
364 public virtual string SqlCacheDependency {
365 get { return sqlCacheDependency != null ? sqlCacheDependency : string.Empty; }
366 set { sqlCacheDependency = value; }
369 [TypeConverter ("System.Web.UI.DataSourceCacheDurationConverter")]
371 public virtual int CacheDuration {
372 get { return cacheDuration; }
376 throw new ArgumentOutOfRangeException ("value", "The duration must be non-negative");
378 cacheDuration = value;
382 [DefaultValue (DataSourceCacheExpiry.Absolute)]
383 public virtual DataSourceCacheExpiry CacheExpirationPolicy {
384 get { return cacheExpirationPolicy; ; }
385 set { cacheExpirationPolicy = value; }
388 [DefaultValue (false)]
389 public virtual bool EnableCaching {
390 get { return enableCaching; }
393 if (DataSourceMode == SqlDataSourceMode.DataReader && value == true)
394 throw new NotSupportedException ();
395 enableCaching = value;
401 public event SqlDataSourceStatusEventHandler Deleted {
402 add { View.Deleted += value; }
403 remove { View.Deleted -= value; }
406 public event SqlDataSourceCommandEventHandler Deleting {
407 add { View.Deleting += value; }
408 remove { View.Deleting -= value; }
411 public event SqlDataSourceStatusEventHandler Inserted {
412 add { View.Inserted += value; }
413 remove { View.Inserted -= value; }
416 public event SqlDataSourceFilteringEventHandler Filtering {
417 add { View.Filtering += value; }
418 remove { View.Filtering -= value; }
421 public event SqlDataSourceCommandEventHandler Inserting {
422 add { View.Inserting += value; }
423 remove { View.Inserting -= value; }
426 public event SqlDataSourceStatusEventHandler Selected {
427 add { View.Selected += value; }
428 remove { View.Selected -= value; }
431 public event SqlDataSourceSelectingEventHandler Selecting {
432 add { View.Selecting += value; }
433 remove { View.Selecting -= value; }
436 public event SqlDataSourceStatusEventHandler Updated {
437 add { View.Updated += value; }
438 remove { View.Updated -= value; }
441 public event SqlDataSourceCommandEventHandler Updating {
442 add { View.Updating += value; }
443 remove { View.Updating -= value; }
446 SqlDataSourceView view;
447 SqlDataSourceView View {
450 view = CreateDataSourceView ("DefaultView");
451 if (IsTrackingViewState)
452 ((IStateManager) view).TrackViewState ();