DBConnection.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DbConnection.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. // <owner current="true" primary="false">Microsoft</owner>
  8. //------------------------------------------------------------------------------
  9. namespace System.Data.Common {
  10. using System;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. public abstract class DbConnection : Component, IDbConnection { // V1.2.3300
  16. private StateChangeEventHandler _stateChangeEventHandler;
  17. protected DbConnection() : base() {
  18. }
  19. [
  20. DefaultValue(""),
  21. #pragma warning disable 618 // ignore obsolete warning about RecommendedAsConfigurable to use SettingsBindableAttribute
  22. RecommendedAsConfigurable(true),
  23. #pragma warning restore 618
  24. SettingsBindableAttribute(true),
  25. RefreshProperties(RefreshProperties.All),
  26. ResCategoryAttribute(Res.DataCategory_Data),
  27. ]
  28. abstract public string ConnectionString {
  29. get;
  30. set;
  31. }
  32. [
  33. ResCategoryAttribute(Res.DataCategory_Data),
  34. ]
  35. virtual public int ConnectionTimeout {
  36. get {
  37. return ADP.DefaultConnectionTimeout;
  38. }
  39. }
  40. [
  41. ResCategoryAttribute(Res.DataCategory_Data),
  42. ]
  43. abstract public string Database {
  44. get;
  45. }
  46. [
  47. ResCategoryAttribute(Res.DataCategory_Data),
  48. ]
  49. abstract public string DataSource {
  50. // NOTE: if you plan on allowing the data source to be changed, you
  51. // should implement a ChangeDataSource method, in keeping with
  52. // the ChangeDatabase method paradigm.
  53. get;
  54. }
  55. /// <summary>
  56. /// The associated provider factory for derived class.
  57. /// </summary>
  58. virtual protected DbProviderFactory DbProviderFactory {
  59. get {
  60. return null;
  61. }
  62. }
  63. internal DbProviderFactory ProviderFactory {
  64. get {
  65. return DbProviderFactory;
  66. }
  67. }
  68. [
  69. Browsable(false),
  70. ]
  71. abstract public string ServerVersion {
  72. get;
  73. }
  74. [
  75. Browsable(false),
  76. ResDescriptionAttribute(Res.DbConnection_State),
  77. ]
  78. abstract public ConnectionState State {
  79. get;
  80. }
  81. [
  82. ResCategoryAttribute(Res.DataCategory_StateChange),
  83. ResDescriptionAttribute(Res.DbConnection_StateChange),
  84. ]
  85. virtual public event StateChangeEventHandler StateChange {
  86. add {
  87. _stateChangeEventHandler += value;
  88. }
  89. remove {
  90. _stateChangeEventHandler -= value;
  91. }
  92. }
  93. abstract protected DbTransaction BeginDbTransaction(IsolationLevel isolationLevel);
  94. public DbTransaction BeginTransaction() {
  95. return BeginDbTransaction(IsolationLevel.Unspecified);
  96. }
  97. public DbTransaction BeginTransaction(IsolationLevel isolationLevel) {
  98. return BeginDbTransaction(isolationLevel);
  99. }
  100. IDbTransaction IDbConnection.BeginTransaction() {
  101. return BeginDbTransaction(IsolationLevel.Unspecified);
  102. }
  103. IDbTransaction IDbConnection.BeginTransaction(IsolationLevel isolationLevel) {
  104. return BeginDbTransaction(isolationLevel);
  105. }
  106. abstract public void Close();
  107. abstract public void ChangeDatabase(string databaseName);
  108. public DbCommand CreateCommand() {
  109. return CreateDbCommand();
  110. }
  111. IDbCommand IDbConnection.CreateCommand() {
  112. return CreateDbCommand();
  113. }
  114. abstract protected DbCommand CreateDbCommand();
  115. virtual public void EnlistTransaction(System.Transactions.Transaction transaction) {
  116. // NOTE: This is virtual because not all providers may choose to support
  117. // distributed transactions.
  118. throw ADP.NotSupported();
  119. }
  120. // these need to be here so that GetSchema is visible when programming to a dbConnection object.
  121. // they are overridden by the real implementations in DbConnectionBase
  122. virtual public DataTable GetSchema() {
  123. throw ADP.NotSupported();
  124. }
  125. virtual public DataTable GetSchema(string collectionName) {
  126. throw ADP.NotSupported();
  127. }
  128. virtual public DataTable GetSchema(string collectionName, string[] restrictionValues ) {
  129. throw ADP.NotSupported();
  130. }
  131. internal bool _supressStateChangeForReconnection = false; // Do not use for anything else ! Value will be overwritten by CR process
  132. protected virtual void OnStateChange(StateChangeEventArgs stateChange) {
  133. if (_supressStateChangeForReconnection) {
  134. return;
  135. }
  136. StateChangeEventHandler handler = _stateChangeEventHandler;
  137. if (null != handler) {
  138. handler(this, stateChange);
  139. }
  140. }
  141. internal bool ForceNewConnection {
  142. get;
  143. set;
  144. }
  145. abstract public void Open();
  146. public Task OpenAsync() {
  147. return OpenAsync(CancellationToken.None);
  148. }
  149. public virtual Task OpenAsync(CancellationToken cancellationToken) {
  150. TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
  151. if (cancellationToken.IsCancellationRequested) {
  152. taskCompletionSource.SetCanceled();
  153. }
  154. else {
  155. try {
  156. Open();
  157. taskCompletionSource.SetResult(null);
  158. }
  159. catch (Exception e) {
  160. taskCompletionSource.SetException(e);
  161. }
  162. }
  163. return taskCompletionSource.Task;
  164. }
  165. }
  166. }