1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
//
// System.Web.UI.WebControls.SqlDataSource
//
// Authors:
// Ben Maurer (bmaurer@users.sourceforge.net)
// Sanjay Gupta (gsanjay@novell.com)
// Chris Toshok (toshok@ximian.com)
//
// (C) 2003 Ben Maurer
// (C) 2004-2006 Novell, Inc. (http://www.novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Drawing;
using System.Web.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using System.ComponentModel;
namespace System.Web.UI.WebControls {
[ParseChildrenAttribute (true)]
[PersistChildrenAttribute (false)]
[DefaultPropertyAttribute ("SelectQuery")]
[DesignerAttribute ("System.Web.UI.Design.WebControls.SqlDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
[DefaultEventAttribute ("Selecting")]
[ToolboxBitmap ("")]
public class SqlDataSource : DataSourceControl {
static readonly string [] emptyNames = new string [] { "DefaultView" };
public SqlDataSource ()
{
}
public SqlDataSource (string connectionString, string selectCommand)
{
ConnectionString = connectionString;
SelectCommand = selectCommand;
}
public SqlDataSource (string providerName, string connectionString, string selectCommand)
{
ProviderName = providerName;
ConnectionString = connectionString;
SelectCommand = selectCommand;
}
protected override DataSourceView GetView (string viewName)
{
if (String.IsNullOrEmpty (viewName) || (String.Compare (viewName, emptyNames [0], StringComparison.InvariantCultureIgnoreCase) == 0))
return View;
else
throw new ArgumentException ("viewName");
}
protected virtual SqlDataSourceView CreateDataSourceView (string viewName)
{
SqlDataSourceView view = new SqlDataSourceView (this, viewName, this.Context);
if (IsTrackingViewState)
((IStateManager) view).TrackViewState ();
return view;
}
protected virtual DbProviderFactory GetDbProviderFactory ()
{
DbProviderFactory f = null;
if (!String.IsNullOrEmpty (ProviderName)) {
f = DbProviderFactories.GetFactory(ProviderName);
return f;
}
return SqlClientFactory.Instance;
}
internal DbProviderFactory GetDbProviderFactoryInternal ()
{
return GetDbProviderFactory ();
}
protected override ICollection GetViewNames ()
{
return emptyNames;
}
public int Insert ()
{
return View.Insert (null);
}
public int Delete ()
{
return View.Delete (null, null);
}
public IEnumerable Select (DataSourceSelectArguments args)
{
return View.Select (args);
}
public int Update ()
{
return View.Update (null, null, null);
}
protected internal override void OnInit (EventArgs e)
{
base.OnInit (e);
Page.LoadComplete += OnPageLoadComplete;
}
void OnPageLoadComplete (object sender, EventArgs e)
{
FilterParameters.UpdateValues (Context, this);
SelectParameters.UpdateValues (Context, this);
}
protected override void LoadViewState (object savedState)
{
Pair p = savedState as Pair;
if (p != null) {
base.LoadViewState (p.First);
((IStateManager) View).LoadViewState (p.Second);
}
}
protected override object SaveViewState ()
{
object me = base.SaveViewState (), view = ((IStateManager) View).SaveViewState ();
if (me != null || view != null)
return new Pair (me, view);
else
return null;
}
protected override void TrackViewState ()
{
base.TrackViewState ();
if (view != null)
((IStateManager) view).TrackViewState ();
}
[DefaultValue (true)]
public virtual bool CancelSelectOnNullParameter {
get { return View.CancelSelectOnNullParameter; }
set { View.CancelSelectOnNullParameter = value; }
}
[DefaultValue (ConflictOptions.OverwriteChanges)]
public ConflictOptions ConflictDetection {
get { return View.ConflictDetection; }
set { View.ConflictDetection = value; }
}
[DefaultValue (SqlDataSourceCommandType.Text)]
public SqlDataSourceCommandType DeleteCommandType {
get { return View.DeleteCommandType; }
set { View.DeleteCommandType = value; }
}
[DefaultValue (SqlDataSourceCommandType.Text)]
public SqlDataSourceCommandType InsertCommandType {
get { return View.InsertCommandType; }
set { View.InsertCommandType = value; }
}
[DefaultValue (SqlDataSourceCommandType.Text)]
public SqlDataSourceCommandType SelectCommandType {
get { return View.SelectCommandType; }
set { View.SelectCommandType = value; }
}
[DefaultValue (SqlDataSourceCommandType.Text)]
public SqlDataSourceCommandType UpdateCommandType {
get { return View.UpdateCommandType; }
set { View.UpdateCommandType = value; }
}
[DefaultValue ("{0}")]
public string OldValuesParameterFormatString {
get { return View.OldValuesParameterFormatString; }
set { View.OldValuesParameterFormatString = value; }
}
[DefaultValue ("")]
public string SortParameterName
{
get { return View.SortParameterName; }
set { View.SortParameterName = value; }
}
[DefaultValueAttribute ("")]
public string FilterExpression
{
get { return View.FilterExpression; }
set { View.FilterExpression = value; }
}
// LAME SPEC: the event is raised on setting only when the old value is different
// from the new one
string providerName = String.Empty;
[DefaultValueAttribute ("")]
[TypeConverterAttribute ("System.Web.UI.Design.WebControls.DataProviderNameConverter, " + Consts.AssemblySystem_Design)]
public virtual string ProviderName {
get { return providerName; }
set
{
if (providerName != value) {
providerName = value;
RaiseDataSourceChangedEvent (EventArgs.Empty);
}
}
}
// LAME SPEC: the event is raised on setting only when the old value is different
// from the new one
string connectionString = String.Empty;
#if NET_4_0
[MergableProperty (false)]
#endif
[EditorAttribute ("System.Web.UI.Design.WebControls.SqlDataSourceConnectionStringEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
[DefaultValueAttribute ("")]
public virtual string ConnectionString {
get { return connectionString; }
set
{
if (connectionString != value) {
connectionString = value;
RaiseDataSourceChangedEvent (EventArgs.Empty);
}
}
}
// LAME SPEC: the event is raised on setting only when the old value is different
// from the new one
// LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
SqlDataSourceMode dataSourceMode = SqlDataSourceMode.DataSet;
[DefaultValueAttribute (SqlDataSourceMode.DataSet)]
public SqlDataSourceMode DataSourceMode {
get { return dataSourceMode; }
set {
if (dataSourceMode != value) {
dataSourceMode = value;
RaiseDataSourceChangedEvent (EventArgs.Empty);
}
}
}
[DefaultValueAttribute ("")]
public string DeleteCommand {
get { return View.DeleteCommand; }
set { View.DeleteCommand = value; }
}
[EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
[MergablePropertyAttribute (false)]
[PersistenceModeAttribute (PersistenceMode.InnerProperty)]
[DefaultValueAttribute (null)]
public ParameterCollection DeleteParameters {
get { return View.DeleteParameters; }
}
[DefaultValueAttribute (null)]
[EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
[MergablePropertyAttribute (false)]
[PersistenceModeAttribute (PersistenceMode.InnerProperty)]
public ParameterCollection FilterParameters {
get { return View.FilterParameters; }
}
[DefaultValueAttribute ("")]
public string InsertCommand {
get { return View.InsertCommand; }
set { View.InsertCommand = value; }
}
[PersistenceModeAttribute (PersistenceMode.InnerProperty)]
[DefaultValueAttribute (null)]
[MergablePropertyAttribute (false)]
[EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
public ParameterCollection InsertParameters {
get { return View.InsertParameters; }
}
[DefaultValueAttribute ("")]
public string SelectCommand {
get { return View.SelectCommand; }
set { View.SelectCommand = value; }
}
[DefaultValueAttribute (null)]
[PersistenceModeAttribute (PersistenceMode.InnerProperty)]
[MergablePropertyAttribute (false)]
[EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
public ParameterCollection SelectParameters {
get { return View.SelectParameters; }
}
[DefaultValueAttribute ("")]
public string UpdateCommand {
get { return View.UpdateCommand; }
set { View.UpdateCommand = value; }
}
[PersistenceModeAttribute (PersistenceMode.InnerProperty)]
[EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
[MergablePropertyAttribute (false)]
[DefaultValueAttribute (null)]
public ParameterCollection UpdateParameters {
get { return View.UpdateParameters; }
}
#region TODO
int cacheDuration = 0;
bool enableCaching = false;
string cacheKeyDependency = null;
string sqlCacheDependency = null;
DataSourceCacheManager cache = null;
DataSourceCacheExpiry cacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
internal DataSourceCacheManager Cache
{
get
{
if (cache == null)
cache = new DataSourceCacheManager (CacheDuration, CacheKeyDependency, CacheExpirationPolicy, this, Context);
return cache;
}
}
[DefaultValue ("")]
public virtual string CacheKeyDependency
{
get { return cacheKeyDependency != null ? cacheKeyDependency : string.Empty; }
set { cacheKeyDependency = value; }
}
[MonoTODO ("SQLServer specific")]
[DefaultValue ("")]
public virtual string SqlCacheDependency {
get { return sqlCacheDependency != null ? sqlCacheDependency : string.Empty; }
set { sqlCacheDependency = value; }
}
[TypeConverter ("System.Web.UI.DataSourceCacheDurationConverter")]
[DefaultValue (0)]
public virtual int CacheDuration {
get { return cacheDuration; }
set
{
if (value < 0)
throw new ArgumentOutOfRangeException ("value", "The duration must be non-negative");
cacheDuration = value;
}
}
[DefaultValue (DataSourceCacheExpiry.Absolute)]
public virtual DataSourceCacheExpiry CacheExpirationPolicy {
get { return cacheExpirationPolicy; ; }
set { cacheExpirationPolicy = value; }
}
[DefaultValue (false)]
public virtual bool EnableCaching {
get { return enableCaching; }
set
{
if (DataSourceMode == SqlDataSourceMode.DataReader && value == true)
throw new NotSupportedException ();
enableCaching = value;
}
}
#endregion
public event SqlDataSourceStatusEventHandler Deleted {
add { View.Deleted += value; }
remove { View.Deleted -= value; }
}
public event SqlDataSourceCommandEventHandler Deleting {
add { View.Deleting += value; }
remove { View.Deleting -= value; }
}
public event SqlDataSourceStatusEventHandler Inserted {
add { View.Inserted += value; }
remove { View.Inserted -= value; }
}
public event SqlDataSourceFilteringEventHandler Filtering {
add { View.Filtering += value; }
remove { View.Filtering -= value; }
}
public event SqlDataSourceCommandEventHandler Inserting {
add { View.Inserting += value; }
remove { View.Inserting -= value; }
}
public event SqlDataSourceStatusEventHandler Selected {
add { View.Selected += value; }
remove { View.Selected -= value; }
}
public event SqlDataSourceSelectingEventHandler Selecting {
add { View.Selecting += value; }
remove { View.Selecting -= value; }
}
public event SqlDataSourceStatusEventHandler Updated {
add { View.Updated += value; }
remove { View.Updated -= value; }
}
public event SqlDataSourceCommandEventHandler Updating {
add { View.Updating += value; }
remove { View.Updating -= value; }
}
SqlDataSourceView view;
SqlDataSourceView View {
get {
if (view == null) {
view = CreateDataSourceView ("DefaultView");
if (IsTrackingViewState)
((IStateManager) view).TrackViewState ();
}
return view;
}
}
}
}
#endif
|