0

Im trying to learn how to access databases from a c# application. I have written this simple code, to access a MySql database I hosted on freemysqlhosting.com. Its a very simple windows forms application: it contains a button called 'login' that upon press, should put the username that has the password '1234' in the textbox called 'username' (should be "sampleUser").

This is the code I have tried:

private void login_Click(object sender, EventArgs e)
    {
        string connectionString = "server=sql11.freemysqlhosting.net;user id=*********;database=sql11158998;password=********;";
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM users WHERE password = 'hrsidkpi'", con);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    username.Text = "" + reader[0];
                }
            }
            finally
            {
                reader.Close();
            }
        }
    }

However, when I press the button, the application hangs. No exception/error is thrown. I left it on for a couple of minutes and nothing happened. I know the connection is working and that the details in the connection string are true because I have a DataSet connection I made to the server using the visual studio's "Data Sources".

Even when I remove all code inside the using caluse, it freezes. If I remove con.Open() it does not hang, so I believe this is the line that's causing the issue.

I have tried to look for a solution on the internet, and found quite a few people having the same problem, however no solution was given, or the solution given had no effect on my situation.

Help would be greatly appreciated, and sorry for english mistakes if I made any.

Nayan Katkani
  • 806
  • 8
  • 18
hrsidkpi
  • 147
  • 8
  • The database server, name and password is all within your posted code. I would recommend removing this on a public forum. – Rob Aston Feb 16 '17 at 13:19
  • This database is a free one I made for testing. If someone damages it I can just create a new one. I prefer to make it as easy as possible to help me, no private information was exposed. Thanks though. – hrsidkpi Feb 16 '17 at 13:19
  • 3
    The fact that the hostname you are connecting to says it's MySQL hosting, but you are trying to connect with MSSQL details might be the issue here... – DavidG Feb 16 '17 at 13:20
  • @RobAston: removing here doesn't help much because there is a revision history – Tim Schmelter Feb 16 '17 at 13:20
  • What do you mean MSSQL details? In the connection string? I used connectionstrings.com to get the connection string I need. – hrsidkpi Feb 16 '17 at 13:21
  • For example: http://stackoverflow.com/questions/21618015/how-to-connect-to-mysql-database – DavidG Feb 16 '17 at 13:22
  • @hrsidkpi I tried to connect to that database from sql server management studio and I was unable to connect. Are you sure it is SQL Server and not MySQL??? – NicoRiff Feb 16 '17 at 13:22
  • just Add providerName="MySql.Data.MySqlClient" in ConnectionString. – Prasanna Kumar J Feb 16 '17 at 13:23
  • If the Server is really test purpose, open the ID, password again and let people try to test it. The name of host domain is mysql. – Kay Lee Feb 16 '17 at 13:31
  • @DavidG Thank you so much! That fixed it. I don't know why I used the wrong connection object. – hrsidkpi Feb 16 '17 at 13:31

1 Answers1

-2

Use this way

    public string _ConexaoBcoDados = "Data Source=10.10.40.12\\DESENV;Initial Catalog=control_db;Uid=usr_desenv14; pwd=12345678;";

        sql.AppendLine("select login_rede,perfil_id from protocol_tb with(nolock) ");

        using (var conn = new SqlConnection(_ConexaoBcoDados))
        {
            var cmd = new SqlCommand(sql.ToString(), conn);
            conn.Open();
            using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    valores = new string[] { reader["login_rede"].ToString(), reader["perfil_id"].ToString() };
                }
            }
        }