By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,948 Members | 1,103 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,948 IT Pros & Developers. It's quick & easy.

Casting oledb to Sqlclient objects

P: 2
Dear Experts,

I have a situation , I am sure about the best approach for it. I have a database application that is designed to handle both SQLServer database and Oleddb "Access mdb" database. the issue is that during application start up , the application detects the configuration file and decides which database to use , eg , oledb to Sqlclient objects. the problem is that the application itself is, eg code to use teh sqlclient objects at different locations of the program. If i need to use oledb , i need to replace all of the sqlclient objects with oledb. who can i do it at run time. the bad solution will be to make at make both objects and decide which one to use , but the application is large. so it will be un-logic to use this approach. I tried to define an object as below

Expand|Select|Wrap|Line Numbers
  1.     Public aa As SqlClient.SqlConnection
  2.     Public bb As OleDb.OleDbConnection
  3.     Public gg as object
  5.    if applicType="AccessDatabase" then directcast(gg,oledib.oleDbConnection") 

but this gave me an error .

I tried also :

Expand|Select|Wrap|Line Numbers
  1.    if applicType="AccessDatabase" then directcast(aa,oledib.oleDbConnection")
but gave an error that I can not cast sqlclient to oledb .

Any solution ?

Sep 29 '12 #1
Share this Question
Share on Google+
2 Replies

P: 43
That is not possible because neither the OleDbConnection or SqlConnection derive from each other. In the System.Data namespace there are some basic classes and interface for ADO.NET. For connection could use IDbConnection interface as abstraction
Oct 25 '12 #2

Expert Mod 5K+
P: 9,731
Both the OleDBConnection class and the SqlConnection class inherit from the Common.DBConnection class.

So, in theory, you can declare your connection as a DbConnection and instantiate it as either a OleDbConnection or a SqlConnection later.

That way, when you're doing your direct cast, it will work properly.

Oct 25 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.