1

So I'm following this tutorial: http://support.microsoft.com/kb/314145/

and I get an unexpected error: A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

My class looks like this:

    class Database
{
    public Database()
    {
        string connectionString = "Password=pass;User ID=userid;Initial Catalog=soksko;Data Source=(local)";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
            Console.WriteLine("State: {0}", connection.State);
        }

        Console.WriteLine("Database: OK");
    }

}

I googled, but I couldnt find anything valuable. I am using MySQL database, it is on the same computer and I am using VS 2013. I successfully added my database to Server Explorer with the same connection information that I use above, but I get exception, when I try to open the connection.

4
  • First of all use try catch and set a breakpoint in the exception handler. this should give you some insights Commented Nov 6, 2014 at 22:01
  • I tried that. It's the open method that throws the exception. Commented Nov 6, 2014 at 22:01
  • And what exactly does the exception say? Commented Nov 6, 2014 at 22:02
  • The problem is that even if I use try/catch, it is still the same message above. I do not get where it comes from. Commented Nov 6, 2014 at 22:06

3 Answers 3

2

SqlConnection is for MS SQL Server. For MySql you need to use a MySqlConnection class provided by the MySQL connector (http://dev.mysql.com/doc/connector-net/en/index.html)

using MySql.Data.MySqlClient;

using(MySqlConnection myConnection = new MySqlConnection(myConnectionString))
{
    myConnection.Open();
    // execute queries, etc
}
Sign up to request clarification or add additional context in comments.

1 Comment

My bad... I appreciate your input.
1

See this link for how a MySQL connection string should look:

ASP.NET use SqlConnection Connect Mysql

See this link for an explanation of the oft mis-used Data Source=(local):

http://blogs.msdn.com/b/sql_protocols/archive/2008/09/19/understanding-data-source-local-in-sql-server-connection-strings.aspx

hint you're not using SQL-Server so it won't work for you

Comments

0

The tutorial you're using is for Sql Server, not MySQL.

Comments

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.