Click here to Skip to main content
15,178,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to connect to mySQL and I generally do not know what is going wrong. I do not understand why it is refusing.

This is for my project, by the way, and I have been struggling to get this one thing right. It just needs to send data from the serial port to mysql. The application, itself, can receive the data and display it on the form. It's just connecting to the database is a issue.

I have downloaded mysql installer for visual studios
Created my table on mySql.
Added a reference for

Am I missing something ?

I would really appreciate the help.

What I have tried:

This is my code:
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using MySql.Data.MySqlClient;

namespace CP_Serial_Port_Terminal
    public partial class Form1 : Form
        string dataOUT;
        string sendWith;
        string dataIN;

        StreamWriter objStreamWriter;
        string pathFile;
        bool state_AppendText = true;

        MySqlConnection myConnection;
        MySqlCommand myCommand;

        private void SaveDataToMySqlDatabase()
                    myConnection = new MySqlConnection("server=localhost; username=root; password=; port=3306; database=database2");

                    myCommand = new MySqlCommand(string.Format("INSERT INTO `serial data` VALUES('{0}')", dataIN), myConnection);

                catch(Exception error)

Updated 4-Oct-21 7:23am

Uhh... A serial port has nothing to do with this code, and you're not going to connect to a MySql server over a serial port.

Your code is saying it's connection over TCP/IP, over Ethernet or some other network transport.

Without knowing more about the machines and the connections between them, there's little that can be said to help you.

Is the Sql server listening on port 3306?
NKL21 4-Oct-21 12:50pm
Yes, it is listening on port 3306.
Dave Kreskowiak 4-Oct-21 12:58pm
On top of what Chill said, once you correct the connection string, if it still fails, check for firewall blocking port 3306.
Your connection string does not look correct - see the options at MySQL connection strings -[^]
- You have not included the driver information or host
- Username is only used for SevenObjects, the rest use User
- No need to specify Port 3306 as it is the default
- Not sure that is how you would specify a blank password - try using an actual password
- Also not sure if MySql is case sensitive so you might want to consider using Server instead of server, Database instead of database etc
NKL21 4-Oct-21 13:17pm
I have changed the code and it still says the same thing
CHill60 5-Oct-21 2:50am
Changed the code to what?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900