Click here to Skip to main content
15,891,740 members

[C#] MySQL Connector/NET - What Am I Doing Wrong?

Revision 4
I'm using the MySQL Connector for .net applications, specifically, C#. A System.Net.Sockets.SocketException is thrown when I invoke MySqlConnection.Open(). I've been looking over this code for the past few days, decided to see if another pair of eyes could help me.

I'm on Windows 7 x64 using MySQL Connector Net 6.3.0 in Microsoft Visual C# Express Edition. I have already linked to the MySql.Data.dll in C:\Program Files (x86)\MySQL\MySQL Connector Net 6.3.0\Assemblies. I don't receive any warnings nor errors. I have to explicitly catch this error or else my application will crash.

I've done a bit of digging around, one answer posted online said that I may have too many network adapters installed, which is not the case for me. I've disabled any unnecessary drivers for each adapter as well. I will be testing this problem in a virtual machine once I setup the development environment, I will post back with any results.

The only other thing that I can think of is that I had installed multiple other connectors for MySQL, along with a driver I have compiled for the Qt framework. I've since removed all of these, to no avail.

Any help is much appreciated. As I said, this has had me pulling my hair for the past two weeks. I'm almost to the point of just blaming MySQL and falling back a version or two of the connector if possible.

What else I've tried:
- Microsoft Fix it 50203
- Removing gratuitous network adapters, drivers and MySQL connectors
- Windows update, for what it's worth
- Reset Winsock controller (netsh winsock reset)

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace MySQLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            MySQLHandler MySQLh = new MySQLHandler();

            if (MySQLh.connectToServer())
                MySQLh.disconnectFromServer();
        }
    }

    class MySQLHandler
    {
        private MySqlConnection connection;
        private string server = string.Empty, user = string.Empty;
        private string pwd = string.Empty, database = string.Empty;

        string connectionString = string.Empty;

        public MySQLHandler()
        {
            server = "localhost";
            database = "qauth";
            user = "root";
            pwd = "";

            connectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + user + ";PASSWORD=" + pwd + ";";
            connection = new MySqlConnection(connectionString);
        }

        public MySQLHandler(string server, string user, string password, string database)
        {
            this.server = server;
            this.database = database;
            this.user = user;
            this.pwd = password;

            connectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + user + ";PASSWORD=" + pwd + ";";
            connection = new MySqlConnection(connectionString);
        }

        public bool connectToServer()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Attempting to connect to server...");
            Console.WriteLine();

            try
            {
                connection.Open();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Connection successfully established with " + this.server + "!");
                return true;
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                Console.WriteLine("A socket exception was thrown! " + ex.Message);
                Console.ReadKey();
                return false;
            }
            catch (MySqlException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                switch (ex.Number)
                {
                    case 0:
                        Console.WriteLine("Cannot connect to the specified server. Contact network administrator");
                        break;

                    case 1045:
                        Console.WriteLine("Invalid username or password.");
                        break;
                    default:
                        Console.WriteLine("An unknown error has occured, no connection to the server was established.");
                        break;
                }

                return false;
            }
        }

        public bool disconnectFromServer()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Attempting to disconnect from server...");
            Console.WriteLine();

            try
            {
                connection.Close();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Disconnected from server successfully!");
                return true;
            }
            catch (MySqlException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed to disconnect from " + this.server + " (" + ex.Message + ")");
                Console.WriteLine();
                return false;
            }
        }
    }
}
Posted 18-Nov-12 16:44pm by JayFab.
Tags: