I am writing an ASP.NET 2.0 app. I need touse several databases, so I am using the DbProviderFactory to I can dynamically change the provider type. Although it works, I find the parameter adding a bit verbose. Because the createParameter method doesn't accept parameters itself, I end up with several lines of code like this for each parameter....
param = oFactory.CreateParameter()
param.ParameterName = "ID"
param.DbType = DbType.Int32
param.Value = 2
What I am trying to do is extend the DbProviderFactory to include a CreateParameter method that DOES accept these properties as parameters. Here is what I have...
Public Class ExtendedDbProviderFactory Inherits DbProviderFactory
Public Function CreateMyParameter(ByVal strParameterName As String, ByVal oDBType As DbType, ByVal oValue As Object) As System.Data.Common.DbParameter
Dim param As System.Data.Common.DbParameter
param = MyBase.CreateParameter()
param.ParameterName = strParameterName
param.DbType = oDBType
param.Value = oValue
Return param
End Function
End Class
I then attempt to create it using this code....
Dim css As System.Configuration.ConnectionStringSettings = ConfigurationManager.ConnectionStrings(0)
Dim oMyExtendedFactory As ExtendedDbProviderFactory = DbProviderFactories.GetFactory(css.ProviderName)
But I keep getting this error when I run it with a SQL Server provider
Unable to cast object of type 'System.Data.SqlClient.SqlClientFactory' to type 'ExtendedDbProviderFactory'.
I would think that, because my class inherits the DBProviderFacotry class, and only extends it with 1 method, it would work as easily as the DBProviderFactory class itself does.