1

I am trying to connect to my database on localhost but for some reason, it throws an exception, and I can't find out why and where.

private void Initialize()
    {
        server = "localhost";
        database = "dbname";
        uid = "uname";
        password = "pass";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new SqlConnection(connectionString);
    }

This is my initialize function; my problem is with:

    private bool OpenConnection()
    {
        try
        {
            connection.Open();
            return true;
        }
             ...

When I debug the program, it stays on "connection.Open();" line for a while, then I see "Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll" on console. But it's not crashing, just skipping.

14
  • Is there an inner exception to that exception, and what are the exception message(s)? Commented Apr 19, 2017 at 22:04
  • after your try {} you chould have catch (Exception ex) { /* put break point here to view the contents of your exception (ex) */ } Commented Apr 19, 2017 at 22:05
  • @hatchet nothing else, just this one. Commented Apr 19, 2017 at 22:05
  • If you aren't stopping on the exception, that's a problem and I'd start there: stackoverflow.com/questions/16970642/… Once you can see what the exception actually is, you can give us more information. Commented Apr 19, 2017 at 22:05
  • @MBurnham i have it, just didn't post. Commented Apr 19, 2017 at 22:06

2 Answers 2

3

If you want to open a connection with MySql, then you don't use the classes from System.Data.SqlClient namespace but with the classes from MySql.Data.MySqlClient

These classes are MySqlConnection, MySqlCommand, MySqlDataReader etc...
If you try to use the SqlConnection class you receive this error if the database to open is MySql because it can only work with the Sql Server, Sql Server Express or LocalDB database systems.

connection = new MySqlConnection(connectionString);

Of course, to use the MySql.Data.MySqlClient namespace you need to download and install the MySql NET Connector and then add a reference into your project to the assembly MySql.Data.dll (and add the using MySql.Data.MySqlClient to the top of each cs file that needs to use these classes)

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

4 Comments

will you marry me?
Please not that this requires the use of a third party namespace, MySql.Data is not provided in the .NET Framework by default. It is an ADO.NET driver provided by MySql
LOL ... didn't knew answering in SO can even get you marriage proposal ... Stevee you are so lucky ... :)
@Rahul it depends :-)
0

It is not crashing because you are opening the connection inside of a try-catch block, so the program thinks you are handling the exception. If you want it to crash, get rid of the try-catch block. (This would be a bad user experience.)

Alternatively, after your try-catch, you could check the connection is valid and then do something if it isn't.

if ((connection == null) || (connection.State != ConnectionState.Open)) { 
    /* do something here to indicate to the user there was an issue.
}

3 Comments

i see, the problem is i can't open the connection so it returns null. but why is this happening i just can't understand this.
@osumatu you will need to provide us with the message from the exception (ex.message) for us to possibly answer that
In order to understand why it returns null, add a catch block as mentioned by @MBurnham and see what goes in there. It should tell you whether the credentials are incorrect or the user doesn't have the right kind of access.

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.