0
|
1 using System;
|
|
2 using System.Collections;
|
|
3 using System.Collections.Generic;
|
|
4 using System.Configuration;
|
|
5 using System.Data;
|
|
6 using System.Data.SqlClient;
|
|
7 using System.Diagnostics;
|
|
8 using System.Linq;
|
|
9
|
|
10 namespace BLToolkit.Data
|
|
11 {
|
|
12 using Configuration;
|
|
13 using DataProvider;
|
|
14 using Properties;
|
|
15
|
|
16 public partial class DbManager
|
|
17 {
|
|
18 #region Constructors
|
|
19
|
|
20 /// <summary>
|
|
21 /// Initializes a new instance of the <see cref="DbManager"/> class
|
|
22 /// and opens a database connection.
|
|
23 /// </summary>
|
|
24 /// <remarks>
|
|
25 /// <para>
|
|
26 /// This constructor uses a configuration, which has been used first in your application.
|
|
27 /// If there has been no connection used before, an empty string is applied as a default configuration.
|
|
28 /// </para>
|
|
29 /// <para>
|
|
30 /// See the <see cref="ConfigurationString"/> property
|
|
31 /// for an explanation and use of the default configuration.
|
|
32 /// </para>
|
|
33 /// </remarks>
|
|
34 /// <include file="Examples.xml" path='examples/db[@name="ctor"]/*' />
|
|
35 /// <seealso cref="AddConnectionString(string)"/>
|
|
36 /// <returns>An instance of the database manager class.</returns>
|
|
37 [DebuggerStepThrough]
|
|
38 public DbManager() : this(DefaultConfiguration)
|
|
39 {
|
|
40 }
|
|
41
|
|
42 /// <summary>
|
|
43 /// Initializes a new instance of the <see cref="DbManager"/> class
|
|
44 /// and opens a database connection for the provided configuration.
|
|
45 /// </summary>
|
|
46 /// <remarks>
|
|
47 /// See the <see cref="ConfigurationString"/> property
|
|
48 /// for an explanation and use of the configuration string.
|
|
49 /// </remarks>
|
|
50 /// <include file="Examples.xml" path='examples/db[@name="ctor(string)"]/*' />
|
|
51 /// <param name="configurationString">Configuration string.</param>
|
|
52 /// <returns>An instance of the <see cref="DbManager"/> class.</returns>
|
|
53 [DebuggerStepThrough]
|
|
54 public DbManager(string configurationString)
|
|
55 : this(
|
|
56 GetDataProvider (configurationString),
|
|
57 GetConnectionString(configurationString))
|
|
58 {
|
|
59 _configurationString = configurationString;
|
|
60 }
|
|
61
|
|
62 /// <summary>
|
|
63 /// Initializes a new instance of the <see cref="DbManager"/> class
|
|
64 /// and opens a database connection for the provided configuration.
|
|
65 /// </summary>
|
|
66 /// <remarks>
|
|
67 /// See the <see cref="ConfigurationString"/> property
|
|
68 /// for an explanation and use of the configuration string.
|
|
69 /// </remarks>
|
|
70 /// <param name="configuration">Configuration string not containing provider name.</param>
|
|
71 /// <param name="providerName">Provider configuration name.</param>
|
|
72 /// <returns>An instance of the <see cref="DbManager"/> class.</returns>
|
|
73 [DebuggerStepThrough]
|
|
74 public DbManager(string providerName, string configuration)
|
|
75 : this(
|
|
76 GetDataProvider (providerName + ProviderNameDivider + configuration),
|
|
77 GetConnectionString(providerName + ProviderNameDivider + configuration))
|
|
78 {
|
|
79 _configurationString = providerName + ProviderNameDivider + configuration;
|
|
80 }
|
|
81
|
|
82 /// <summary>
|
|
83 /// Initializes a new instance of the <see cref="DbManager"/> class for the provided connection.
|
|
84 /// </summary>
|
|
85 /// <remarks>
|
|
86 /// This constructor tries to open the connection if the connection state equals
|
|
87 /// <see cref="System.Data.ConnectionState">ConnectionState.Closed</see>.
|
|
88 /// In this case the <see cref="IDbConnection.ConnectionString"/> property of the connection
|
|
89 /// must be set before colling the constructor.
|
|
90 /// Otherwise, it neither opens nor closes the connection.
|
|
91 /// </remarks>
|
|
92 /// <exception cref="DataException">
|
|
93 /// Type of the connection could not be recognized.
|
|
94 /// </exception>
|
|
95 /// <include file="Examples.xml" path='examples/db[@name="ctor(IDbConnection)"]/*' />
|
|
96 /// <param name="connection">An instance of the <see cref="IDbConnection"/> class.</param>
|
|
97 /// <returns>An instance of the <see cref="DbManager"/> class.</returns>
|
|
98 [DebuggerStepThrough]
|
|
99 public DbManager(IDbConnection connection)
|
|
100 : this(GetDataProvider(connection), connection)
|
|
101 {
|
|
102 }
|
|
103
|
|
104 /// <summary>
|
|
105 /// Initializes a new instance of the <see cref="DbManager"/> class for the provided transaction.
|
|
106 /// </summary>
|
|
107 /// <include file="Examples.xml" path='examples/db[@name="ctor(IDbTransaction)"]/*' />
|
|
108 /// <param name="transaction"></param>
|
|
109 [DebuggerStepThrough]
|
|
110 public DbManager(IDbTransaction transaction)
|
|
111 : this(GetDataProvider(transaction.Connection), transaction)
|
|
112 {
|
|
113 }
|
|
114
|
|
115 /*
|
|
116 /// <summary>
|
|
117 /// Initializes a new instance of the <see cref="DbManager"/> class
|
|
118 /// and opens a database connection for the provided configuration and database connection.
|
|
119 /// </summary>
|
|
120 /// <remarks>
|
|
121 /// <para>
|
|
122 /// This constructor opens the connection only if the connection state equals
|
|
123 /// <see cref="System.Data.ConnectionState">ConnectionState.Closed</see>.
|
|
124 /// Otherwise, it neither opens nor closes the connection.
|
|
125 /// </para>
|
|
126 /// <para>
|
|
127 /// See the <see cref="ConfigurationString"/> property
|
|
128 /// for an explanation and use of the configuration string.
|
|
129 /// </para>
|
|
130 /// </remarks>
|
|
131 /// <include file="Examples.xml" path='examples/db[@name="ctor(IDbConnection,string)"]/*' />
|
|
132 /// <param name="connection">An instance of the <see cref="IDbConnection"/>.</param>
|
|
133 /// <param name="configurationString">The configuration string.</param>
|
|
134 /// <returns>An instance of the <see cref="DbManager"/> class.</returns>
|
|
135 [DebuggerStepThrough]
|
|
136 public DbManager(
|
|
137 IDbConnection connection,
|
|
138 string configurationString)
|
|
139 {
|
|
140 if (connection == null)
|
|
141 {
|
|
142 Init(configurationString);
|
|
143
|
|
144 if (configurationString != null)
|
|
145 OpenConnection(configurationString);
|
|
146 }
|
|
147 else
|
|
148 {
|
|
149 Init(connection);
|
|
150
|
|
151 _configurationString = configurationString;
|
|
152 _connection.ConnectionString = GetConnectionString(configurationString);
|
|
153
|
|
154 if (_connection.State == ConnectionState.Closed)
|
|
155 OpenConnection();
|
|
156 }
|
|
157 }
|
|
158 */
|
|
159
|
|
160 #endregion
|
|
161
|
|
162 #region Public Properties
|
|
163
|
|
164 private string _configurationString;
|
|
165 /// <summary>
|
|
166 /// Gets the string used to open a database.
|
|
167 /// </summary>
|
|
168 /// <value>
|
|
169 /// A string containing configuration settings.
|
|
170 /// </value>
|
|
171 /// <remarks>
|
|
172 /// <para>
|
|
173 /// An actual database connection string is read from the <i>appSettings</i> section
|
|
174 /// of application configuration file (App.config, Web.config, or Machine.config)
|
|
175 /// according to the follow rule:
|
|
176 /// </para>
|
|
177 /// <code>
|
|
178 /// <appSettings>
|
|
179 /// <add
|
|
180 /// key = "ConnectionString.<b>configurationString</b>"
|
|
181 /// va<i></i>lue = "Server=(local);Database=Northwind;Integrated Security=SSPI" />
|
|
182 /// </appSettings>
|
|
183 /// </code>
|
|
184 /// <para>
|
|
185 /// If the configuration string is empty, the following rule is applied:
|
|
186 /// </para>
|
|
187 /// <code>
|
|
188 /// <appSettings>
|
|
189 /// <add
|
|
190 /// key = "ConnectionString"
|
|
191 /// va<i></i>lue = "Server=(local);Database=Northwind;Integrated Security=SSPI" />
|
|
192 /// </appSettings>
|
|
193 /// </code>
|
|
194 /// <para>
|
|
195 /// If you don't want to use a configuration file, you can add a database connection string
|
|
196 /// using the <see cref="AddConnectionString(string)"/> method.
|
|
197 /// </para>
|
|
198 /// <para>
|
|
199 /// The configuration string may have a prefix used to define a data provider. The following table
|
|
200 /// contains prefixes for all supported data providers:
|
|
201 /// <list type="table">
|
|
202 /// <listheader><term>Prefix</term><description>Provider</description></listheader>
|
|
203 /// <item><term>Sql</term><description>Data Provider for SQL Server</description></item>
|
|
204 /// <item><term>OleDb</term><description>Data Provider for OLE DB</description></item>
|
|
205 /// <item><term>Odbc</term><description>Data Provider for ODBC</description></item>
|
|
206 /// <item><term>Oracle</term><description>Data Provider for Oracle</description></item>
|
|
207 /// </list>
|
|
208 /// </para>
|
|
209 /// </remarks>
|
|
210 /// <seealso cref="AddConnectionString(string)"/>
|
|
211 public string ConfigurationString
|
|
212 {
|
|
213 [DebuggerStepThrough]
|
|
214 get { return _configurationString; }
|
|
215 }
|
|
216
|
|
217 #endregion
|
|
218
|
|
219 #region Config Overrides
|
|
220
|
|
221 protected virtual void InitDataProvider(IDbConnection connection)
|
|
222 {
|
|
223 DataProvider = GetDataProvider(connection);
|
|
224 }
|
|
225
|
|
226 protected virtual IDbConnection CloneConnection()
|
|
227 {
|
|
228 if (Connection is ICloneable || ConfigurationString == null)
|
|
229 return _dataProvider.CloneConnection(_connection);
|
|
230
|
|
231 var con = DataProvider.CreateConnectionObject();
|
|
232
|
|
233 con.ConnectionString = GetConnectionString(ConfigurationString);
|
|
234
|
|
235 return con;
|
|
236 }
|
|
237
|
|
238 protected virtual string GetConnectionHash()
|
|
239 {
|
|
240 return ConfigurationString ?? Connection.ConnectionString.GetHashCode().ToString();
|
|
241 }
|
|
242
|
|
243 #endregion
|
|
244
|
|
245 #region Protected Static Members
|
|
246
|
|
247 static DbManager()
|
|
248 {
|
|
249 AddDataProvider(new Sql2012DataProvider());
|
|
250 AddDataProvider(new Sql2008DataProvider());
|
|
251 AddDataProvider(new Sql2005DataProvider());
|
|
252 AddDataProvider(new SqlDataProvider());
|
|
253 AddDataProvider(new Sql2000DataProvider());
|
|
254 AddDataProvider(new AccessDataProvider());
|
|
255 AddDataProvider(new OleDbDataProvider());
|
|
256 AddDataProvider(new OdbcDataProvider());
|
|
257
|
|
258 var section = BLToolkitSection.Instance;
|
|
259
|
|
260 if (section != null)
|
|
261 {
|
|
262 _defaultConfiguration = section.DefaultConfiguration;
|
|
263
|
|
264 foreach (DataProviderElement provider in section.DataProviders)
|
|
265 {
|
|
266 var dataProviderType = Type.GetType(provider.TypeName, true);
|
|
267 var providerInstance = (DataProviderBase)Activator.CreateInstance(dataProviderType);
|
|
268
|
|
269 if (!string.IsNullOrEmpty(provider.Name))
|
|
270 providerInstance.UniqueName = provider.Name;
|
|
271
|
|
272 providerInstance.Configure(provider.Attributes);
|
|
273
|
|
274 AddDataProvider(providerInstance);
|
|
275
|
|
276 if (!provider.Default)
|
|
277 continue;
|
|
278
|
|
279 if (_defaultDataProviderName != null)
|
|
280 {
|
|
281 throw new ConfigurationErrorsException(string.Format(
|
|
282 Resources.DbManager_MoreThenOneDefaultProvider, _defaultDataProviderName, providerInstance.UniqueName),
|
|
283 provider.ElementInformation.Source, provider.ElementInformation.LineNumber);
|
|
284 }
|
|
285
|
|
286 _defaultDataProviderName = providerInstance.UniqueName;
|
|
287 }
|
|
288 }
|
|
289
|
|
290 var dataProviders = ConfigurationManager.AppSettings.Get("BLToolkit.DataProviders");
|
|
291
|
|
292 if (dataProviders != null)
|
|
293 {
|
|
294 if (TraceSwitch.TraceWarning)
|
|
295 WriteTraceLine("Using appSettings\\BLToolkit.DataProviders is obsolete. Consider using bltoolkit configuration section instead.", TraceSwitch.DisplayName);
|
|
296
|
|
297 foreach (var dataProviderTypeName in dataProviders.Split(';'))
|
|
298 AddDataProvider(Type.GetType(dataProviderTypeName, true));
|
|
299 }
|
|
300
|
|
301 if (string.IsNullOrEmpty(_defaultConfiguration))
|
|
302 _defaultConfiguration = ConfigurationManager.AppSettings.Get("BLToolkit.DefaultConfiguration");
|
|
303
|
|
304 if (string.IsNullOrEmpty(_defaultDataProviderName))
|
|
305 _defaultDataProviderName = SqlDataProviderBase.NameString;
|
|
306 }
|
|
307
|
|
308 volatile static string _firstConfiguration;
|
|
309 private static DataProviderBase _firstProvider;
|
|
310 private static readonly Hashtable _configurationList = Hashtable.Synchronized(new Hashtable());
|
|
311 private static readonly Hashtable _anyProviderConfigurationList = Hashtable.Synchronized(new Hashtable());
|
|
312
|
|
313 private static DataProviderBase GetDataProvider(IDbConnection connection)
|
|
314 {
|
|
315 if (connection == null) throw new ArgumentNullException("connection");
|
|
316
|
|
317 var dp = _dataProviderTypeList[connection.GetType()];
|
|
318
|
|
319 if (dp == null)
|
|
320 throw new DataException(string.Format(
|
|
321 Resources.DbManager_UnknownConnectionType, connection.GetType().FullName));
|
|
322
|
|
323 return dp;
|
|
324 }
|
|
325
|
|
326 public static DataProviderBase GetDataProvider(string configurationString)
|
|
327 {
|
|
328 if (configurationString == null) throw new ArgumentNullException("configurationString");
|
|
329
|
|
330 if (configurationString.StartsWith(AnyProvider))
|
|
331 return FindFirstSuitableProvider(configurationString);
|
|
332
|
|
333 if (configurationString == _firstConfiguration)
|
|
334 return _firstProvider;
|
|
335
|
|
336 var dp = (DataProviderBase)_configurationList[configurationString];
|
|
337
|
|
338 if (dp == null)
|
|
339 {
|
|
340 var css = ConfigurationManager.ConnectionStrings[configurationString];
|
|
341
|
|
342 if (css != null && !string.IsNullOrEmpty(css.ProviderName))
|
|
343 {
|
|
344 string provider = null;
|
|
345
|
|
346 if (css.ProviderName == "System.Data.SqlClient")
|
|
347 {
|
|
348 try
|
|
349 {
|
|
350 using (SqlConnection sqlConnection = new SqlConnection(css.ConnectionString))
|
|
351 {
|
|
352 sqlConnection.Open();
|
|
353
|
|
354 string serverVersion = sqlConnection.ServerVersion;
|
|
355 string[] serverVersionDetails = serverVersion.Split(new string[] {"."},
|
|
356 StringSplitOptions.None);
|
|
357
|
|
358 int versionNumber = int.Parse(serverVersionDetails[0]);
|
|
359
|
|
360 switch (versionNumber)
|
|
361 {
|
|
362 case 8: provider = "MSSQL2000"; break;
|
|
363 case 9: provider = "MSSQL2005"; break; //MSSQL 2005 -> Can the same as 2008
|
|
364 case 10: provider = "MSSQL2008"; break;
|
|
365 case 11: provider = "MSSQL2012"; break;
|
|
366 default: provider = "MSSQL2008"; break;
|
|
367 }
|
|
368 }
|
|
369 }
|
|
370 catch (Exception)
|
|
371 {}
|
|
372 }
|
|
373
|
|
374 if (provider == null)
|
|
375 {
|
|
376 // This hack should be redone.
|
|
377 //
|
|
378 provider = css.ProviderName == "System.Data.SqlClient" ?
|
|
379 configurationString.IndexOf("2012") >= 0 ? "MSSQL2012" :
|
|
380 configurationString.IndexOf("2008") >= 0 ? "MSSQL2008" :
|
|
381 configurationString.IndexOf("2000") >= 0 ? "MSSQL2000" :
|
|
382 css.ProviderName :
|
|
383 css.ProviderName;
|
|
384 }
|
|
385
|
|
386 dp = _dataProviderNameList[provider];
|
|
387 }
|
|
388 else
|
|
389 {
|
|
390 // configurationString can be:
|
|
391 // '' : default provider, default configuration;
|
|
392 // '.' : default provider, default configuration;
|
|
393 // 'foo.bar' : 'foo' provider, 'bar' configuration;
|
|
394 // 'foo.' : 'foo' provider, default configuration;
|
|
395 // 'foo' : default provider, 'foo' configuration or
|
|
396 // foo provider, default configuration;
|
|
397 // '.foo' : default provider, 'foo' configuration;
|
|
398 // '.foo.bar': default provider, 'foo.bar' configuration;
|
|
399 //
|
|
400 // Default provider is SqlDataProvider
|
|
401 //
|
|
402 var cs = configurationString.ToUpper();
|
|
403 var key = _defaultDataProviderName;
|
|
404
|
|
405 if (cs.Length > 0)
|
|
406 {
|
|
407 cs += ProviderNameDivider;
|
|
408
|
|
409 foreach (var k in _dataProviderNameList.Keys)
|
|
410 {
|
|
411 if (cs.StartsWith(k + ProviderNameDivider))
|
|
412 {
|
|
413 key = k;
|
|
414 break;
|
|
415 }
|
|
416 }
|
|
417 }
|
|
418
|
|
419 dp = _dataProviderNameList[key];
|
|
420 }
|
|
421
|
|
422 if (dp == null)
|
|
423 throw new DataException(string.Format(
|
|
424 Resources.DbManager_UnknownDataProvider, configurationString));
|
|
425
|
|
426 _configurationList[configurationString] = dp;
|
|
427 }
|
|
428
|
|
429 if (_firstConfiguration == null)
|
|
430 {
|
|
431 lock (_configurationList.SyncRoot)
|
|
432 {
|
|
433 if (_firstConfiguration == null)
|
|
434 {
|
|
435 _firstConfiguration = configurationString;
|
|
436 _firstProvider = dp;
|
|
437 }
|
|
438 }
|
|
439 }
|
|
440
|
|
441 return dp;
|
|
442 }
|
|
443
|
|
444 private static bool IsMatchedConfigurationString(string configurationString, string csWithoutProvider)
|
|
445 {
|
|
446 int dividerPos;
|
|
447
|
|
448 return
|
|
449 !configurationString.StartsWith(AnyProvider) &&
|
|
450 (dividerPos = configurationString.IndexOf(ProviderNameDivider)) >= 0 &&
|
|
451 0 == StringComparer.OrdinalIgnoreCase.Compare
|
|
452 (configurationString.Substring(dividerPos + ProviderNameDivider.Length), csWithoutProvider);
|
|
453 }
|
|
454
|
|
455 private static DataProviderBase FindFirstSuitableProvider(string configurationString)
|
|
456 {
|
|
457 var cs = (string)_anyProviderConfigurationList[configurationString];
|
|
458 var searchRequired = (cs == null);
|
|
459
|
|
460 if (searchRequired)
|
|
461 {
|
|
462 var csWithoutProvider = configurationString.Substring(AnyProvider.Length);
|
|
463
|
|
464 if (configurationString.Length == 0) throw new ArgumentNullException("configurationString");
|
|
465
|
|
466 foreach (var str in _connectionStringList.Keys)
|
|
467 {
|
|
468 if (IsMatchedConfigurationString(str, csWithoutProvider))
|
|
469 {
|
|
470 cs = str;
|
|
471 break;
|
|
472 }
|
|
473 }
|
|
474
|
|
475 if (cs == null)
|
|
476 {
|
|
477 foreach (ConnectionStringSettings css in ConfigurationManager.ConnectionStrings)
|
|
478 {
|
|
479 if (IsMatchedConfigurationString(css.Name, csWithoutProvider))
|
|
480 {
|
|
481 cs = css.Name;
|
|
482 break;
|
|
483 }
|
|
484 }
|
|
485 }
|
|
486
|
|
487 if (cs == null)
|
|
488 {
|
|
489 foreach (var name in ConfigurationManager.AppSettings.AllKeys)
|
|
490 {
|
|
491 if (name.StartsWith("ConnectionString" + ProviderNameDivider))
|
|
492 {
|
|
493 var str = name.Substring(name.IndexOf(ProviderNameDivider) + ProviderNameDivider.Length);
|
|
494
|
|
495 if (IsMatchedConfigurationString(str, csWithoutProvider))
|
|
496 {
|
|
497 cs = str;
|
|
498 break;
|
|
499 }
|
|
500 }
|
|
501 }
|
|
502 }
|
|
503
|
|
504 if (cs == null)
|
|
505 cs = csWithoutProvider;
|
|
506 }
|
|
507
|
|
508 var dp = GetDataProvider(cs);
|
|
509
|
|
510 if (searchRequired)
|
|
511 _anyProviderConfigurationList[configurationString] = cs;
|
|
512 return dp;
|
|
513 }
|
|
514
|
|
515 private static readonly Dictionary<string,string> _connectionStringList = new Dictionary<string,string>(4);
|
|
516
|
|
517 public static string GetConnectionString(string configurationString)
|
|
518 {
|
|
519 // Use default configuration.
|
|
520 //
|
|
521 if (configurationString == null)
|
|
522 configurationString = DefaultConfiguration;
|
|
523
|
|
524 if (_anyProviderConfigurationList.Contains(configurationString))
|
|
525 configurationString = (string)_anyProviderConfigurationList[configurationString];
|
|
526
|
|
527 string str;
|
|
528
|
|
529 // Check cached strings first.
|
|
530 //
|
|
531 if (!_connectionStringList.TryGetValue(configurationString, out str))
|
|
532 {
|
|
533 lock (_dataProviderListLock)
|
|
534 {
|
|
535 // Connection string is not in the cache.
|
|
536 //
|
|
537 var key = string.Format("ConnectionString{0}{1}",
|
|
538 configurationString.Length == 0? String.Empty: ProviderNameDivider, configurationString);
|
|
539
|
|
540 var css = ConfigurationManager.ConnectionStrings[configurationString];
|
|
541
|
|
542 str = css != null? css.ConnectionString: ConfigurationManager.AppSettings.Get(key);
|
|
543
|
|
544 if (string.IsNullOrEmpty(str))
|
|
545 {
|
|
546 throw new DataException(string.Format(
|
|
547 Resources.DbManager_UnknownConfiguration, key));
|
|
548 }
|
|
549
|
|
550 // Store the result in cache.
|
|
551 //
|
|
552 _connectionStringList[configurationString] = str;
|
|
553 }
|
|
554 }
|
|
555
|
|
556 return str;
|
|
557 }
|
|
558
|
|
559 /*
|
|
560 private void OpenConnection(string configurationString)
|
|
561 {
|
|
562 // If connection is already opened, we close it and open again.
|
|
563 //
|
|
564 if (_connection != null)
|
|
565 {
|
|
566 Dispose();
|
|
567 GC.ReRegisterForFinalize(this);
|
|
568 }
|
|
569
|
|
570 // Store the configuration string.
|
|
571 //
|
|
572 _configurationString = configurationString;
|
|
573
|
|
574 // Create and open the connection object.
|
|
575 //
|
|
576 _connection = _dataProvider.CreateConnectionObject();
|
|
577 _connection.ConnectionString = GetConnectionString(ConfigurationString);
|
|
578
|
|
579 OpenConnection();
|
|
580 }
|
|
581 */
|
|
582
|
|
583 #endregion
|
|
584
|
|
585 #region AddDataProvider
|
|
586
|
|
587 static readonly Dictionary<string, DataProviderBase> _dataProviderNameList = new Dictionary<string, DataProviderBase>(8, StringComparer.OrdinalIgnoreCase);
|
|
588 static readonly Dictionary<Type, DataProviderBase> _dataProviderTypeList = new Dictionary<Type, DataProviderBase>(4);
|
|
589 static readonly object _dataProviderListLock = new object();
|
|
590
|
|
591 /// <summary>
|
|
592 /// Adds a new data provider.
|
|
593 /// </summary>
|
|
594 /// <remarks>
|
|
595 /// The method can be used to register a new data provider for further use.
|
|
596 /// </remarks>
|
|
597 /// <include file="Examples1.xml" path='examples/db[@name="AddDataProvider(DataProvider.IDataProvider)"]/*' />
|
|
598 /// <seealso cref="AddConnectionString(string)"/>
|
|
599 /// <seealso cref="BLToolkit.Data.DataProvider.DataProviderBase.Name"/>
|
|
600 /// <param name="dataProvider">An instance of the <see cref="BLToolkit.Data.DataProvider.DataProviderBase"/> interface.</param>
|
|
601 public static void AddDataProvider(DataProviderBase dataProvider)
|
|
602 {
|
|
603 if (null == dataProvider)
|
|
604 throw new ArgumentNullException("dataProvider");
|
|
605
|
|
606 if (string.IsNullOrEmpty(dataProvider.UniqueName))
|
|
607 throw new ArgumentException(Resources.DbManager_InvalidDataProviderName, "dataProvider");
|
|
608
|
|
609 if (string.IsNullOrEmpty(dataProvider.ProviderName))
|
|
610 throw new ArgumentException(Resources.DbManager_InvalidDataProviderProviderName, "dataProvider");
|
|
611
|
|
612 if (dataProvider.ConnectionType == null || !typeof(IDbConnection).IsAssignableFrom(dataProvider.ConnectionType))
|
|
613 throw new ArgumentException(Resources.DbManager_InvalidDataProviderConnectionType, "dataProvider");
|
|
614
|
|
615 lock (_dataProviderListLock)
|
|
616 {
|
|
617 _dataProviderNameList[dataProvider.UniqueName.ToUpper()] = dataProvider;
|
|
618 _dataProviderNameList[dataProvider.ProviderName] = dataProvider;
|
|
619 _dataProviderTypeList[dataProvider.ConnectionType] = dataProvider;
|
|
620 }
|
|
621 }
|
|
622
|
|
623 /// <summary>
|
|
624 /// Adds a new data provider witch a specified name.
|
|
625 /// </summary>
|
|
626 /// <remarks>
|
|
627 /// The method can be used to register a new data provider for further use.
|
|
628 /// </remarks>
|
|
629 /// <include file="Examples1.xml" path='examples/db[@name="AddDataProvider(DataProvider.IDataProvider)"]/*' />
|
|
630 /// <seealso cref="AddConnectionString(string)"/>
|
|
631 /// <seealso cref="BLToolkit.Data.DataProvider.DataProviderBase.Name"/>
|
|
632 /// <param name="providerName">The data provider name.</param>
|
|
633 /// <param name="dataProvider">An instance of the <see cref="BLToolkit.Data.DataProvider.DataProviderBase"/> interface.</param>
|
|
634 public static void AddDataProvider(string providerName, DataProviderBase dataProvider)
|
|
635 {
|
|
636 if (dataProvider == null)
|
|
637 throw new ArgumentNullException("dataProvider");
|
|
638
|
|
639 if (string.IsNullOrEmpty(providerName))
|
|
640 throw new ArgumentException(Resources.DbManager_InvalidDataProviderName, "providerName");
|
|
641
|
|
642 dataProvider.UniqueName = providerName;
|
|
643 AddDataProvider(dataProvider);
|
|
644 }
|
|
645
|
|
646 /// <summary>
|
|
647 /// Adds a new data provider.
|
|
648 /// </summary>
|
|
649 /// <remarks>
|
|
650 /// The method can be used to register a new data provider for further use.
|
|
651 /// </remarks>
|
|
652 /// <seealso cref="AddConnectionString(string)"/>
|
|
653 /// <seealso cref="BLToolkit.Data.DataProvider.DataProviderBase.Name"/>
|
|
654 /// <param name="dataProviderType">A data provider type.</param>
|
|
655 public static void AddDataProvider(Type dataProviderType)
|
|
656 {
|
|
657 AddDataProvider((DataProviderBase)Activator.CreateInstance(dataProviderType));
|
|
658 }
|
|
659
|
|
660 /// <summary>
|
|
661 /// Adds a new data provider witch a specified name.
|
|
662 /// </summary>
|
|
663 /// <remarks>
|
|
664 /// The method can be used to register a new data provider for further use.
|
|
665 /// </remarks>
|
|
666 /// <seealso cref="AddConnectionString(string)"/>
|
|
667 /// <seealso cref="BLToolkit.Data.DataProvider.DataProviderBase.Name"/>
|
|
668 /// <param name="providerName">The data provider name.</param>
|
|
669 /// <param name="dataProviderType">A data provider type.</param>
|
|
670 public static void AddDataProvider(string providerName, Type dataProviderType)
|
|
671 {
|
|
672 AddDataProvider(providerName, (DataProviderBase)Activator.CreateInstance(dataProviderType));
|
|
673 }
|
|
674
|
|
675 #endregion
|
|
676
|
|
677 #region AddConnectionString
|
|
678
|
|
679 /// <summary>
|
|
680 /// Adds a new connection string or replaces existing one.
|
|
681 /// </summary>
|
|
682 /// <remarks>
|
|
683 /// Use this method when you use only one configuration and
|
|
684 /// you don't want to use a configuration file.
|
|
685 /// </remarks>
|
|
686 /// <include file="Examples.xml" path='examples/db[@name="AddConnectionString(string)"]/*' />
|
|
687 /// <param name="connectionString">A valid database connection string.</param>
|
|
688 public static void AddConnectionString(string connectionString)
|
|
689 {
|
|
690 AddConnectionString(string.Empty, connectionString);
|
|
691 }
|
|
692
|
|
693 /// <summary>
|
|
694 /// Adds a new connection string or replaces existing one.
|
|
695 /// </summary>
|
|
696 /// <remarks>
|
|
697 /// Use this method when you use multiple configurations and
|
|
698 /// you don't want to use a configuration file.
|
|
699 /// </remarks>
|
|
700 /// <include file="Examples.xml" path='examples/db[@name="AddConnectionString(string,string)"]/*' />
|
|
701 /// <param name="configurationString">The configuration string.</param>
|
|
702 /// <param name="connectionString">A valid database connection string.</param>
|
|
703 public static void AddConnectionString(string configurationString, string connectionString)
|
|
704 {
|
|
705 _connectionStringList[configurationString] = connectionString;
|
|
706 }
|
|
707
|
|
708 /// <summary>
|
|
709 /// Adds a new connection string or replaces existing one.
|
|
710 /// </summary>
|
|
711 /// <remarks>
|
|
712 /// Use this method when you use multiple configurations and
|
|
713 /// you don't want to use a configuration file.
|
|
714 /// </remarks>
|
|
715 /// <include file="Examples.xml" path='examples/db[@name="AddConnectionString(string,string)"]/*' />
|
|
716 /// <param name="providerName">The data provider name.</param>
|
|
717 /// <param name="configurationString">The configuration string.</param>
|
|
718 /// <param name="connectionString">A valid database connection string.</param>
|
|
719 public static void AddConnectionString(
|
|
720 string providerName, string configurationString, string connectionString)
|
|
721 {
|
|
722 AddConnectionString(providerName + ProviderNameDivider + configurationString, connectionString);
|
|
723 }
|
|
724
|
|
725 #endregion
|
|
726
|
|
727 #region Public Static Properties
|
|
728
|
|
729 public const string ProviderNameDivider = ".";
|
|
730 public const string AnyProvider = "*" + ProviderNameDivider;
|
|
731
|
|
732 private static readonly string _defaultDataProviderName;
|
|
733 private static string _defaultConfiguration;
|
|
734
|
|
735 /// <summary>
|
|
736 /// Gets and sets the default configuration string.
|
|
737 /// </summary>
|
|
738 /// <remarks>
|
|
739 /// See the <see cref="ConfigurationString"/> property
|
|
740 /// for an explanation and use of the default configuration.
|
|
741 /// </remarks>
|
|
742 /// <value>
|
|
743 /// A string containing default configuration settings.
|
|
744 /// </value>
|
|
745 /// <seealso cref="ConfigurationString"/>
|
|
746 public static string DefaultConfiguration
|
|
747 {
|
|
748 get
|
|
749 {
|
|
750 if (_defaultConfiguration == null)
|
|
751 {
|
|
752 // Grab first registered configuration.
|
|
753 //
|
|
754 var defaultConfiguration = _connectionStringList.Select(de => de.Key).FirstOrDefault();
|
|
755
|
|
756 if (defaultConfiguration == null)
|
|
757 {
|
|
758 defaultConfiguration = string.Empty;
|
|
759
|
|
760 foreach (ConnectionStringSettings css in ConfigurationManager.ConnectionStrings)
|
|
761 {
|
|
762 if (css.ElementInformation.Source != null &&
|
|
763 !css.ElementInformation.Source.EndsWith("machine.config", StringComparison.OrdinalIgnoreCase))
|
|
764 {
|
|
765 defaultConfiguration = css.Name;
|
|
766 break;
|
|
767 }
|
|
768 }
|
|
769 }
|
|
770
|
|
771 _defaultConfiguration = defaultConfiguration;
|
|
772 }
|
|
773
|
|
774 return _defaultConfiguration;
|
|
775 }
|
|
776
|
|
777 set { _defaultConfiguration = value; }
|
|
778 }
|
|
779
|
|
780 #endregion
|
|
781 }
|
|
782 }
|