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
|
//------------------------------------------------------------------------------
// <copyright file="DbConnection.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Data.Common {
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
public abstract class DbConnection : Component, IDbConnection { // V1.2.3300
private StateChangeEventHandler _stateChangeEventHandler;
protected DbConnection() : base() {
}
[
DefaultValue(""),
#pragma warning disable 618 // ignore obsolete warning about RecommendedAsConfigurable to use SettingsBindableAttribute
RecommendedAsConfigurable(true),
#pragma warning restore 618
SettingsBindableAttribute(true),
RefreshProperties(RefreshProperties.All),
ResCategoryAttribute(Res.DataCategory_Data),
]
abstract public string ConnectionString {
get;
set;
}
[
ResCategoryAttribute(Res.DataCategory_Data),
]
virtual public int ConnectionTimeout {
get {
return ADP.DefaultConnectionTimeout;
}
}
[
ResCategoryAttribute(Res.DataCategory_Data),
]
abstract public string Database {
get;
}
[
ResCategoryAttribute(Res.DataCategory_Data),
]
abstract public string DataSource {
// NOTE: if you plan on allowing the data source to be changed, you
// should implement a ChangeDataSource method, in keeping with
// the ChangeDatabase method paradigm.
get;
}
/// <summary>
/// The associated provider factory for derived class.
/// </summary>
virtual protected DbProviderFactory DbProviderFactory {
get {
return null;
}
}
internal DbProviderFactory ProviderFactory {
get {
return DbProviderFactory;
}
}
[
Browsable(false),
]
abstract public string ServerVersion {
get;
}
[
Browsable(false),
ResDescriptionAttribute(Res.DbConnection_State),
]
abstract public ConnectionState State {
get;
}
[
ResCategoryAttribute(Res.DataCategory_StateChange),
ResDescriptionAttribute(Res.DbConnection_StateChange),
]
virtual public event StateChangeEventHandler StateChange {
add {
_stateChangeEventHandler += value;
}
remove {
_stateChangeEventHandler -= value;
}
}
abstract protected DbTransaction BeginDbTransaction(IsolationLevel isolationLevel);
public DbTransaction BeginTransaction() {
return BeginDbTransaction(IsolationLevel.Unspecified);
}
public DbTransaction BeginTransaction(IsolationLevel isolationLevel) {
return BeginDbTransaction(isolationLevel);
}
IDbTransaction IDbConnection.BeginTransaction() {
return BeginDbTransaction(IsolationLevel.Unspecified);
}
IDbTransaction IDbConnection.BeginTransaction(IsolationLevel isolationLevel) {
return BeginDbTransaction(isolationLevel);
}
abstract public void Close();
abstract public void ChangeDatabase(string databaseName);
public DbCommand CreateCommand() {
return CreateDbCommand();
}
IDbCommand IDbConnection.CreateCommand() {
return CreateDbCommand();
}
abstract protected DbCommand CreateDbCommand();
virtual public void EnlistTransaction(System.Transactions.Transaction transaction) {
// NOTE: This is virtual because not all providers may choose to support
// distributed transactions.
throw ADP.NotSupported();
}
// these need to be here so that GetSchema is visible when programming to a dbConnection object.
// they are overridden by the real implementations in DbConnectionBase
virtual public DataTable GetSchema() {
throw ADP.NotSupported();
}
virtual public DataTable GetSchema(string collectionName) {
throw ADP.NotSupported();
}
virtual public DataTable GetSchema(string collectionName, string[] restrictionValues ) {
throw ADP.NotSupported();
}
internal bool _supressStateChangeForReconnection = false; // Do not use for anything else ! Value will be overwritten by CR process
protected virtual void OnStateChange(StateChangeEventArgs stateChange) {
if (_supressStateChangeForReconnection) {
return;
}
StateChangeEventHandler handler = _stateChangeEventHandler;
if (null != handler) {
handler(this, stateChange);
}
}
internal bool ForceNewConnection {
get;
set;
}
abstract public void Open();
public Task OpenAsync() {
return OpenAsync(CancellationToken.None);
}
public virtual Task OpenAsync(CancellationToken cancellationToken) {
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
if (cancellationToken.IsCancellationRequested) {
taskCompletionSource.SetCanceled();
}
else {
try {
Open();
taskCompletionSource.SetResult(null);
}
catch (Exception e) {
taskCompletionSource.SetException(e);
}
}
return taskCompletionSource.Task;
}
}
}
|