tags:

views:

183

answers:

2

Hi all,

I'm writing an application that is able to be connected to multiple types of database (eg: SQL Server, Oracle, MySQL, etc). It's an international-targeted application, so I ideally want to be able to do this without needing to rely on string literals if possible. At the moment, I'm checking the driver name and using a case statement. Is there a better way to do this?

Thanks in advance!

A: 

One way to do this is to check the connection objects "Data Source Name" and "DBMS" property

Example

'lets say you have a connection object like below
Cn1.ConnectionString =  "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=dbname;User=root;Password=;Option=3"
Cn1.open
'Once the connection is opened, get the properties you are interested in
If Cn1.Properties(9) = "MS Jet" Then 'if you are connected to Access
  strDBType = "Jet"
ElseIf Cn1.Properties(11) = "MySQL" Then 'if you are connected to MySQL or MSSQL
  strDBType = "MySQL"
End If

HTH

Anand
A: 

If I might suggest a different approach that has worked well for me when I worked with VB6 back in the day.

Create an Interface class that defines your methods, properties, etc... Perhaps one of the properties should be DatabaseType. Then create a class for each type of database you will use that implements the above-mentioned interface.

After you instantiate the appropriate class, you could always interrogate its DatabaseType property to find out what database you are working with.

AngryHacker