SqlDataSource.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // System.Web.UI.WebControls.SqlDataSource
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) 2004 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Text;
  35. namespace System.Web.UI.WebControls {
  36. public class SqlDataSource : DataSourceControl {
  37. public SqlDataSource ()
  38. {
  39. }
  40. public SqlDataSource (string connectionString, string selectCommand)
  41. {
  42. ConnectionString = connectionString;
  43. SelectCommand = selectCommand;
  44. }
  45. public SqlDataSource (string providerName, string connectionString, string selectCommand)
  46. {
  47. ProviderName = providerName;
  48. ConnectionString = connectionString;
  49. SelectCommand = selectCommand;
  50. }
  51. protected override DataSourceView GetView (string viewName)
  52. {
  53. if (viewName == "" || viewName == null)
  54. return View;
  55. else
  56. throw new ArgumentException ("viewName");
  57. }
  58. protected virtual SqlDataSourceView CreateDataSourceView (string viewName)
  59. {
  60. SqlDataSourceView view = new SqlDataSourceView (this, viewName, this.Context);
  61. view.DataSourceViewChanged += new EventHandler (ViewChanged);
  62. if (IsTrackingViewState)
  63. ((IStateManager) view).TrackViewState ();
  64. return view;
  65. }
  66. protected override ICollection GetViewNames ()
  67. {
  68. return new string [] { "DefaultView" };
  69. }
  70. public int Insert ()
  71. {
  72. return View.Insert (null);
  73. }
  74. public int Delete ()
  75. {
  76. return View.Delete (null, null);
  77. }
  78. public IEnumerable Select (DataSourceSelectArguments args)
  79. {
  80. return View.Select (args);
  81. }
  82. public int Update ()
  83. {
  84. return View.Update (null, null, null);
  85. }
  86. protected override void LoadViewState (object savedState)
  87. {
  88. Pair p = savedState as Pair;
  89. if (p != null) {
  90. base.LoadViewState (p.First);
  91. ((IStateManager) View).LoadViewState (p.Second);
  92. }
  93. }
  94. protected override object SaveViewState ()
  95. {
  96. object me = base.SaveViewState (), view = ((IStateManager) View).SaveViewState ();
  97. if (me != null || view != null)
  98. return new Pair (me, view);
  99. else
  100. return null;
  101. }
  102. protected override void TrackViewState ()
  103. {
  104. base.TrackViewState ();
  105. ((IStateManager) View).TrackViewState ();
  106. }
  107. //protected virtual DataSourceCache Cache { get; }
  108. //public virtual int CacheDuration { get; set; }
  109. //public virtual DataSourceCacheExpiry CacheExpirationPolicy { get; set; }
  110. //public virtual string CacheKeyDependency { get; set; }
  111. //public virtual string SqlCacheDependency { get; set; }
  112. //public virtual bool EnableCaching { get; set; }
  113. public virtual string ProviderName {
  114. get {
  115. string val = ViewState ["ProviderName"] as string;
  116. return val == null ? "System.Data.SqlClient" : val;
  117. }
  118. set { ViewState ["ProviderName"] = value; }
  119. }
  120. public virtual string ConnectionString {
  121. get {
  122. string val = ViewState ["ConnectionString"] as string;
  123. return val == null ? "" : val;
  124. }
  125. set { ViewState ["ConnectionString"] = value; }
  126. }
  127. public SqlDataSourceMode DataSourceMode {
  128. get {
  129. object val = ViewState ["DataSourceMode"];
  130. return val == null ? SqlDataSourceMode.DataSet : (SqlDataSourceMode) val;
  131. }
  132. set { ViewState ["DataSourceMode"] = value; }
  133. }
  134. public string DeleteCommand {
  135. get { return View.DeleteCommand; }
  136. set { View.DeleteCommand = value; }
  137. }
  138. public ParameterCollection DeleteParameters {
  139. get { return View.DeleteParameters; }
  140. }
  141. public ParameterCollection FilterParameters {
  142. get { return View.FilterParameters; }
  143. }
  144. public string InsertCommand {
  145. get { return View.InsertCommand; }
  146. set { View.InsertCommand = value; }
  147. }
  148. public ParameterCollection InsertParameters {
  149. get { return View.InsertParameters; }
  150. }
  151. public string SelectCommand {
  152. get { return View.SelectCommand; }
  153. set { View.SelectCommand = value; }
  154. }
  155. public ParameterCollection SelectParameters {
  156. get { return View.SelectParameters; }
  157. }
  158. public string UpdateCommand {
  159. get { return View.UpdateCommand; }
  160. set { View.UpdateCommand = value; }
  161. }
  162. public ParameterCollection UpdateParameters {
  163. get { return View.UpdateParameters; }
  164. }
  165. public string FilterExpression {
  166. get { return View.FilterExpression; }
  167. set { View.FilterExpression = value; }
  168. }
  169. public event SqlDataSourceStatusEventHandler Deleted {
  170. add { View.Deleted += value; }
  171. remove { View.Deleted -= value; }
  172. }
  173. public event SqlDataSourceCommandEventHandler Deleting {
  174. add { View.Deleting += value; }
  175. remove { View.Deleting -= value; }
  176. }
  177. public event SqlDataSourceStatusEventHandler Inserted {
  178. add { View.Inserted += value; }
  179. remove { View.Inserted -= value; }
  180. }
  181. public event SqlDataSourceCommandEventHandler Inserting {
  182. add { View.Inserting += value; }
  183. remove { View.Inserting -= value; }
  184. }
  185. public event SqlDataSourceStatusEventHandler Selected {
  186. add { View.Selected += value; }
  187. remove { View.Selected -= value; }
  188. }
  189. public event SqlDataSourceCommandEventHandler Selecting {
  190. add { View.Selecting += value; }
  191. remove { View.Selecting -= value; }
  192. }
  193. public event SqlDataSourceStatusEventHandler Updated {
  194. add { View.Updated += value; }
  195. remove { View.Updated -= value; }
  196. }
  197. public event SqlDataSourceCommandEventHandler Updating {
  198. add { View.Updating += value; }
  199. remove { View.Updating -= value; }
  200. }
  201. SqlDataSourceView view;
  202. SqlDataSourceView View {
  203. get {
  204. if (view == null) {
  205. view = new SqlDataSourceView (this, "DefaultView", this.Context);
  206. view.DataSourceViewChanged += new EventHandler (ViewChanged);
  207. if (IsTrackingViewState)
  208. ((IStateManager) view).TrackViewState ();
  209. }
  210. return view;
  211. }
  212. }
  213. void ViewChanged (object source, EventArgs e)
  214. {
  215. OnDataSourceChanged (e);
  216. }
  217. }
  218. }
  219. #endif