Casting oledb to Sqlclient objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waleednosir
    New Member
    • Apr 2012
    • 2

    Casting oledb to Sqlclient objects

    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

    Code:
        Public aa As SqlClient.SqlConnection
        Public bb As OleDb.OleDbConnection
        Public gg as object
    
       if applicType="AccessDatabase" then directcast(gg,oledib.oleDbConnection")

    but this gave me an error .

    I tried also :

    Code:
       if applicType="AccessDatabase" then directcast(aa,oledib.oleDbConnection")
    but gave an error that I can not cast sqlclient to oledb .


    Any solution ?

    Thanks,
    Last edited by Rabbit; Oct 25 '12, 03:24 PM. Reason: Fixed code tags
  • ZiadElmalki
    New Member
    • Sep 2010
    • 43

    #2
    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

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Both the OleDBConnection class and the SqlConnection class inherit from the Common.DBConnec tion 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.

      -Frinny

      Comment

      Working...