1

I'm trying to learn how to create basic C# programs in Visual Studio 2015 with a MySQL database connection.

I'm working on a login form that checks the database for an unencrypted username and password match and displays a message box confirming correct or incorrect.

It works, but the response time is about 15 seconds! The table only has 4 rows it should be basically instantaneous as it's localhost.

Please help. The application I'm planning on writing needs a fast response time and will have a LOT more data to query.

EDIT: I've now written another program that just opens the connection and displays a message. No selecting. It still takes around 15 secs for a response. So the slow response is to with establishing the connection are there any common causes of this? I'm using Windows 10

 private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string myConnection = "datasource=localhost;port=3306;username=root;password =root";
            MySqlConnection myConn = new MySqlConnection(myConnection);

            MySqlCommand selectCommand = new MySqlCommand("select * from database1.edata where user_name='"
                + this.username_txt.Text + "' and password='" + this.pwd_txt.Text + "' ;", myConn);

            MySqlDataReader myReader;
            myConn.Open();
            myReader = selectCommand.ExecuteReader();
            int count = 0;
            while (myReader.Read())
            {
                count += 1;
            }

            if (count == 1)
            {
                MessageBox.Show("Username and Password accepted...Access Granted :)");
            }
            else if (count > 1)
            {
                MessageBox.Show("Duplicate username and password found....Access Denied :(");
            }
            else
            {
                MessageBox.Show("Incorrect Details entered...Access Denied :(");
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
DMcDonald
  • 89
  • 1
  • 14
  • Why did you select all? select only whats necessary –  Apr 14 '16 at 22:11
  • 1
    Use sql parameters to prevent sql injections. –  Apr 14 '16 at 22:13
  • I'm following a tutorial and that's what they did. If it's a small table though would that really slow it down this much? – DMcDonald Apr 14 '16 at 22:20
  • Probably not. Did you try runing the same select command in sql? –  Apr 14 '16 at 22:23
  • 1
    Yes it worked fine. I've now written another program that just opens the connection and displays a message. No selecting. It still takes around 15 secs for a response... – DMcDonald Apr 14 '16 at 22:27
  • Check if the database is used anywhere else. –  Apr 14 '16 at 22:34
  • What happends if you ron a program without a connection? –  Apr 14 '16 at 22:35
  • If you are following a tutorial, they are teaching you bad techniques. The Connection and the DataReader should both be wrapped in `using` blocks. And your code is open to Sql Injection because you are building a command from a user string. – Icemanind Apr 14 '16 at 22:36
  • I dont think the database is being used anywhere else I just created it. All other programs are functioning fine. Computer is functioning fine. For some reason establishing the connection is taking a long time... – DMcDonald Apr 14 '16 at 22:46
  • Icemanind I appreciate you letting me know that. I'll reconsider the tutorial I am using and look into the correct techniques. But i don't believe they will fix the problem I'm having.... – DMcDonald Apr 14 '16 at 22:47
  • What happens if you perform the same query twice on the same connection? There is some ramp-up time for opening a connection on MySQL. And if libraries are compiling for the first time, that will add to the first time a query executes as well. – MutantNinjaCodeMonkey Apr 15 '16 at 18:29
  • Hi MutantNinjaCodeMonkey thanks for your input. You're right, running multiple queries on the same connection gives a much faster response time(almost instant) however given the simplicity of the code and the small size of the table I'm of the opinion that 15 seconds is too slow even for establishing the initial connection. I've made some progress since this initial question, please find the updated problem here [link](http://stackoverflow.com/questions/36651150/any-way-to-trace-mysqlconnection-to-detect-reasons-for-slow-connection?noredirect=1#comment60901721_36651150) – DMcDonald Apr 15 '16 at 18:45

0 Answers0