SqlDataSource.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 override ICollection GetViewNames ()
  59. {
  60. return new string [] { "DefaultView" };
  61. }
  62. public int Insert ()
  63. {
  64. return View.Insert (null);
  65. }
  66. public int Delete ()
  67. {
  68. return View.Delete (null, null);
  69. }
  70. public IEnumerable Select (DataSourceSelectArguments args)
  71. {
  72. //View.Selecting;
  73. IEnumerable enums = View.Select (null);
  74. //View.Selected;
  75. return enums;
  76. }
  77. public int Update ()
  78. {
  79. return View.Update (null, null, null);
  80. }
  81. protected override void LoadViewState (object savedState)
  82. {
  83. Pair p = savedState as Pair;
  84. if (p != null) {
  85. base.LoadViewState (p.First);
  86. ((IStateManager) View).LoadViewState (p.Second);
  87. }
  88. }
  89. protected override object SaveViewState ()
  90. {
  91. object me = base.SaveViewState (), view = ((IStateManager) View).SaveViewState ();
  92. if (me != null || view != null)
  93. return new Pair (me, view);
  94. else
  95. return null;
  96. }
  97. protected override void TrackViewState ()
  98. {
  99. base.TrackViewState ();
  100. ((IStateManager) View).TrackViewState ();
  101. }
  102. //protected virtual DataSourceCache Cache { get; }
  103. //public virtual int CacheDuration { get; set; }
  104. //public virtual DataSourceCacheExpiry CacheExpirationPolicy { get; set; }
  105. //public virtual string CacheKeyDependency { get; set; }
  106. //public virtual string SqlCacheDependency { get; set; }
  107. //public virtual bool EnableCaching { get; set; }
  108. public virtual string ProviderName {
  109. get {
  110. string val = ViewState ["ProviderName"] as string;
  111. return val == null ? "System.Data.SqlClient" : val;
  112. }
  113. set { ViewState ["ProviderName"] = value; }
  114. }
  115. public virtual string ConnectionString {
  116. get {
  117. string val = ViewState ["ConnectionString"] as string;
  118. return val == null ? "" : val;
  119. }
  120. set { ViewState ["ConnectionString"] = value; }
  121. }
  122. public SqlDataSourceMode DataSourceMode {
  123. get {
  124. object val = ViewState ["DataSourceMode"];
  125. return val == null ? SqlDataSourceMode.DataSet : (SqlDataSourceMode) val;
  126. }
  127. set { ViewState ["DataSourceMode"] = value; }
  128. }
  129. public string DeleteCommand {
  130. get { return View.DeleteCommand; }
  131. set { View.DeleteCommand = value; }
  132. }
  133. public ParameterCollection DeleteParameters {
  134. get { return View.DeleteParameters; }
  135. }
  136. public ParameterCollection FilterParameters {
  137. get { return View.FilterParameters; }
  138. }
  139. public string InsertCommand {
  140. get { return View.InsertCommand; }
  141. set { View.InsertCommand = value; }
  142. }
  143. public ParameterCollection InsertParameters {
  144. get { return View.InsertParameters; }
  145. }
  146. public string SelectCommand {
  147. get { return View.SelectCommand; }
  148. set { View.SelectCommand = value; }
  149. }
  150. public ParameterCollection SelectParameters {
  151. get { return View.SelectParameters; }
  152. }
  153. public string UpdateCommand {
  154. get { return View.UpdateCommand; }
  155. set { View.UpdateCommand = value; }
  156. }
  157. public ParameterCollection UpdateParameters {
  158. get { return View.UpdateParameters; }
  159. }
  160. public string FilterExpression {
  161. get { return View.FilterExpression; }
  162. set { View.FilterExpression = value; }
  163. }
  164. public event SqlDataSourceStatusEventHandler Deleted {
  165. add { View.Deleted += value; }
  166. remove { View.Deleted -= value; }
  167. }
  168. public event SqlDataSourceCommandEventHandler Deleting {
  169. add { View.Deleting += value; }
  170. remove { View.Deleting -= value; }
  171. }
  172. public event SqlDataSourceStatusEventHandler Inserted {
  173. add { View.Inserted += value; }
  174. remove { View.Inserted -= value; }
  175. }
  176. public event SqlDataSourceCommandEventHandler Inserting {
  177. add { View.Inserting += value; }
  178. remove { View.Inserting -= value; }
  179. }
  180. public event SqlDataSourceStatusEventHandler Selected {
  181. add { View.Selected += value; }
  182. remove { View.Selected -= value; }
  183. }
  184. public event SqlDataSourceCommandEventHandler Selecting {
  185. add { View.Selecting += value; }
  186. remove { View.Selecting -= value; }
  187. }
  188. public event SqlDataSourceStatusEventHandler Updated {
  189. add { View.Updated += value; }
  190. remove { View.Updated -= value; }
  191. }
  192. public event SqlDataSourceCommandEventHandler Updating {
  193. add { View.Updating += value; }
  194. remove { View.Updating -= value; }
  195. }
  196. SqlDataSourceView view;
  197. SqlDataSourceView View {
  198. get {
  199. if (view == null) {
  200. view = new SqlDataSourceView (this, "DefaultView");
  201. view.DataSourceViewChanged += new EventHandler (ViewChanged);
  202. if (IsTrackingViewState)
  203. ((IStateManager) view).TrackViewState ();
  204. }
  205. return view;
  206. }
  207. }
  208. void ViewChanged (object source, EventArgs e)
  209. {
  210. OnDataSourceChanged (e);
  211. }
  212. }
  213. }
  214. #endif