25-05-13, 04:18 AM
(آخر تعديل لهذه المشاركة : 06-07-13, 03:51 PM {2} بواسطة محمود رغمان.)
Imports System
Imports System.ComponentModel
Imports System.Data.Common
Imports System.Data.ProviderBase
Imports System.Diagnostics
Imports System.EnterpriseServices
Imports System.Globalization
Imports System.Runtime.ConstrainedExecution
Imports System.Runtime.InteropServices
Imports System.Security
Imports System.Security.Permissions
Imports System.Text
Imports System.Threading
Imports System.Transactions
Namespace System.Data.OleDb
<DefaultEvent("InfoMessage")>
Public Class OleDbConnection
Inherits DbConnection
Implements ICloneable, IDbConnection, IDisposable
Private Shared EventInfoMessage As Object = New Object()
Private Shared _connectionFactory As DbConnectionFactory = OleDbConnectionFactory.SingletonInstance
Friend Shared ExecutePermission As CodeAccessPermission = OleDbConnection.CreateExecutePermission()
Private _userConnectionOptions As DbConnectionOptions
Private _poolGroup As DbConnectionPoolGroup
Private _innerConnection As DbConnectionInternal
Private _closeCount As Integer
Private Shared _objectTypeCount As Integer
Friend ObjectID As Integer = Interlocked.Increment(OleDbConnection._objectTypeCount)
<ResCategory("DataCategory_InfoMessage"), ResDescription("DbConnection_InfoMessage")>
Public Custom Event InfoMessage As OleDbInfoMessageEventHandler
AddHandler
MyBase.Events.[AddHandler](OleDbConnection.EventInfoMessage, value)
End AddHandler
RemoveHandler
MyBase.Events.[RemoveHandler](OleDbConnection.EventInfoMessage, value)
End RemoveHandler
End Event
<DefaultValue(""), Editor("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), RecommendedAsConfigurable(True), RefreshProperties(RefreshProperties.All), ResCategory("DataCategory_Data"), ResDescription("OleDbConnection_ConnectionString")>
Public Override Property ConnectionString() As String
Get
Return Me.ConnectionString_Get()
End Get
Set(value As String)
Me.ConnectionString_Set(value)
End Set
End Property
Private ReadOnly Property OleDbConnectionStringValue() As OleDbConnectionString
Get
Return CType(Me.ConnectionOptions, OleDbConnectionString)
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), ResDescription("OleDbConnection_ConnectionTimeout")>
Public Override ReadOnly Property ConnectionTimeout() As Integer
Get
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.get_ConnectionTimeout|API> %d#" & vbLf, Me.ObjectID)
Dim result As Integer
Try
Dim obj As Object
If Me.IsOpen Then
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DBInit, 66)
Else
Dim oleDbConnectionStringValue As OleDbConnectionString = Me.OleDbConnectionStringValue
obj = (If((oleDbConnectionStringValue IsNot Nothing), oleDbConnectionStringValue.ConnectTimeout, 15))
End If
If obj IsNot Nothing Then
result = Convert.ToInt32(obj, CultureInfo.InvariantCulture)
Else
result = 15
End If
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), ResDescription("OleDbConnection_Database")>
Public Override ReadOnly Property Database() As String
Get
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.get_Database|API> %d#" & vbLf, Me.ObjectID)
Dim result As String
Try
Dim oleDbConnectionString As OleDbConnectionString = CType(Me.UserConnectionOptions, OleDbConnectionString)
Dim obj As Object = If((oleDbConnectionString IsNot Nothing), oleDbConnectionString.InitialCatalog, ADP.StrEmpty)
If obj IsNot Nothing AndAlso Not(CStr(obj)).StartsWith("|datadirectory|", StringComparison.OrdinalIgnoreCase) Then
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
If openConnection IsNot Nothing Then
If openConnection.HasSession Then
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DataSource, 37)
Else
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DBInit, 233)
End If
Else
oleDbConnectionString = Me.OleDbConnectionStringValue
obj = (If((oleDbConnectionString IsNot Nothing), oleDbConnectionString.InitialCatalog, ADP.StrEmpty))
End If
End If
result = Convert.ToString(obj, CultureInfo.InvariantCulture)
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Get
End Property
<Browsable(True), ResDescription("OleDbConnection_DataSource")>
Public Override ReadOnly Property DataSource() As String
Get
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.get_DataSource|API> %d#" & vbLf, Me.ObjectID)
Dim result As String
Try
Dim oleDbConnectionString As OleDbConnectionString = CType(Me.UserConnectionOptions, OleDbConnectionString)
Dim obj As Object = If((oleDbConnectionString IsNot Nothing), oleDbConnectionString.DataSource, ADP.StrEmpty)
If obj IsNot Nothing AndAlso Not(CStr(obj)).StartsWith("|datadirectory|", StringComparison.OrdinalIgnoreCase) Then
If Me.IsOpen Then
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DBInit, 59)
If obj Is Nothing OrElse (TypeOf obj Is String AndAlso (TryCast(obj, String)).Length = 0) Then
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DataSourceInfo, 38)
End If
Else
oleDbConnectionString = Me.OleDbConnectionStringValue
obj = (If((oleDbConnectionString IsNot Nothing), oleDbConnectionString.DataSource, ADP.StrEmpty))
End If
End If
result = Convert.ToString(obj, CultureInfo.InvariantCulture)
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Get
End Property
Friend ReadOnly Property IsOpen() As Boolean
Get
Return Nothing IsNot Me.GetOpenConnection()
End Get
End Property
Friend WriteOnly Property LocalTransaction() As OleDbTransaction
Set(value As OleDbTransaction)
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
If openConnection IsNot Nothing Then
openConnection.LocalTransaction = value
End If
End Set
End Property
<Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), ResCategory("DataCategory_Data"), ResDescription("OleDbConnection_Provider")>
Public ReadOnly Property Provider() As String
Get
Bid.Trace("<oledb.OleDbConnection.get_Provider|API> %d#" & vbLf, Me.ObjectID)
Dim oleDbConnectionStringValue As OleDbConnectionString = Me.OleDbConnectionStringValue
Dim text As String = If((oleDbConnectionStringValue IsNot Nothing), oleDbConnectionStringValue.ConvertValueToString("provider", Nothing), Nothing)
If text Is Nothing Then
Return ADP.StrEmpty
End If
Return text
End Get
End Property
Friend ReadOnly Property ProviderInfo() As OleDbConnectionPoolGroupProviderInfo
Get
Return CType(Me.PoolGroup.ProviderInfo, OleDbConnectionPoolGroupProviderInfo)
End Get
End Property
<ResDescription("OleDbConnection_ServerVersion")>
Public Override ReadOnly Property ServerVersion() As String
Get
Return Me.InnerConnection.ServerVersion
End Get
End Property
Friend ReadOnly Property CloseCount() As Integer
Get
Return Me._closeCount
End Get
End Property
Friend ReadOnly Property ConnectionFactory() As DbConnectionFactory
Get
Return OleDbConnection._connectionFactory
End Get
End Property
Friend ReadOnly Property ConnectionOptions() As DbConnectionOptions
Get
Dim poolGroup As DbConnectionPoolGroup = Me.PoolGroup
If poolGroup Is Nothing Then
Return Nothing
End If
Return poolGroup.ConnectionOptions
End Get
End Property
Friend ReadOnly Property InnerConnection() As DbConnectionInternal
Get
Return Me._innerConnection
End Get
End Property
Friend Property PoolGroup() As DbConnectionPoolGroup
Get
Return Me._poolGroup
End Get
Set(value As DbConnectionPoolGroup)
Me._poolGroup = value
End Set
End Property
<Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), ResDescription("DbConnection_State")>
Public Override ReadOnly Property State() As ConnectionState
Get
Return Me.InnerConnection.State
End Get
End Property
Friend ReadOnly Property UserConnectionOptions() As DbConnectionOptions
Get
Return Me._userConnectionOptions
End Get
End Property
Public Sub New(connectionString As String)
Me.[New]()
Me.ConnectionString = connectionString
End Sub
Private Sub New(connection As OleDbConnection)
Me.[New]()
Me.CopyFrom(connection)
End Sub
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Sub ResetState()
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbCommand.ResetState|API> %d#" & vbLf, Me.ObjectID)
Try
If Me.IsOpen Then
Dim dataSourcePropertyValue As Object = Me.GetDataSourcePropertyValue(OleDbPropertySetGuid.DataSourceInfo, 244)
If TypeOf dataSourcePropertyValue Is Integer Then
Select Case CInt(dataSourcePropertyValue)
Case 0, 2
Me.GetOpenConnection().DoomThisConnection()
Me.NotifyWeakReference(-1)
Me.Close()
End Select
End If
End If
Finally
Bid.ScopeLeave(intPtr)
End Try
End Sub
Friend Function ICommandText() As UnsafeNativeMethods.ICommandText
Return Me.GetOpenConnection().ICommandText()
End Function
Private Function IDBProperties() As IDBPropertiesWrapper
Return Me.GetOpenConnection().IDBProperties()
End Function
Friend Function IOpenRowset() As IOpenRowsetWrapper
Return Me.GetOpenConnection().IOpenRowset()
End Function
Friend Function SqlSupport() As Integer
Return Me.OleDbConnectionStringValue.GetSqlSupport(Me)
End Function
Friend Function SupportMultipleResults() As Boolean
Return Me.OleDbConnectionStringValue.GetSupportMultipleResults(Me)
End Function
Friend Function SupportIRow(cmd As OleDbCommand) As Boolean
Return Me.OleDbConnectionStringValue.GetSupportIRow(Me, cmd)
End Function
Friend Function QuotedIdentifierCase() As Integer
Dim dataSourcePropertyValue As Object = Me.GetDataSourcePropertyValue(OleDbPropertySetGuid.DataSourceInfo, 100)
Dim result As Integer
If TypeOf dataSourcePropertyValue Is Integer Then
result = CInt(dataSourcePropertyValue)
Else
result = -1
End If
Return result
End Function
Public Function BeginTransaction() As OleDbTransaction
Return Me.BeginTransaction(System.Data.IsolationLevel.Unspecified)
End Function
Public Function BeginTransaction(isolationLevel As System.Data.IsolationLevel) As OleDbTransaction
Return CType(Me.InnerConnection.BeginTransaction(isolationLevel), OleDbTransaction)
End Function
Public Override Sub ChangeDatabase(value As String)
OleDbConnection.ExecutePermission.Demand()
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.ChangeDatabase|API> %d#, value='%ls'" & vbLf, Me.ObjectID, value)
Try
Me.CheckStateOpen("ChangeDatabase")
If value Is Nothing OrElse value.Trim().Length = 0 Then
Throw ADP.EmptyDatabaseName()
End If
Me.SetDataSourcePropertyValue(OleDbPropertySetGuid.DataSource, 37, "current catalog", True, value)
Finally
Bid.ScopeLeave(intPtr)
End Try
End Sub
Friend Sub CheckStateOpen(method As String)
Dim state As ConnectionState = Me.State
If ConnectionState.Open <> state Then
Throw ADP.OpenConnectionRequired(method, state)
End If
End Sub
Function Clone() As Object Implements ICloneable.Clone
Dim oleDbConnection As OleDbConnection = New OleDbConnection(Me)
Bid.Trace("<oledb.OleDbConnection.Clone|API> %d#, clone=%d#" & vbLf, Me.ObjectID, oleDbConnection.ObjectID)
Return oleDbConnection
End Function
Public Override Sub Close()
Me.InnerConnection.CloseConnection(Me, Me.ConnectionFactory)
End Sub
Public Function CreateCommand() As OleDbCommand
Return New OleDbCommand("", Me)
End Function
Private Sub DisposeMe(disposing As Boolean)
If disposing AndAlso MyBase.DesignMode Then
OleDbConnection.ReleaseObjectPool()
End If
End Sub
Public Sub EnlistDistributedTransaction(transaction As ITransaction)
Me.EnlistDistributedTransactionHelper(transaction)
End Sub
Friend Function GetDataSourcePropertyValue(propertySet As Guid, propertyID As Integer) As Object
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
Return openConnection.GetDataSourcePropertyValue(propertySet, propertyID)
End Function
Friend Function GetDataSourceValue(propertySet As Guid, propertyID As Integer) As Object
Dim obj As Object = Me.GetDataSourcePropertyValue(propertySet, propertyID)
If TypeOf obj Is OleDbPropertyStatus OrElse Convert.IsDBNull(obj) Then
obj = Nothing
End If
Return obj
End Function
Private Function GetOpenConnection() As OleDbConnectionInternal
Dim innerConnection As DbConnectionInternal = Me.InnerConnection
Return TryCast(innerConnection, OleDbConnectionInternal)
End Function
Friend Sub GetLiteralQuotes(method As String, <Out()> ByRef quotePrefix As String, <Out()> ByRef quoteSuffix As String)
Me.CheckStateOpen(method)
Dim providerInfo As OleDbConnectionPoolGroupProviderInfo = Me.ProviderInfo
If providerInfo.HasQuoteFix Then
quotePrefix = providerInfo.QuotePrefix
quoteSuffix = providerInfo.QuoteSuffix
Return
End If
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
quotePrefix = openConnection.GetLiteralInfo(15)
quoteSuffix = openConnection.GetLiteralInfo(28)
If quotePrefix Is Nothing Then
quotePrefix = ""
End If
If quoteSuffix Is Nothing Then
quoteSuffix = quotePrefix
End If
providerInfo.SetQuoteFix(quotePrefix, quoteSuffix)
End Sub
Public Function GetOleDbSchemaTable(schema As Guid, restrictions As Object()) As DataTable
OleDbConnection.ExecutePermission.Demand()
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.GetOleDbSchemaTable|API> %d#, schema=%p{GUID}, restrictions" & vbLf, Me.ObjectID, schema)
Dim result As DataTable
Try
Me.CheckStateOpen("GetOleDbSchemaTable")
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
If OleDbSchemaGuid.DbInfoLiterals = schema Then
If restrictions IsNot Nothing AndAlso restrictions.Length <> 0 Then
Throw ODB.InvalidRestrictionsDbInfoLiteral("restrictions")
End If
result = openConnection.BuildInfoLiterals()
Else
If OleDbSchemaGuid.SchemaGuids = schema Then
If restrictions IsNot Nothing AndAlso restrictions.Length <> 0 Then
Throw ODB.InvalidRestrictionsSchemaGuids("restrictions")
End If
result = openConnection.BuildSchemaGuids()
Else
If OleDbSchemaGuid.DbInfoKeywords = schema Then
If restrictions IsNot Nothing AndAlso restrictions.Length <> 0 Then
Throw ODB.InvalidRestrictionsDbInfoKeywords("restrictions")
End If
result = openConnection.BuildInfoKeywords()
Else
If Not openConnection.SupportSchemaRowset(schema) Then
Using Dim iDBSchemaRowsetWrapper As IDBSchemaRowsetWrapper = openConnection.IDBSchemaRowset()
If iDBSchemaRowsetWrapper.Value Is Nothing Then
Throw ODB.SchemaRowsetsNotSupported(Me.Provider)
End If
End Using
Throw ODB.NotSupportedSchemaTable(schema, Me)
End If
result = openConnection.GetSchemaRowset(schema, restrictions)
End If
End If
End If
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Function
Friend Function GetSchemaRowset(schema As Guid, restrictions As Object()) As DataTable
Return Me.GetOpenConnection().GetSchemaRowset(schema, restrictions)
End Function
Friend Function HasLiveReader(cmd As OleDbCommand) As Boolean
Dim result As Boolean = False
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
If openConnection IsNot Nothing Then
result = openConnection.HasLiveReader(cmd)
End If
Return result
End Function
Friend Sub OnInfoMessage(errorInfo As UnsafeNativeMethods.IErrorInfo, errorCode As OleDbHResult)
Dim oleDbInfoMessageEventHandler As OleDbInfoMessageEventHandler = CType(MyBase.Events(OleDbConnection.EventInfoMessage), OleDbInfoMessageEventHandler)
If oleDbInfoMessageEventHandler IsNot Nothing Then
Try
Dim exception As OleDbException = OleDbException.CreateException(errorInfo, errorCode, Nothing)
Dim oleDbInfoMessageEventArgs As OleDbInfoMessageEventArgs = New OleDbInfoMessageEventArgs(exception)
If Bid.TraceOn Then
Bid.Trace("<oledb.OledbConnection.OnInfoMessage|API|INFO> %d#, Message='%ls'" & vbLf, Me.ObjectID, oleDbInfoMessageEventArgs.Message)
End If
oleDbInfoMessageEventHandler(Me, oleDbInfoMessageEventArgs)
Catch e As Exception
If Not ADP.IsCatchableOrSecurityExceptionType(e) Then
Throw
End If
ADP.TraceExceptionWithoutRethrow(e)
End Try
End If
End Sub
Public Override Sub Open()
Me.InnerConnection.OpenConnection(Me, Me.ConnectionFactory)
If(2 And (CType(Me.ConnectionOptions, OleDbConnectionString)).OleDbServices) <> 0 AndAlso ADP.NeedManualEnlistment() Then
Me.GetOpenConnection().EnlistTransactionInternal(Transaction.Current, True)
End If
End Sub
Friend Sub SetDataSourcePropertyValue(propertySet As Guid, propertyID As Integer, description As String, required As Boolean, value As Object)
Me.CheckStateOpen("SetProperties")
Using Dim iDBPropertiesWrapper As IDBPropertiesWrapper = Me.IDBProperties()
Using Dim dBPropSet As DBPropSet = DBPropSet.CreateProperty(propertySet, propertyID, required, value)
Bid.Trace("<oledb.IDBProperties.SetProperties|API|OLEDB> %d#" & vbLf, Me.ObjectID)
Dim oleDbHResult As OleDbHResult = iDBPropertiesWrapper.Value.SetProperties(dBPropSet.PropertySetCount, dBPropSet)
Bid.Trace("<oledb.IDBProperties.SetProperties|API|OLEDB|RET> %08X{HRESULT}" & vbLf, oleDbHResult)
If oleDbHResult < OleDbHResult.S_OK Then
Dim ex As Exception = OleDbConnection.ProcessResults(oleDbHResult, Nothing, Me)
If OleDbHResult.DB_E_ERRORSOCCURRED = oleDbHResult Then
Dim stringBuilder As StringBuilder = New StringBuilder()
Dim propertySet2 As tagDBPROP() = dBPropSet.GetPropertySet(0, propertySet)
ODB.PropsetSetFailure(stringBuilder, description, propertySet2(0).dwStatus)
ex = ODB.PropsetSetFailure(stringBuilder.ToString(), ex)
End If
If ex IsNot Nothing Then
Throw ex
End If
Else
SafeNativeMethods.Wrapper.ClearErrorInfo()
End If
End Using
End Using
End Sub
Friend Function SupportSchemaRowset(schema As Guid) As Boolean
Return Me.GetOpenConnection().SupportSchemaRowset(schema)
End Function
Friend Function ValidateTransaction(transaction As OleDbTransaction, method As String) As OleDbTransaction
Return Me.GetOpenConnection().ValidateTransaction(transaction, method)
End Function
Friend Shared Function ProcessResults(hresult As OleDbHResult, connection As OleDbConnection, src As Object) As Exception
If OleDbHResult.S_OK <= hresult AndAlso (connection Is Nothing OrElse connection.Events(OleDbConnection.EventInfoMessage) Is Nothing) Then
SafeNativeMethods.Wrapper.ClearErrorInfo()
Return Nothing
End If
Dim ex As Exception = Nothing
Dim errorInfo As UnsafeNativeMethods.IErrorInfo = Nothing
If UnsafeNativeMethods.GetErrorInfo(0, errorInfo) = OleDbHResult.S_OK AndAlso errorInfo IsNot Nothing Then
If hresult < OleDbHResult.S_OK Then
ex = OleDbException.CreateException(errorInfo, hresult, Nothing)
If OleDbHResult.DB_E_OBJECTOPEN = hresult Then
ex = ADP.OpenReaderExists(ex)
End If
OleDbConnection.ResetState(connection)
Else
If connection IsNot Nothing Then
connection.OnInfoMessage(errorInfo, hresult)
Else
Bid.Trace("<oledb.OledbConnection|WARN|INFO> ErrorInfo available, but not connection %08X{HRESULT}" & vbLf, hresult)
End If
End If
Marshal.ReleaseComObject(errorInfo)
Else
If OleDbHResult.S_OK < hresult Then
Bid.Trace("<oledb.OledbConnection|ERR|INFO> ErrorInfo not available %08X{HRESULT}" & vbLf, hresult)
Else
If hresult < OleDbHResult.S_OK Then
ex = ODB.NoErrorInformation(If((connection IsNot Nothing), connection.Provider, Nothing), hresult, Nothing)
OleDbConnection.ResetState(connection)
End If
End If
End If
If ex IsNot Nothing Then
ADP.TraceExceptionAsReturnValue(ex)
End If
Return ex
End Function
Public Shared Sub ReleaseObjectPool()
New OleDbPermission(PermissionState.Unrestricted).Demand()
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.ReleaseObjectPool|API>" & vbLf)
Try
OleDbConnectionString.ReleaseObjectPool()
OleDbConnectionInternal.ReleaseObjectPool()
OleDbConnectionFactory.SingletonInstance.ClearAllPools()
Finally
Bid.ScopeLeave(intPtr)
End Try
End Sub
Private Shared Sub ResetState(connection As OleDbConnection)
If connection IsNot Nothing Then
connection.ResetState()
End If
End Sub
Public Sub New()
GC.SuppressFinalize(Me)
Me._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance
End Sub
Private Sub CopyFrom(connection As OleDbConnection)
ADP.CheckArgumentNull(connection, "connection")
Me._userConnectionOptions = connection.UserConnectionOptions
Me._poolGroup = connection.PoolGroup
If DbConnectionClosedNeverOpened.SingletonInstance Is connection._innerConnection Then
Me._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance
Return
End If
Me._innerConnection = DbConnectionClosedPreviouslyOpened.SingletonInstance
End Sub
Private Function ConnectionString_Get() As String
Bid.Trace("<prov.DbConnectionHelper.ConnectionString_Get|API> %d#" & vbLf, Me.ObjectID)
Dim shouldHidePassword As Boolean = Me.InnerConnection.ShouldHidePassword
Dim userConnectionOptions As DbConnectionOptions = Me.UserConnectionOptions
If userConnectionOptions Is Nothing Then
Return""
End If
Return userConnectionOptions.UsersConnectionString(shouldHidePassword)
End Function
Private Sub ConnectionString_Set(value As String)
Dim dbConnectionOptions As DbConnectionOptions = Nothing
Dim connectionPoolGroup As DbConnectionPoolGroup = Me.ConnectionFactory.GetConnectionPoolGroup(value, Nothing, dbConnectionOptions)
Dim innerConnection As DbConnectionInternal = Me.InnerConnection
Dim flag As Boolean = innerConnection.AllowSetConnectionString
If flag Then
flag = Me.SetInnerConnectionFrom(DbConnectionClosedBusy.SingletonInstance, innerConnection)
If flag Then
Me._userConnectionOptions = dbConnectionOptions
Me._poolGroup = connectionPoolGroup
Me._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance
End If
End If
If Not flag Then
Throw ADP.OpenConnectionPropertySet("ConnectionString", innerConnection.State)
End If
If Bid.TraceOn Then
Dim a As String = If((dbConnectionOptions IsNot Nothing), dbConnectionOptions.UsersConnectionStringForTrace(), "")
Bid.Trace("<prov.DbConnectionHelper.ConnectionString_Set|API> %d#, '%ls'" & vbLf, Me.ObjectID, a)
End If
End Sub
<ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)>
Friend Sub Abort(e As Exception)
Dim innerConnection As DbConnectionInternal = Me._innerConnection
If ConnectionState.Open = innerConnection.State Then
Interlocked.CompareExchange(Of DbConnectionInternal)(Me._innerConnection, DbConnectionClosedPreviouslyOpened.SingletonInstance, innerConnection)
innerConnection.DoomThisConnection()
End If
If TypeOf e Is OutOfMemoryException Then
Bid.Trace("<prov.DbConnectionHelper.Abort|RES|INFO|CPOOL> %d#, Aborting operation due to asynchronous exception: %ls" & vbLf, Me.ObjectID, "OutOfMemory")
Return
End If
Bid.Trace("<prov.DbConnectionHelper.Abort|RES|INFO|CPOOL> %d#, Aborting operation due to asynchronous exception: %ls" & vbLf, Me.ObjectID, e.ToString())
End Sub
Friend Sub AddWeakReference(value As Object, tag As Integer)
Me.InnerConnection.AddWeakReference(value, tag)
End Sub
Protected Override Function BeginDbTransaction(isolationLevel As System.Data.IsolationLevel) As DbTransaction
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<prov.DbConnectionHelper.BeginDbTransaction|API> %d#, isolationLevel=%d{ds.IsolationLevel}", Me.ObjectID, CInt(isolationLevel))
Dim result As DbTransaction
Try
Dim dbTransaction As DbTransaction = Me.InnerConnection.BeginTransaction(isolationLevel)
result = dbTransaction
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Function
Protected Override Function CreateDbCommand() As DbCommand
Dim dbCommand As DbCommand = Nothing
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<prov.DbConnectionHelper.CreateDbCommand|API> %d#" & vbLf, Me.ObjectID)
Try
Dim providerFactory As DbProviderFactory = Me.ConnectionFactory.ProviderFactory
dbCommand = providerFactory.CreateCommand()
dbCommand.Connection = Me
Finally
Bid.ScopeLeave(intPtr)
End Try
Return dbCommand
End Function
Private Shared Function CreateExecutePermission() As CodeAccessPermission
Dim dBDataPermission As DBDataPermission = CType(OleDbConnectionFactory.SingletonInstance.ProviderFactory.CreatePermission(PermissionState.None), DBDataPermission)
dBDataPermission.Add(String.Empty, String.Empty, KeyRestrictionBehavior.AllowOnly)
Return dBDataPermission
End Function
Protected Override Sub Dispose(disposing As Boolean)
If disposing Then
Me._userConnectionOptions = Nothing
Me._poolGroup = Nothing
Me.Close()
End If
Me.DisposeMe(disposing)
MyBase.Dispose(disposing)
End Sub
Private Sub EnlistDistributedTransactionHelper(transaction As ITransaction)
Dim permissionSet As PermissionSet = New PermissionSet(PermissionState.None)
permissionSet.AddPermission(OleDbConnection.ExecutePermission)
permissionSet.AddPermission(New SecurityPermission(SecurityPermissionFlag.UnmanagedCode))
permissionSet.Demand()
Bid.Trace("<prov.DbConnectionHelper.EnlistDistributedTransactionHelper|RES|TRAN> %d#, Connection enlisting in a transaction." & vbLf, Me.ObjectID)
Dim transaction2 As Transaction = Nothing
If transaction IsNot Nothing Then
transaction2 = TransactionInterop.GetTransactionFromDtcTransaction(CType(transaction, IDtcTransaction))
End If
Me.InnerConnection.EnlistTransaction(transaction2)
GC.KeepAlive(Me)
End Sub
Public Override Sub EnlistTransaction(transaction As Transaction)
OleDbConnection.ExecutePermission.Demand()
Bid.Trace("<prov.DbConnectionHelper.EnlistTransaction|RES|TRAN> %d#, Connection enlisting in a transaction." & vbLf, Me.ObjectID)
Dim innerConnection As DbConnectionInternal = Me.InnerConnection
If Not innerConnection.HasEnlistedTransaction Then
innerConnection.EnlistTransaction(transaction)
GC.KeepAlive(Me)
Return
End If
If innerConnection.EnlistedTransaction.Equals(transaction) Then
Return
End If
Throw ADP.TransactionPresent()
End Sub
Private Function GetMetaDataFactory(internalConnection As DbConnectionInternal) As DbMetaDataFactory
Return Me.ConnectionFactory.GetMetaDataFactory(Me._poolGroup, internalConnection)
End Function
Friend Function GetMetaDataFactoryInternal(internalConnection As DbConnectionInternal) As DbMetaDataFactory
Return Me.GetMetaDataFactory(internalConnection)
End Function
Public Override Function GetSchema() As DataTable
Return Me.GetSchema(DbMetaDataCollectionNames.MetaDataCollections, Nothing)
End Function
Public Override Function GetSchema(collectionName As String) As DataTable
Return Me.GetSchema(collectionName, Nothing)
End Function
Public Override Function GetSchema(collectionName As String, restrictionValues As String()) As DataTable
OleDbConnection.ExecutePermission.Demand()
Return Me.InnerConnection.GetSchema(Me.ConnectionFactory, Me.PoolGroup, Me, collectionName, restrictionValues)
End Function
Friend Sub NotifyWeakReference(message As Integer)
Me.InnerConnection.NotifyWeakReference(message)
End Sub
Friend Sub PermissionDemand()
Dim poolGroup As DbConnectionPoolGroup = Me.PoolGroup
Dim dbConnectionOptions As DbConnectionOptions = If((poolGroup IsNot Nothing), poolGroup.ConnectionOptions, Nothing)
If dbConnectionOptions Is Nothing OrElse dbConnectionOptions.IsEmpty Then
Throw ADP.NoConnectionString()
End If
Dim userConnectionOptions As DbConnectionOptions = Me.UserConnectionOptions
userConnectionOptions.DemandPermission()
End Sub
Friend Sub RemoveWeakReference(value As Object)
Me.InnerConnection.RemoveWeakReference(value)
End Sub
Friend Sub SetInnerConnectionEvent([to] As DbConnectionInternal)
Dim connectionState As ConnectionState = Me._innerConnection.State And ConnectionState.Open
Dim connectionState2 As ConnectionState = [to].State And ConnectionState.Open
If connectionState <> connectionState2 AndAlso connectionState2 = ConnectionState.Closed Then
Me._closeCount += 1
End If
Me._innerConnection = [to]
If connectionState = ConnectionState.Closed AndAlso ConnectionState.Open = connectionState2 Then
Me.OnStateChange(DbConnectionInternal.StateChangeOpen)
Return
End If
If ConnectionState.Open = connectionState AndAlso connectionState2 = ConnectionState.Closed Then
Me.OnStateChange(DbConnectionInternal.StateChangeClosed)
Return
End If
If connectionState <> connectionState2 Then
Me.OnStateChange(New StateChangeEventArgs(connectionState, connectionState2))
End If
End Sub
Friend Function SetInnerConnectionFrom([to] As DbConnectionInternal, from As DbConnectionInternal) As Boolean
Return from Is Interlocked.CompareExchange(Of DbConnectionInternal)(Me._innerConnection, [to], from)
End Function
Friend Sub SetInnerConnectionTo([to] As DbConnectionInternal)
Me._innerConnection = [to]
End Sub
<Conditional("DEBUG")>
Friend Shared Sub VerifyExecutePermission()
Try
OleDbConnection.ExecutePermission.Demand()
Catch ex_0C As SecurityException
Throw
End Try
End Sub
End Class
End Namespace
Imports System.ComponentModel
Imports System.Data.Common
Imports System.Data.ProviderBase
Imports System.Diagnostics
Imports System.EnterpriseServices
Imports System.Globalization
Imports System.Runtime.ConstrainedExecution
Imports System.Runtime.InteropServices
Imports System.Security
Imports System.Security.Permissions
Imports System.Text
Imports System.Threading
Imports System.Transactions
Namespace System.Data.OleDb
<DefaultEvent("InfoMessage")>
Public Class OleDbConnection
Inherits DbConnection
Implements ICloneable, IDbConnection, IDisposable
Private Shared EventInfoMessage As Object = New Object()
Private Shared _connectionFactory As DbConnectionFactory = OleDbConnectionFactory.SingletonInstance
Friend Shared ExecutePermission As CodeAccessPermission = OleDbConnection.CreateExecutePermission()
Private _userConnectionOptions As DbConnectionOptions
Private _poolGroup As DbConnectionPoolGroup
Private _innerConnection As DbConnectionInternal
Private _closeCount As Integer
Private Shared _objectTypeCount As Integer
Friend ObjectID As Integer = Interlocked.Increment(OleDbConnection._objectTypeCount)
<ResCategory("DataCategory_InfoMessage"), ResDescription("DbConnection_InfoMessage")>
Public Custom Event InfoMessage As OleDbInfoMessageEventHandler
AddHandler
MyBase.Events.[AddHandler](OleDbConnection.EventInfoMessage, value)
End AddHandler
RemoveHandler
MyBase.Events.[RemoveHandler](OleDbConnection.EventInfoMessage, value)
End RemoveHandler
End Event
<DefaultValue(""), Editor("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), RecommendedAsConfigurable(True), RefreshProperties(RefreshProperties.All), ResCategory("DataCategory_Data"), ResDescription("OleDbConnection_ConnectionString")>
Public Override Property ConnectionString() As String
Get
Return Me.ConnectionString_Get()
End Get
Set(value As String)
Me.ConnectionString_Set(value)
End Set
End Property
Private ReadOnly Property OleDbConnectionStringValue() As OleDbConnectionString
Get
Return CType(Me.ConnectionOptions, OleDbConnectionString)
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), ResDescription("OleDbConnection_ConnectionTimeout")>
Public Override ReadOnly Property ConnectionTimeout() As Integer
Get
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.get_ConnectionTimeout|API> %d#" & vbLf, Me.ObjectID)
Dim result As Integer
Try
Dim obj As Object
If Me.IsOpen Then
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DBInit, 66)
Else
Dim oleDbConnectionStringValue As OleDbConnectionString = Me.OleDbConnectionStringValue
obj = (If((oleDbConnectionStringValue IsNot Nothing), oleDbConnectionStringValue.ConnectTimeout, 15))
End If
If obj IsNot Nothing Then
result = Convert.ToInt32(obj, CultureInfo.InvariantCulture)
Else
result = 15
End If
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), ResDescription("OleDbConnection_Database")>
Public Override ReadOnly Property Database() As String
Get
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.get_Database|API> %d#" & vbLf, Me.ObjectID)
Dim result As String
Try
Dim oleDbConnectionString As OleDbConnectionString = CType(Me.UserConnectionOptions, OleDbConnectionString)
Dim obj As Object = If((oleDbConnectionString IsNot Nothing), oleDbConnectionString.InitialCatalog, ADP.StrEmpty)
If obj IsNot Nothing AndAlso Not(CStr(obj)).StartsWith("|datadirectory|", StringComparison.OrdinalIgnoreCase) Then
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
If openConnection IsNot Nothing Then
If openConnection.HasSession Then
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DataSource, 37)
Else
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DBInit, 233)
End If
Else
oleDbConnectionString = Me.OleDbConnectionStringValue
obj = (If((oleDbConnectionString IsNot Nothing), oleDbConnectionString.InitialCatalog, ADP.StrEmpty))
End If
End If
result = Convert.ToString(obj, CultureInfo.InvariantCulture)
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Get
End Property
<Browsable(True), ResDescription("OleDbConnection_DataSource")>
Public Override ReadOnly Property DataSource() As String
Get
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.get_DataSource|API> %d#" & vbLf, Me.ObjectID)
Dim result As String
Try
Dim oleDbConnectionString As OleDbConnectionString = CType(Me.UserConnectionOptions, OleDbConnectionString)
Dim obj As Object = If((oleDbConnectionString IsNot Nothing), oleDbConnectionString.DataSource, ADP.StrEmpty)
If obj IsNot Nothing AndAlso Not(CStr(obj)).StartsWith("|datadirectory|", StringComparison.OrdinalIgnoreCase) Then
If Me.IsOpen Then
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DBInit, 59)
If obj Is Nothing OrElse (TypeOf obj Is String AndAlso (TryCast(obj, String)).Length = 0) Then
obj = Me.GetDataSourceValue(OleDbPropertySetGuid.DataSourceInfo, 38)
End If
Else
oleDbConnectionString = Me.OleDbConnectionStringValue
obj = (If((oleDbConnectionString IsNot Nothing), oleDbConnectionString.DataSource, ADP.StrEmpty))
End If
End If
result = Convert.ToString(obj, CultureInfo.InvariantCulture)
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Get
End Property
Friend ReadOnly Property IsOpen() As Boolean
Get
Return Nothing IsNot Me.GetOpenConnection()
End Get
End Property
Friend WriteOnly Property LocalTransaction() As OleDbTransaction
Set(value As OleDbTransaction)
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
If openConnection IsNot Nothing Then
openConnection.LocalTransaction = value
End If
End Set
End Property
<Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), ResCategory("DataCategory_Data"), ResDescription("OleDbConnection_Provider")>
Public ReadOnly Property Provider() As String
Get
Bid.Trace("<oledb.OleDbConnection.get_Provider|API> %d#" & vbLf, Me.ObjectID)
Dim oleDbConnectionStringValue As OleDbConnectionString = Me.OleDbConnectionStringValue
Dim text As String = If((oleDbConnectionStringValue IsNot Nothing), oleDbConnectionStringValue.ConvertValueToString("provider", Nothing), Nothing)
If text Is Nothing Then
Return ADP.StrEmpty
End If
Return text
End Get
End Property
Friend ReadOnly Property ProviderInfo() As OleDbConnectionPoolGroupProviderInfo
Get
Return CType(Me.PoolGroup.ProviderInfo, OleDbConnectionPoolGroupProviderInfo)
End Get
End Property
<ResDescription("OleDbConnection_ServerVersion")>
Public Override ReadOnly Property ServerVersion() As String
Get
Return Me.InnerConnection.ServerVersion
End Get
End Property
Friend ReadOnly Property CloseCount() As Integer
Get
Return Me._closeCount
End Get
End Property
Friend ReadOnly Property ConnectionFactory() As DbConnectionFactory
Get
Return OleDbConnection._connectionFactory
End Get
End Property
Friend ReadOnly Property ConnectionOptions() As DbConnectionOptions
Get
Dim poolGroup As DbConnectionPoolGroup = Me.PoolGroup
If poolGroup Is Nothing Then
Return Nothing
End If
Return poolGroup.ConnectionOptions
End Get
End Property
Friend ReadOnly Property InnerConnection() As DbConnectionInternal
Get
Return Me._innerConnection
End Get
End Property
Friend Property PoolGroup() As DbConnectionPoolGroup
Get
Return Me._poolGroup
End Get
Set(value As DbConnectionPoolGroup)
Me._poolGroup = value
End Set
End Property
<Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), ResDescription("DbConnection_State")>
Public Override ReadOnly Property State() As ConnectionState
Get
Return Me.InnerConnection.State
End Get
End Property
Friend ReadOnly Property UserConnectionOptions() As DbConnectionOptions
Get
Return Me._userConnectionOptions
End Get
End Property
Public Sub New(connectionString As String)
Me.[New]()
Me.ConnectionString = connectionString
End Sub
Private Sub New(connection As OleDbConnection)
Me.[New]()
Me.CopyFrom(connection)
End Sub
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Sub ResetState()
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbCommand.ResetState|API> %d#" & vbLf, Me.ObjectID)
Try
If Me.IsOpen Then
Dim dataSourcePropertyValue As Object = Me.GetDataSourcePropertyValue(OleDbPropertySetGuid.DataSourceInfo, 244)
If TypeOf dataSourcePropertyValue Is Integer Then
Select Case CInt(dataSourcePropertyValue)
Case 0, 2
Me.GetOpenConnection().DoomThisConnection()
Me.NotifyWeakReference(-1)
Me.Close()
End Select
End If
End If
Finally
Bid.ScopeLeave(intPtr)
End Try
End Sub
Friend Function ICommandText() As UnsafeNativeMethods.ICommandText
Return Me.GetOpenConnection().ICommandText()
End Function
Private Function IDBProperties() As IDBPropertiesWrapper
Return Me.GetOpenConnection().IDBProperties()
End Function
Friend Function IOpenRowset() As IOpenRowsetWrapper
Return Me.GetOpenConnection().IOpenRowset()
End Function
Friend Function SqlSupport() As Integer
Return Me.OleDbConnectionStringValue.GetSqlSupport(Me)
End Function
Friend Function SupportMultipleResults() As Boolean
Return Me.OleDbConnectionStringValue.GetSupportMultipleResults(Me)
End Function
Friend Function SupportIRow(cmd As OleDbCommand) As Boolean
Return Me.OleDbConnectionStringValue.GetSupportIRow(Me, cmd)
End Function
Friend Function QuotedIdentifierCase() As Integer
Dim dataSourcePropertyValue As Object = Me.GetDataSourcePropertyValue(OleDbPropertySetGuid.DataSourceInfo, 100)
Dim result As Integer
If TypeOf dataSourcePropertyValue Is Integer Then
result = CInt(dataSourcePropertyValue)
Else
result = -1
End If
Return result
End Function
Public Function BeginTransaction() As OleDbTransaction
Return Me.BeginTransaction(System.Data.IsolationLevel.Unspecified)
End Function
Public Function BeginTransaction(isolationLevel As System.Data.IsolationLevel) As OleDbTransaction
Return CType(Me.InnerConnection.BeginTransaction(isolationLevel), OleDbTransaction)
End Function
Public Override Sub ChangeDatabase(value As String)
OleDbConnection.ExecutePermission.Demand()
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.ChangeDatabase|API> %d#, value='%ls'" & vbLf, Me.ObjectID, value)
Try
Me.CheckStateOpen("ChangeDatabase")
If value Is Nothing OrElse value.Trim().Length = 0 Then
Throw ADP.EmptyDatabaseName()
End If
Me.SetDataSourcePropertyValue(OleDbPropertySetGuid.DataSource, 37, "current catalog", True, value)
Finally
Bid.ScopeLeave(intPtr)
End Try
End Sub
Friend Sub CheckStateOpen(method As String)
Dim state As ConnectionState = Me.State
If ConnectionState.Open <> state Then
Throw ADP.OpenConnectionRequired(method, state)
End If
End Sub
Function Clone() As Object Implements ICloneable.Clone
Dim oleDbConnection As OleDbConnection = New OleDbConnection(Me)
Bid.Trace("<oledb.OleDbConnection.Clone|API> %d#, clone=%d#" & vbLf, Me.ObjectID, oleDbConnection.ObjectID)
Return oleDbConnection
End Function
Public Override Sub Close()
Me.InnerConnection.CloseConnection(Me, Me.ConnectionFactory)
End Sub
Public Function CreateCommand() As OleDbCommand
Return New OleDbCommand("", Me)
End Function
Private Sub DisposeMe(disposing As Boolean)
If disposing AndAlso MyBase.DesignMode Then
OleDbConnection.ReleaseObjectPool()
End If
End Sub
Public Sub EnlistDistributedTransaction(transaction As ITransaction)
Me.EnlistDistributedTransactionHelper(transaction)
End Sub
Friend Function GetDataSourcePropertyValue(propertySet As Guid, propertyID As Integer) As Object
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
Return openConnection.GetDataSourcePropertyValue(propertySet, propertyID)
End Function
Friend Function GetDataSourceValue(propertySet As Guid, propertyID As Integer) As Object
Dim obj As Object = Me.GetDataSourcePropertyValue(propertySet, propertyID)
If TypeOf obj Is OleDbPropertyStatus OrElse Convert.IsDBNull(obj) Then
obj = Nothing
End If
Return obj
End Function
Private Function GetOpenConnection() As OleDbConnectionInternal
Dim innerConnection As DbConnectionInternal = Me.InnerConnection
Return TryCast(innerConnection, OleDbConnectionInternal)
End Function
Friend Sub GetLiteralQuotes(method As String, <Out()> ByRef quotePrefix As String, <Out()> ByRef quoteSuffix As String)
Me.CheckStateOpen(method)
Dim providerInfo As OleDbConnectionPoolGroupProviderInfo = Me.ProviderInfo
If providerInfo.HasQuoteFix Then
quotePrefix = providerInfo.QuotePrefix
quoteSuffix = providerInfo.QuoteSuffix
Return
End If
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
quotePrefix = openConnection.GetLiteralInfo(15)
quoteSuffix = openConnection.GetLiteralInfo(28)
If quotePrefix Is Nothing Then
quotePrefix = ""
End If
If quoteSuffix Is Nothing Then
quoteSuffix = quotePrefix
End If
providerInfo.SetQuoteFix(quotePrefix, quoteSuffix)
End Sub
Public Function GetOleDbSchemaTable(schema As Guid, restrictions As Object()) As DataTable
OleDbConnection.ExecutePermission.Demand()
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.GetOleDbSchemaTable|API> %d#, schema=%p{GUID}, restrictions" & vbLf, Me.ObjectID, schema)
Dim result As DataTable
Try
Me.CheckStateOpen("GetOleDbSchemaTable")
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
If OleDbSchemaGuid.DbInfoLiterals = schema Then
If restrictions IsNot Nothing AndAlso restrictions.Length <> 0 Then
Throw ODB.InvalidRestrictionsDbInfoLiteral("restrictions")
End If
result = openConnection.BuildInfoLiterals()
Else
If OleDbSchemaGuid.SchemaGuids = schema Then
If restrictions IsNot Nothing AndAlso restrictions.Length <> 0 Then
Throw ODB.InvalidRestrictionsSchemaGuids("restrictions")
End If
result = openConnection.BuildSchemaGuids()
Else
If OleDbSchemaGuid.DbInfoKeywords = schema Then
If restrictions IsNot Nothing AndAlso restrictions.Length <> 0 Then
Throw ODB.InvalidRestrictionsDbInfoKeywords("restrictions")
End If
result = openConnection.BuildInfoKeywords()
Else
If Not openConnection.SupportSchemaRowset(schema) Then
Using Dim iDBSchemaRowsetWrapper As IDBSchemaRowsetWrapper = openConnection.IDBSchemaRowset()
If iDBSchemaRowsetWrapper.Value Is Nothing Then
Throw ODB.SchemaRowsetsNotSupported(Me.Provider)
End If
End Using
Throw ODB.NotSupportedSchemaTable(schema, Me)
End If
result = openConnection.GetSchemaRowset(schema, restrictions)
End If
End If
End If
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Function
Friend Function GetSchemaRowset(schema As Guid, restrictions As Object()) As DataTable
Return Me.GetOpenConnection().GetSchemaRowset(schema, restrictions)
End Function
Friend Function HasLiveReader(cmd As OleDbCommand) As Boolean
Dim result As Boolean = False
Dim openConnection As OleDbConnectionInternal = Me.GetOpenConnection()
If openConnection IsNot Nothing Then
result = openConnection.HasLiveReader(cmd)
End If
Return result
End Function
Friend Sub OnInfoMessage(errorInfo As UnsafeNativeMethods.IErrorInfo, errorCode As OleDbHResult)
Dim oleDbInfoMessageEventHandler As OleDbInfoMessageEventHandler = CType(MyBase.Events(OleDbConnection.EventInfoMessage), OleDbInfoMessageEventHandler)
If oleDbInfoMessageEventHandler IsNot Nothing Then
Try
Dim exception As OleDbException = OleDbException.CreateException(errorInfo, errorCode, Nothing)
Dim oleDbInfoMessageEventArgs As OleDbInfoMessageEventArgs = New OleDbInfoMessageEventArgs(exception)
If Bid.TraceOn Then
Bid.Trace("<oledb.OledbConnection.OnInfoMessage|API|INFO> %d#, Message='%ls'" & vbLf, Me.ObjectID, oleDbInfoMessageEventArgs.Message)
End If
oleDbInfoMessageEventHandler(Me, oleDbInfoMessageEventArgs)
Catch e As Exception
If Not ADP.IsCatchableOrSecurityExceptionType(e) Then
Throw
End If
ADP.TraceExceptionWithoutRethrow(e)
End Try
End If
End Sub
Public Override Sub Open()
Me.InnerConnection.OpenConnection(Me, Me.ConnectionFactory)
If(2 And (CType(Me.ConnectionOptions, OleDbConnectionString)).OleDbServices) <> 0 AndAlso ADP.NeedManualEnlistment() Then
Me.GetOpenConnection().EnlistTransactionInternal(Transaction.Current, True)
End If
End Sub
Friend Sub SetDataSourcePropertyValue(propertySet As Guid, propertyID As Integer, description As String, required As Boolean, value As Object)
Me.CheckStateOpen("SetProperties")
Using Dim iDBPropertiesWrapper As IDBPropertiesWrapper = Me.IDBProperties()
Using Dim dBPropSet As DBPropSet = DBPropSet.CreateProperty(propertySet, propertyID, required, value)
Bid.Trace("<oledb.IDBProperties.SetProperties|API|OLEDB> %d#" & vbLf, Me.ObjectID)
Dim oleDbHResult As OleDbHResult = iDBPropertiesWrapper.Value.SetProperties(dBPropSet.PropertySetCount, dBPropSet)
Bid.Trace("<oledb.IDBProperties.SetProperties|API|OLEDB|RET> %08X{HRESULT}" & vbLf, oleDbHResult)
If oleDbHResult < OleDbHResult.S_OK Then
Dim ex As Exception = OleDbConnection.ProcessResults(oleDbHResult, Nothing, Me)
If OleDbHResult.DB_E_ERRORSOCCURRED = oleDbHResult Then
Dim stringBuilder As StringBuilder = New StringBuilder()
Dim propertySet2 As tagDBPROP() = dBPropSet.GetPropertySet(0, propertySet)
ODB.PropsetSetFailure(stringBuilder, description, propertySet2(0).dwStatus)
ex = ODB.PropsetSetFailure(stringBuilder.ToString(), ex)
End If
If ex IsNot Nothing Then
Throw ex
End If
Else
SafeNativeMethods.Wrapper.ClearErrorInfo()
End If
End Using
End Using
End Sub
Friend Function SupportSchemaRowset(schema As Guid) As Boolean
Return Me.GetOpenConnection().SupportSchemaRowset(schema)
End Function
Friend Function ValidateTransaction(transaction As OleDbTransaction, method As String) As OleDbTransaction
Return Me.GetOpenConnection().ValidateTransaction(transaction, method)
End Function
Friend Shared Function ProcessResults(hresult As OleDbHResult, connection As OleDbConnection, src As Object) As Exception
If OleDbHResult.S_OK <= hresult AndAlso (connection Is Nothing OrElse connection.Events(OleDbConnection.EventInfoMessage) Is Nothing) Then
SafeNativeMethods.Wrapper.ClearErrorInfo()
Return Nothing
End If
Dim ex As Exception = Nothing
Dim errorInfo As UnsafeNativeMethods.IErrorInfo = Nothing
If UnsafeNativeMethods.GetErrorInfo(0, errorInfo) = OleDbHResult.S_OK AndAlso errorInfo IsNot Nothing Then
If hresult < OleDbHResult.S_OK Then
ex = OleDbException.CreateException(errorInfo, hresult, Nothing)
If OleDbHResult.DB_E_OBJECTOPEN = hresult Then
ex = ADP.OpenReaderExists(ex)
End If
OleDbConnection.ResetState(connection)
Else
If connection IsNot Nothing Then
connection.OnInfoMessage(errorInfo, hresult)
Else
Bid.Trace("<oledb.OledbConnection|WARN|INFO> ErrorInfo available, but not connection %08X{HRESULT}" & vbLf, hresult)
End If
End If
Marshal.ReleaseComObject(errorInfo)
Else
If OleDbHResult.S_OK < hresult Then
Bid.Trace("<oledb.OledbConnection|ERR|INFO> ErrorInfo not available %08X{HRESULT}" & vbLf, hresult)
Else
If hresult < OleDbHResult.S_OK Then
ex = ODB.NoErrorInformation(If((connection IsNot Nothing), connection.Provider, Nothing), hresult, Nothing)
OleDbConnection.ResetState(connection)
End If
End If
End If
If ex IsNot Nothing Then
ADP.TraceExceptionAsReturnValue(ex)
End If
Return ex
End Function
Public Shared Sub ReleaseObjectPool()
New OleDbPermission(PermissionState.Unrestricted).Demand()
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<oledb.OleDbConnection.ReleaseObjectPool|API>" & vbLf)
Try
OleDbConnectionString.ReleaseObjectPool()
OleDbConnectionInternal.ReleaseObjectPool()
OleDbConnectionFactory.SingletonInstance.ClearAllPools()
Finally
Bid.ScopeLeave(intPtr)
End Try
End Sub
Private Shared Sub ResetState(connection As OleDbConnection)
If connection IsNot Nothing Then
connection.ResetState()
End If
End Sub
Public Sub New()
GC.SuppressFinalize(Me)
Me._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance
End Sub
Private Sub CopyFrom(connection As OleDbConnection)
ADP.CheckArgumentNull(connection, "connection")
Me._userConnectionOptions = connection.UserConnectionOptions
Me._poolGroup = connection.PoolGroup
If DbConnectionClosedNeverOpened.SingletonInstance Is connection._innerConnection Then
Me._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance
Return
End If
Me._innerConnection = DbConnectionClosedPreviouslyOpened.SingletonInstance
End Sub
Private Function ConnectionString_Get() As String
Bid.Trace("<prov.DbConnectionHelper.ConnectionString_Get|API> %d#" & vbLf, Me.ObjectID)
Dim shouldHidePassword As Boolean = Me.InnerConnection.ShouldHidePassword
Dim userConnectionOptions As DbConnectionOptions = Me.UserConnectionOptions
If userConnectionOptions Is Nothing Then
Return""
End If
Return userConnectionOptions.UsersConnectionString(shouldHidePassword)
End Function
Private Sub ConnectionString_Set(value As String)
Dim dbConnectionOptions As DbConnectionOptions = Nothing
Dim connectionPoolGroup As DbConnectionPoolGroup = Me.ConnectionFactory.GetConnectionPoolGroup(value, Nothing, dbConnectionOptions)
Dim innerConnection As DbConnectionInternal = Me.InnerConnection
Dim flag As Boolean = innerConnection.AllowSetConnectionString
If flag Then
flag = Me.SetInnerConnectionFrom(DbConnectionClosedBusy.SingletonInstance, innerConnection)
If flag Then
Me._userConnectionOptions = dbConnectionOptions
Me._poolGroup = connectionPoolGroup
Me._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance
End If
End If
If Not flag Then
Throw ADP.OpenConnectionPropertySet("ConnectionString", innerConnection.State)
End If
If Bid.TraceOn Then
Dim a As String = If((dbConnectionOptions IsNot Nothing), dbConnectionOptions.UsersConnectionStringForTrace(), "")
Bid.Trace("<prov.DbConnectionHelper.ConnectionString_Set|API> %d#, '%ls'" & vbLf, Me.ObjectID, a)
End If
End Sub
<ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)>
Friend Sub Abort(e As Exception)
Dim innerConnection As DbConnectionInternal = Me._innerConnection
If ConnectionState.Open = innerConnection.State Then
Interlocked.CompareExchange(Of DbConnectionInternal)(Me._innerConnection, DbConnectionClosedPreviouslyOpened.SingletonInstance, innerConnection)
innerConnection.DoomThisConnection()
End If
If TypeOf e Is OutOfMemoryException Then
Bid.Trace("<prov.DbConnectionHelper.Abort|RES|INFO|CPOOL> %d#, Aborting operation due to asynchronous exception: %ls" & vbLf, Me.ObjectID, "OutOfMemory")
Return
End If
Bid.Trace("<prov.DbConnectionHelper.Abort|RES|INFO|CPOOL> %d#, Aborting operation due to asynchronous exception: %ls" & vbLf, Me.ObjectID, e.ToString())
End Sub
Friend Sub AddWeakReference(value As Object, tag As Integer)
Me.InnerConnection.AddWeakReference(value, tag)
End Sub
Protected Override Function BeginDbTransaction(isolationLevel As System.Data.IsolationLevel) As DbTransaction
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<prov.DbConnectionHelper.BeginDbTransaction|API> %d#, isolationLevel=%d{ds.IsolationLevel}", Me.ObjectID, CInt(isolationLevel))
Dim result As DbTransaction
Try
Dim dbTransaction As DbTransaction = Me.InnerConnection.BeginTransaction(isolationLevel)
result = dbTransaction
Finally
Bid.ScopeLeave(intPtr)
End Try
Return result
End Function
Protected Override Function CreateDbCommand() As DbCommand
Dim dbCommand As DbCommand = Nothing
Dim intPtr As IntPtr
Bid.ScopeEnter(intPtr, "<prov.DbConnectionHelper.CreateDbCommand|API> %d#" & vbLf, Me.ObjectID)
Try
Dim providerFactory As DbProviderFactory = Me.ConnectionFactory.ProviderFactory
dbCommand = providerFactory.CreateCommand()
dbCommand.Connection = Me
Finally
Bid.ScopeLeave(intPtr)
End Try
Return dbCommand
End Function
Private Shared Function CreateExecutePermission() As CodeAccessPermission
Dim dBDataPermission As DBDataPermission = CType(OleDbConnectionFactory.SingletonInstance.ProviderFactory.CreatePermission(PermissionState.None), DBDataPermission)
dBDataPermission.Add(String.Empty, String.Empty, KeyRestrictionBehavior.AllowOnly)
Return dBDataPermission
End Function
Protected Override Sub Dispose(disposing As Boolean)
If disposing Then
Me._userConnectionOptions = Nothing
Me._poolGroup = Nothing
Me.Close()
End If
Me.DisposeMe(disposing)
MyBase.Dispose(disposing)
End Sub
Private Sub EnlistDistributedTransactionHelper(transaction As ITransaction)
Dim permissionSet As PermissionSet = New PermissionSet(PermissionState.None)
permissionSet.AddPermission(OleDbConnection.ExecutePermission)
permissionSet.AddPermission(New SecurityPermission(SecurityPermissionFlag.UnmanagedCode))
permissionSet.Demand()
Bid.Trace("<prov.DbConnectionHelper.EnlistDistributedTransactionHelper|RES|TRAN> %d#, Connection enlisting in a transaction." & vbLf, Me.ObjectID)
Dim transaction2 As Transaction = Nothing
If transaction IsNot Nothing Then
transaction2 = TransactionInterop.GetTransactionFromDtcTransaction(CType(transaction, IDtcTransaction))
End If
Me.InnerConnection.EnlistTransaction(transaction2)
GC.KeepAlive(Me)
End Sub
Public Override Sub EnlistTransaction(transaction As Transaction)
OleDbConnection.ExecutePermission.Demand()
Bid.Trace("<prov.DbConnectionHelper.EnlistTransaction|RES|TRAN> %d#, Connection enlisting in a transaction." & vbLf, Me.ObjectID)
Dim innerConnection As DbConnectionInternal = Me.InnerConnection
If Not innerConnection.HasEnlistedTransaction Then
innerConnection.EnlistTransaction(transaction)
GC.KeepAlive(Me)
Return
End If
If innerConnection.EnlistedTransaction.Equals(transaction) Then
Return
End If
Throw ADP.TransactionPresent()
End Sub
Private Function GetMetaDataFactory(internalConnection As DbConnectionInternal) As DbMetaDataFactory
Return Me.ConnectionFactory.GetMetaDataFactory(Me._poolGroup, internalConnection)
End Function
Friend Function GetMetaDataFactoryInternal(internalConnection As DbConnectionInternal) As DbMetaDataFactory
Return Me.GetMetaDataFactory(internalConnection)
End Function
Public Override Function GetSchema() As DataTable
Return Me.GetSchema(DbMetaDataCollectionNames.MetaDataCollections, Nothing)
End Function
Public Override Function GetSchema(collectionName As String) As DataTable
Return Me.GetSchema(collectionName, Nothing)
End Function
Public Override Function GetSchema(collectionName As String, restrictionValues As String()) As DataTable
OleDbConnection.ExecutePermission.Demand()
Return Me.InnerConnection.GetSchema(Me.ConnectionFactory, Me.PoolGroup, Me, collectionName, restrictionValues)
End Function
Friend Sub NotifyWeakReference(message As Integer)
Me.InnerConnection.NotifyWeakReference(message)
End Sub
Friend Sub PermissionDemand()
Dim poolGroup As DbConnectionPoolGroup = Me.PoolGroup
Dim dbConnectionOptions As DbConnectionOptions = If((poolGroup IsNot Nothing), poolGroup.ConnectionOptions, Nothing)
If dbConnectionOptions Is Nothing OrElse dbConnectionOptions.IsEmpty Then
Throw ADP.NoConnectionString()
End If
Dim userConnectionOptions As DbConnectionOptions = Me.UserConnectionOptions
userConnectionOptions.DemandPermission()
End Sub
Friend Sub RemoveWeakReference(value As Object)
Me.InnerConnection.RemoveWeakReference(value)
End Sub
Friend Sub SetInnerConnectionEvent([to] As DbConnectionInternal)
Dim connectionState As ConnectionState = Me._innerConnection.State And ConnectionState.Open
Dim connectionState2 As ConnectionState = [to].State And ConnectionState.Open
If connectionState <> connectionState2 AndAlso connectionState2 = ConnectionState.Closed Then
Me._closeCount += 1
End If
Me._innerConnection = [to]
If connectionState = ConnectionState.Closed AndAlso ConnectionState.Open = connectionState2 Then
Me.OnStateChange(DbConnectionInternal.StateChangeOpen)
Return
End If
If ConnectionState.Open = connectionState AndAlso connectionState2 = ConnectionState.Closed Then
Me.OnStateChange(DbConnectionInternal.StateChangeClosed)
Return
End If
If connectionState <> connectionState2 Then
Me.OnStateChange(New StateChangeEventArgs(connectionState, connectionState2))
End If
End Sub
Friend Function SetInnerConnectionFrom([to] As DbConnectionInternal, from As DbConnectionInternal) As Boolean
Return from Is Interlocked.CompareExchange(Of DbConnectionInternal)(Me._innerConnection, [to], from)
End Function
Friend Sub SetInnerConnectionTo([to] As DbConnectionInternal)
Me._innerConnection = [to]
End Sub
<Conditional("DEBUG")>
Friend Shared Sub VerifyExecutePermission()
Try
OleDbConnection.ExecutePermission.Demand()
Catch ex_0C As SecurityException
Throw
End Try
End Sub
End Class
End Namespace