'需要提供三個參數:
'providerName : 原先的ADO.NET Data Provider名字,如果这种方式可行就优先使用。 providerName :原先的ADO.NET Data Provider名字,如果這種方式可行就優先使用。 可以为null。可以為null。
'assemblyName : 需要动态加载的Data Provider所用的Assembly Name,如“FirebirdSql.Data.FirebirdClient”。 assemblyName :需要動態加載的Data Provider所用的Assembly Name,如“FirebirdSql.Data.FirebirdClient”。
'className : 需要动态加载的Data Provider的class name,如“FirebirdSql.Data.FirebirdClient.FirebirdClientFactory”。 className :需要動態加載的Data Provider的class name,如“FirebirdSql.Data.FirebirdClient.FirebirdClientFactory”。
'当然同时项目需要添加到这个Assembly的引用。當然同時項目需要添加到這個Assembly的引用。
'这样做了之后,系统就能够顺利的动态加载该ADO.NET Data Provider并提供相应的服务。這樣做了之後,系統就能夠順利的動態加載該ADO.NET Data Provider並提供相應的服務。
Public Shared Function GetPublicStaticField(ByVal type As Type, ByVal fieldName As String) As Object
Try
Return type.InvokeMember(fieldName, BindingFlags.[Public] Or BindingFlags.[Static] Or BindingFlags.GetField, Nothing, Nothing, Nothing)
Catch e As Exception
Return Nothing
End Try
End Function
Public Shared Function LoadType(ByVal assemblyName As String, ByVal className As String) As Type
Try
Return Assembly.Load(assemblyName).[GetType](className)
Catch e As Exception
Return Nothing
End Try
End Function
Public Shared Function GetProviderFactory(ByVal providerName As String, ByVal assemblyName As String, ByVal className As String) As DbProviderFactory
Dim provider As DbProviderFactory
Try
provider = DbProviderFactories.GetFactory(providerName)
Catch generatedExceptionName As Exception
provider = TryCast(GetPublicStaticField(LoadType(assemblyName, className), "Instance"), DbProviderFactory)
End Try
If provider Is Nothing Then
Dim msg As String = "Create DbProviderFactory failed."
Throw New Exception(msg)
End If
Return provider
End Function
沒有留言:
張貼留言