SqlDataSource.cs 14 KB

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