Question it cant connect.


Sep 5, 2016
Programming Experience
Hello all.
First i want to say it good to be here with you people. Im trying to do someting for my school project and i need help.
I have my mysql database on dreamhost server and i make program to test my connection on that database and i get every time message that it cant connect.

here is the code:
        private void button1_Click(object sender, EventArgs e)
            string connetionString = null;
            SqlConnection connection;
            SqlCommand command;
            string sql = null;

            string serverName = textBox1.Text;
            string databaseName = textBox2.Text;
            string userName = textBox3.Text;
            string Password = textBox4.Text;
            int num = 1;
            connetionString = "Data Source=" +serverName+ ";Initial Catalog=" +databaseName+ ";User ID=" +userName+ ";Password=" +Password+ "";
            sql = "SELECT * FROM mitAdminLogIn WHERE adminID =" +num+ "";

            connection = new SqlConnection(connetionString);
                command = new SqlCommand(sql, connection);
                MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
            catch (Exception ex)
                MessageBox.Show("Can not open connection ! ");

and i post photo of my program.
Please help me and thank you all.
Firstly, I have deleted your attachment that showed the user name and password for your database. User names are OK but please don't EVER display a password publicly like that. If you want to post a screen shot that includes a password then obscure it in some way first at least.

Anyway, the issue is that you are using the SqlClient ADO.NET provider, which is specifically for SQL Server. It can't possibly connect to a MySQL database. You need a provider that supports MySQL for that. Unless there's a reason you can't, the best option is to download and install the Connector/Net provider from MySQL. You will then use the MySQL.Data.MySqlClient namespace in pretty much exactly the same way as you would use the System.Data.SqlClient namespace for SQL Server. You can visit to make sure that your connection string is valid for MySQL and I would advise using a MySqlConnectionStringBuilder to build it up from parts rather than string concatenation as it will be less error-prone.
Thank you jmcilhinney for your answer. I change password of my database and i never post it agen. I downloaded MySQL WorkBench as my connector/Net provider from MySQL and try to log in but i have same problem. I post photo of problem.

prob data.PNG
I downloaded MySQL WorkBench as my connector/Net provider from MySQL
No you didn't. If you downloaded MySQL Workbench then you downloaded MySQL Workbench. Connector/Net is something else. If you want to connect to MySQL from a C# app then have to actually download and install Connector/Net.

Of course, that's still not going to help if your connection string is invalid so if you can't login to MySQL from elsewhere then you won't be able to write a valid connection string either. That's nothing to do with C# so it's outside my area of expertise, as someone who barely uses MySQL.
Aham i see now.
I downloaded and instal all in MySQL package. In that package is Connector/Net. I contacted my server suport and we find the problem. Problem is that my server is set to recognize only dreamhost ip address. When i inserted my IP address all works fine.
Thank you jmcilhinney for your help. You help me a lot.
Top Bottom