2
0

SqlDataSource.cs 6.4 KB

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