tags:

views:

91

answers:

3

I develop a product with a custom database configuration. I use ADO.NET with System.Data.Odbc.OdbcConnection for it. To make some differences between the databases is there a simple solution to detect the current DBMS of a connection.

+1  A: 

connection.Database would work or you can execute

select db_name()

to expand on connection.Database

connection.Open();
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);
        connection.ChangeDatabase("master");
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);
        Console.ReadLine();
SQLMenace
+2  A: 

I think that the OdbcConnection.Driver property may be more appropriate in the OP's context, since ServerVersion should return only the version number.

The Driver property will return the name of the DLL, such as "Sqlsrv32.dll" in case of SQL server. Then the OP can apply case based logic.

Cerebrus
I understood it as he wants the default database name
SQLMenace
A: 

I found the follow solution self:

DataTable td = conn.GetSchema(DbMetaDataCollectionNames.DataSourceInformation);
DataRow info = td.Rows[0];
String name = info[DbMetaDataColumnNames.DataSourceProductName];

For example this returns Microsoft SQL Server and is driver independent.

Horcrux7