1

I'm designing a WPF form that needs to connect to a local MySQL database. I've installed mysql on my local machine and created a database, with a username and password to connect to it. See two screenshots below for more information.

Screenshot from within MySQL Workbench

Connecting to the database through MySQL Workbench

My code currently looks like this:

    public static bool InitialConnection()
    {
        try
        {
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = "Server=127.0.0.1;Database=StudentTravelPlannerDB;Uid=STPUser;Pwd=CORRECT_PASSWORD;";
                con.Open();
            }
            return true;
        }
        catch
        {
            return false;
        }
    }

However, when I run my WPF form, which calls this function, it always freezes for a few seconds, and the above function will return false.

Removing the try/catch statements, the application fails and gives me an error pointing to the 'con.Open()' line, stating:

"An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"


So what could be the problem here? My password is definitely correct. I think I laid out the connection string right, and I've tried different variants and it's always the same response, adding the port doesn't help either.

Any guidance or solutions appreciated, thank you.

Conor
  • 721
  • 1
  • 6
  • 18
  • 4
    You have `MySQL` but connecting to `MS SQL` - check this one for example http://stackoverflow.com/questions/21618015/how-to-connect-to-mysql-database – Lanorkin Mar 24 '17 at 11:42
  • @Lanorkin Oh thanks.. quick question - would it be relatively easy to just create a new MS SQL database? I have nothing in my database yet. Then I can use this instead. Because looking at the link your provided, I can't be bothered to re-write code to connect to a MySQL database instead, if it's more long winded than just creating a MS SQL Database. – Conor Mar 24 '17 at 11:48
  • Sure if you have SQL Server installed then that basically just as easy as right click - new database - done. – Lanorkin Mar 24 '17 at 11:49
  • if you change to MSSQL server you should be aware that, contrary to MySQL, you probably need to buy a license - depending on how demanding your project will be. There is a cost-free version of MSSQL (Express edition) but it's severly limited. You should do some research and evaluate what you need when it comes to your DBMS. – Steffen Winkler Mar 24 '17 at 11:52
  • @SteffenWinkler SQL2016 express doesn't look too limited - 4 cores, DB size up to 10GB? https://www.microsoft.com/en-gb/sql-server/sql-server-editions – stuartd Mar 24 '17 at 12:03
  • @stuartd yeah that's decent. Last touched MSSQL Express two years ago. Just wanted to give OP a heads-up. – Steffen Winkler Mar 24 '17 at 12:06
  • Thanks, I stuck with MySQL because the latest versions of MSSQL are for Win8 and higher, but I'm using Windows 7. I'd rather stick with MySQL than use an older version of MSSQL, thanks for all the advice. – Conor Mar 24 '17 at 13:39

0 Answers0