AccessDataSource.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // System.Web.UI.WebControls.AccessDataSource.cs
  3. //
  4. // Authors:
  5. // Sanjay Gupta ([email protected])
  6. //
  7. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System.IO;
  30. using System.ComponentModel;
  31. using System.Data.Common;
  32. using System.Drawing;
  33. using System.Security.Permissions;
  34. namespace System.Web.UI.WebControls {
  35. // CAS
  36. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. // attributes
  39. [DesignerAttribute ("System.Web.UI.Design.WebControls.AccessDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. [ToolboxBitmap ("")]
  41. public class AccessDataSource : SqlDataSource {
  42. const string PROVIDER_NAME = "System.Data.OleDb";
  43. const string PROVIDER_STRING = "Microsoft.Jet.OLEDB.4.0";
  44. //string dataFile;
  45. string connectionString;
  46. public AccessDataSource () : base ()
  47. {
  48. base.ProviderName = PROVIDER_NAME;
  49. }
  50. public AccessDataSource (string dataFile, string selectCommand) :
  51. base (String.Empty, selectCommand)
  52. {
  53. //this.dataFile = dataFile;
  54. this.ProviderName = PROVIDER_NAME;
  55. }
  56. protected override SqlDataSourceView CreateDataSourceView (string viewName)
  57. {
  58. AccessDataSourceView view = new AccessDataSourceView (this, viewName, this.Context);
  59. if (IsTrackingViewState)
  60. ((IStateManager) view).TrackViewState ();
  61. return view;
  62. }
  63. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  64. [Browsable (false)]
  65. [MonoTODO("AccessDataSource does not support SQL Cache Dependencies")]
  66. public override string SqlCacheDependency {
  67. get { throw new NotSupportedException ("AccessDataSource does not supports SQL Cache Dependencies."); }
  68. set { throw new NotSupportedException ("AccessDataSource does not supports SQL Cache Dependencies."); }
  69. }
  70. [MonoTODO ("why override? maybe it doesn't call DbProviderFactories.GetFactory?")]
  71. protected override DbProviderFactory GetDbProviderFactory ()
  72. {
  73. return DbProviderFactories.GetFactory (PROVIDER_NAME);
  74. }
  75. string GetPhysicalDataFilePath ()
  76. {
  77. if (DataFile == null || DataFile == "")
  78. return "";
  79. // more here? how do we handle |DataDirectory|?
  80. return HttpContext.Current.Request.MapPath (DataFile);
  81. }
  82. [BrowsableAttribute (false),
  83. DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  84. public override string ConnectionString {
  85. get {
  86. if (connectionString == null) {
  87. connectionString = String.Format ("Provider={0}; Data Source={1}",
  88. PROVIDER_STRING, GetPhysicalDataFilePath ());
  89. }
  90. return connectionString;
  91. }
  92. set {
  93. throw new InvalidOperationException
  94. ("The ConnectionString is automatically generated for AccessDataSource and hence cannot be set.");
  95. }
  96. }
  97. [UrlPropertyAttribute]
  98. [DefaultValueAttribute ("")]
  99. [WebCategoryAttribute ("Data")]
  100. [WebSysDescriptionAttribute ("MS Office Access database file name")]
  101. [EditorAttribute ("System.Web.UI.Design.MdbDataFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  102. public string DataFile {
  103. get { return ViewState.GetString ("DataFile", ""); }
  104. set {
  105. ViewState ["DataFile"] = value;
  106. connectionString = null;
  107. }
  108. }
  109. [BrowsableAttribute (false),
  110. DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  111. public override string ProviderName {
  112. get { return base.ProviderName; }
  113. set { throw new InvalidOperationException
  114. ("Setting ProviderName on an AccessDataSource is not allowed");
  115. }
  116. }
  117. }
  118. }
  119. #endif