S
Scott Hodson
I'm trying to dynamically instantiate database connection objects but it
keeps giving me an error
Consider this code (I have set up proper "using" clauses)
==========================
1: System.Data.Odbc.OdbcConnection whatever = new
System.Data.Odbc.OdbcConnection();
2: Type classType = Type.GetType("System.Data.Odbc.OdbcConnection", true);
3: IDbConnection connection = (IDbConnection)
Activator.CreateInstance(classType);
==========================
I know the System.Data.dll assembly is being loaded because I can see it
being loaded in VS.NET's Output window, and the first line of code works
fine. But on the 2nd line I get (assume my app is called MyApp.exe, and the
name of the assembly is "MyApp")
==========================
An unhandled exception of type 'System.TypeLoadException' occurred in
MyApp.exe
Additional information: Could not load type System.Data.Odbc.OdbcConnection
from assembly MyApp, Version=1.0.1290.31707, Culture=neutral,
PublicKeyToken=null.
==========================
I have done this sort of thing successfully when loading classes that are in
the app's assembly, but, for some reason, I'm not able to do it with classes
in other assemblies. If I set the 2nd parameter on the 2nd line to false or
take it out I get an error on line 3
==========================
An unhandled exception of type 'System.ArgumentNullException' occurred in
mscorlib.dll
Additional information: Value cannot be null.
==========================
Because the GetType() failed and returns a null Type object...
Help!
P.S. In case you're wondering why I'm doing this, I'm trying to create a
database object factory for an app that needs to support SQL, Oracle and DB2
and based on some config settings I want to instantiate different database
objects that implement the same System.Data interfaces but in different ways
and I want to compare the performance of System.Data.SqlClient,
System.Data.Odbc to DataDirect's .NET Connect drivers for
SQL/Oracle/DB2/Sybase.
http://www.datadirect-technologies.com/products/dotnet/dotnetindex.asp
keeps giving me an error
Consider this code (I have set up proper "using" clauses)
==========================
1: System.Data.Odbc.OdbcConnection whatever = new
System.Data.Odbc.OdbcConnection();
2: Type classType = Type.GetType("System.Data.Odbc.OdbcConnection", true);
3: IDbConnection connection = (IDbConnection)
Activator.CreateInstance(classType);
==========================
I know the System.Data.dll assembly is being loaded because I can see it
being loaded in VS.NET's Output window, and the first line of code works
fine. But on the 2nd line I get (assume my app is called MyApp.exe, and the
name of the assembly is "MyApp")
==========================
An unhandled exception of type 'System.TypeLoadException' occurred in
MyApp.exe
Additional information: Could not load type System.Data.Odbc.OdbcConnection
from assembly MyApp, Version=1.0.1290.31707, Culture=neutral,
PublicKeyToken=null.
==========================
I have done this sort of thing successfully when loading classes that are in
the app's assembly, but, for some reason, I'm not able to do it with classes
in other assemblies. If I set the 2nd parameter on the 2nd line to false or
take it out I get an error on line 3
==========================
An unhandled exception of type 'System.ArgumentNullException' occurred in
mscorlib.dll
Additional information: Value cannot be null.
==========================
Because the GetType() failed and returns a null Type object...
Help!
P.S. In case you're wondering why I'm doing this, I'm trying to create a
database object factory for an app that needs to support SQL, Oracle and DB2
and based on some config settings I want to instantiate different database
objects that implement the same System.Data interfaces but in different ways
and I want to compare the performance of System.Data.SqlClient,
System.Data.Odbc to DataDirect's .NET Connect drivers for
SQL/Oracle/DB2/Sybase.
http://www.datadirect-technologies.com/products/dotnet/dotnetindex.asp