SqlDataSource.cs 6.5 KB

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