简体   繁体   中英

MySQL database to C# connection timeout

stackoverflow community! Once more, I come here seeking for your help...

My problem is: I have made ac# application used to register accounts in a MySQL database hosted by Byethost (SecureSignup). The connection works, but not all the time, even though no conditions are changed. One minute I can submit the query and PhpMyAdmin shows it as beeing written in the database, but a few minutes later, when I try the exact same thing, I get various timeout errors ranging from "Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond." to "Timeout IO Operation"... Here is my code, maybe having it could help you understand why it has such a strange behaviour from time to time, because I sure can't.

(I have granted my IP remote MySQL access from the cpanel, and the login data are 100% correct)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Main
{
    public partial class Form1 : Form
    {
        string connString;
        MySqlDataReader reader;
        MySqlConnection conn;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            connString = "Server=sql.byethost46.org;Port=3306;Database=bleachon_aza;UID=bleachon;password=********";
            conn = new MySqlConnection(connString);
            MySqlCommand command = conn.CreateCommand(); 
            command.CommandText = "INSERT into users (Username,password) VALUES('" + usr.Text + "','" + pass.Text + "')";
            conn.Open();
            int timeoutvalue = conn.ConnectionTimeout;
            command.ExecuteNonQuery();
            MessageBox.Show(timeoutvalue.ToString()); //This was a curiousity, it displays 15
            conn.Close();
        }
    }
}

You should use parameters to prevent SQL injection.

Also use the using statement to properly dispose your MySQL objects.

private void button1_Click(object sender, EventArgs e)
{
    connString = "Server=sql.byethost46.org;Port=3306;Database=bleachon_aza;UID=bleachon;password=********";
    using (conn = new MySqlConnection(connString))
    {
        string query = "INSERT INTO users (Username, password) VALUES (@usr, @pass)";
        // are you sure your column name is Username and not username ?
        conn.Open();
        using (MySqlCommand cmd = new MySqlCommand(conn, query))
        {
            cmd.Parameters.AddWithValue("@usr", usr.Text);
            cmd.Parameters.AddWithValue("@pass", pass.Text); 
            cmd.ExecuteNonQuery();
            // No need to close your connection. The using statement will do that for you.
        }
    }
}

As for the connection problems, I think you should contact your host for answers. I use MySql all the time using C# , without any problems, so i think it's a problem with your hosting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM