1

Does anyone know of a good way, programmatically, to determine if a sql server is a mysql or mssql installation?

I've got an app in c# that has to be able to connect to either mysql or mssql. I can connect to either if I know which it is, but I can't tell from the connection settings because all I get are the server name, userid, password, and database name.

I can have the settings file store the type of server, but this opens up an avenue for user error that I'd like to avoid.

5
  • 3
    select version(). that'll work on mysql, and give you a version number. mssql equivalent select @@version. neither should work on the opposite system. Commented Oct 2, 2015 at 21:08
  • Workaround: Use a try/catch block. Try to open with SQL Server drivers and if it fails, try to open with mySQL drivers. Flip these around to catch the most common type of connection first. Commented Oct 2, 2015 at 21:12
  • What type of authentication are you using? If it is user authentication you can tell from the connection string. MySql = UID and Sql Server = User ID Commented Oct 2, 2015 at 21:14
  • He can connect to both MSSQL and mysql with ODBC. Commented Oct 2, 2015 at 21:43
  • So you're saying you cannot trust your end-user to pick the correct RDBMS system via a listbox? Wowsers. Yep, the answer of "try to connect with 1...then the other" answer is all you can do. Commented Oct 2, 2015 at 21:50

2 Answers 2

2

Try to connect as MySQL and if it fails, handle the error gracefully then try to connect as MSSQL. Something loosely similar to this might work for you ...

string FindDbType(string connectionString)
{
    try
    {
        // try to connect with MySql
        return "MySql";
    } catch()
    {
        try
        {
            // try to connect with MSSQL
            return "MSSql";
        } catch()
        {
            return "Cannot determine DB Type"
        }
    }   
}

You will probably want to handle errors like incorrect password separately, but this should get you started.

Sign up to request clarification or add additional context in comments.

Comments

0

Create table like config or settings and put there record for DB type. (If DB which you access is yours.)

1 Comment

jboeke you've got the most useful answer but I'd prefer to do it without doing a fail test. For the other answers, like I said in my post, I don't get a connection string, just a userid, password, servername, and database name. There's no connection string to decode because my code builds it; And select version() works fine if you're already connected to the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.