File: AccessDataSource.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (264 lines) | stat: -rw-r--r-- 9,691 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//------------------------------------------------------------------------------
// <copyright file="AccessDataSource.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls {

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Common;
    using System.Data.OleDb;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Design;
    using System.IO;
    using System.Text;
    using System.Web.Caching;
    using System.Web.UI;


    /// <devdoc>
    /// Allows a user to create a declarative connection to an Access database in a .aspx page.
    /// </devdoc>
    [
    Designer("System.Web.UI.Design.WebControls.AccessDataSourceDesigner, " + AssemblyRef.SystemDesign),
    ToolboxBitmap(typeof(AccessDataSource)),
    WebSysDescription(SR.AccessDataSource_Description),
    WebSysDisplayName(SR.AccessDataSource_DisplayName)
    ]
    public class AccessDataSource : SqlDataSource {

        private const string OleDbProviderName = "System.Data.OleDb";
        private const string JetProvider = "Microsoft.Jet.OLEDB.4.0";
        private const string Access2007Provider = "Microsoft.ACE.OLEDB.12.0";
        private const string Access2007FileExtension = ".accdb";

        private FileDataSourceCache _cache;
        private string _connectionString;
        private string _dataFile;
        private string _physicalDataFile;



        /// <devdoc>
        /// Creates a new instance of AccessDataSource.
        /// </devdoc>
        public AccessDataSource() : base() {
        }


        /// <devdoc>
        /// Creates a new instance of AccessDataSource with a specified connection string and select command.
        /// </devdoc>
        public AccessDataSource(string dataFile, string selectCommand) : base() {
            if (String.IsNullOrEmpty(dataFile)) {
                throw new ArgumentNullException("dataFile");
            }
            DataFile = dataFile;
            SelectCommand = selectCommand;
        }

        /// <devdoc>
        /// Specifies the cache settings for this data source. For the cache to
        /// work, the DataSourceMode must be set to DataSet.
        /// </devdoc>
        internal override DataSourceCache Cache {
            get {
                if (_cache == null) {
                    _cache = new FileDataSourceCache();
                }
                return _cache;
            }
        }

        /// <devdoc>
        /// Gets the connection string for the AccessDataSource. This property is auto-generated and cannot be set.
        /// </devdoc>
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override string ConnectionString {
            get {
                if (_connectionString == null) {
                    _connectionString = CreateConnectionString();
                }
                return _connectionString;
            }
            set {
                throw new InvalidOperationException(SR.GetString(SR.AccessDataSource_CannotSetConnectionString));
            }
        }

        /// <devdoc>
        /// The name of an Access database file.
        /// This property is not stored in ViewState.
        /// </devdoc>
        [
        DefaultValue(""),
        Editor("System.Web.UI.Design.MdbDataFileEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        UrlProperty(),
        WebCategory("Data"),
        WebSysDescription(SR.AccessDataSource_DataFile),
        ]
        public string DataFile {
            get {
                return (_dataFile == null) ? String.Empty : _dataFile;
            }
            set {
                if (DataFile != value) {
                    _dataFile = value;
                    _connectionString = null;
                    _physicalDataFile = null;
                    RaiseDataSourceChangedEvent(EventArgs.Empty);
                }
            }
        }

        /// <devdoc>
        /// Gets the file data source cache object.
        /// </devdoc>
        private FileDataSourceCache FileDataSourceCache {
            get {
                FileDataSourceCache fileCache = Cache as FileDataSourceCache;
                Debug.Assert(fileCache != null, "Cache object should be a FileDataSourceCache");
                return fileCache;
            }
        }

        /// <devdoc>
        /// Gets the Physical path of the data file.
        /// </devdoc>
        private string PhysicalDataFile {
            get {
                if (_physicalDataFile == null) {
                    _physicalDataFile = GetPhysicalDataFilePath();
                }
                return _physicalDataFile;
            }
        }

        internal string NativeProvider {
            get {
                if (IsAccess2007) {
                    return Access2007Provider;
                }
                return JetProvider;
            }
        }

        internal virtual bool IsAccess2007 {
            get {
                // Access 2007 changed the file format so we have to pick the right provider based on extension.
                return Path.GetExtension(PhysicalDataFile) == Access2007FileExtension;
            }
        }

        /// <devdoc>
        /// Gets/sets the ADO.net managed provider name. This property is restricted to the OLE DB provider and cannot be set.
        /// </devdoc>
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override string ProviderName {
            get {
                return OleDbProviderName;
            }
            set {
                throw new InvalidOperationException(SR.GetString(SR.AccessDataSource_CannotSetProvider, ID));
            }
        }

        /// <devdoc>
        /// A semi-colon delimited string indicating which databases to use for the dependency in the format "database1:table1;database2:table2".
        /// </devdoc>
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override string SqlCacheDependency {
            get {
                throw new NotSupportedException(SR.GetString(SR.AccessDataSource_SqlCacheDependencyNotSupported, ID));
            }
            set {
                throw new NotSupportedException(SR.GetString(SR.AccessDataSource_SqlCacheDependencyNotSupported, ID));
            }
        }

        private void AddCacheFileDependency() {
            FileDataSourceCache.FileDependencies.Clear();
            string filename = PhysicalDataFile;
            if (filename.Length > 0) {
                FileDataSourceCache.FileDependencies.Add(filename);
            }
        }

        /// <devdoc>
        /// Creates a connection string for an Access database connection.
        /// The JET or ACE provider is used based on the DataFile extension, and the filename, username, password, and
        /// share mode are are set.
        /// </devdoc>
        private string CreateConnectionString() {
            return "Provider=" + NativeProvider + "; Data Source=" + PhysicalDataFile;
        }

        /// <devdoc>
        /// Creates a new AccessDataSourceView.
        /// </devdoc>
        protected override SqlDataSourceView CreateDataSourceView(string viewName) {
            return new AccessDataSourceView(this, viewName, Context);
        }

        protected override DbProviderFactory GetDbProviderFactory() {
            return OleDbFactory.Instance;
        }

        /// <devdoc>
        /// Gets the appropriate mapped path for the DataFile.
        /// </devdoc>
        private string GetPhysicalDataFilePath() {
            string filename = DataFile;
            if (filename.Length == 0) {
                return null;
            }
            if (!System.Web.Util.UrlPath.IsAbsolutePhysicalPath(filename)) {
                // Root relative path
                if (DesignMode) {
                    // This exception should never be thrown - the designer always maps paths
                    // before using the runtime control.
                    throw new NotSupportedException(SR.GetString(SR.AccessDataSource_DesignTimeRelativePathsNotSupported, ID));
                }
                filename = Context.Request.MapPath(filename, AppRelativeTemplateSourceDirectory, true);
            }

            HttpRuntime.CheckFilePermission(filename, true);

            // We also need to check for path discovery permissions for the
            // file since the page developer will be able to see the physical
            // path in the ConnectionString property.
            if (!HttpRuntime.HasPathDiscoveryPermission(filename)) {
                throw new HttpException(SR.GetString(SR.AccessDataSource_NoPathDiscoveryPermission, HttpRuntime.GetSafePath(filename), ID));
            }
            return filename;
        }

        /// <devdoc>
        /// Saves data to the cache.
        /// </devdoc>
        internal override void SaveDataToCache(int startRowIndex, int maximumRows, object data, CacheDependency dependency) {
            AddCacheFileDependency();
            base.SaveDataToCache(startRowIndex, maximumRows, data, dependency);
        }

        /*internal override void SaveTotalRowCountToCache(int totalRowCount) {
            AddCacheFileDependency();
            base.SaveTotalRowCountToCache(totalRowCount);
        }*/
    }
}