简体   繁体   中英

Timeout error when trying to connect to remote address database

I keep getting timeout error, only a few times my code works. I was reading in this forum that I had to use "using" before the mysql. So I tried adding it, but still no luck. Maybe I'm using it wrong? Left my last connection to database without "using" so you guys can see how it was at first

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

namespace WindowsFormsApplication4
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            FillCombo();
        }
        void FillCombo()
        {
            string dbcon = "SERVER=IP;DATABASE=mudiw_test;UID=mudiw_test;PASSWORD=PASS";
            string Query = "select * from mudiw_test.Registration ;";
            using (MySqlConnection conDatabase = new MySqlConnection(dbcon))
            {
                using (MySqlCommand cmDatabase = new MySqlCommand(Query, conDatabase))
                {
                    MySqlDataReader myReader;
                    try
                    {
                        conDatabase.Open();
                        myReader = cmDatabase.ExecuteReader();
                        while (myReader.Read())
                        {
                            string sName = myReader.GetString("Name");
                            comboBox1.Items.Add(sName);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string dbcon = "SERVER=IP;DATABASE=mudiw_test;UID=mudiw_test;PASSWORD=PASS";
            string Query = "select * from mudiw_test.Registration where Name='" + comboBox1.Text + "' ;";
            using (MySqlConnection conDatabase = new MySqlConnection(dbcon))
            {
                using (MySqlCommand cmDatabase = new MySqlCommand(Query, conDatabase))
                {
                    MySqlDataReader myReader;
                    try
                    {
                        conDatabase.Open();
                        myReader = cmDatabase.ExecuteReader();
                        while (myReader.Read())
                        {
                            string sName = myReader.GetString("Name");
                            string sId = myReader.GetString("ID");
                            string sEmail = myReader.GetString("Email");
                            string sAddress = myReader.GetString("Address");
                            string sPhone = myReader.GetString("Phone");
                            string sVisits = myReader.GetString("Visits");
                            name.Text = sName;
                            id.Text = sId;
                            email.Text = sEmail;
                            address.Text = sAddress;
                            phone.Text = sPhone;
                            address.Text = sAddress;
                            visits.Text = sVisits;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedItem == "Mantenimiento")
            {
                int value;
                if (Int32.TryParse(visits.Text, out value))
                {
                    if (value < 3)
                    {
                        price.Text = "150";
                        total.Text = "150";
                        discount.Text = "0%";
                    }
                    else if (value >= 3 && value <= 6)
                    {
                        price.Text = "150";
                        total.Text = "127.50";
                        discount.Text = "15%";
                    }
                    else if (value >= 7)
                    {
                        price.Text = "150";
                        total.Text = "112.50";
                        discount.Text = "25%";
                    }
                }
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dbcon = "SERVER=IP;DATABASE=mudiw_test;UID=mudiw_test;PASSWORD=PASS";
            string Query = "insert into mudiw_test.Registration (Name,ID,Email,Address,Phone) values('" + this.name.Text + "','" + this.id.Text + "','" + this.email.Text + "','" + this.address.Text + "','" + this.phone.Text + "');";
            MySqlConnection conDatabase = new MySqlConnection(dbcon);
            MySqlCommand cmDatabase = new MySqlCommand(Query, conDatabase);
            MySqlDataReader myReader;
            try
            {
                conDatabase.Open();
                myReader = cmDatabase.ExecuteReader();
                MessageBox.Show("Saved");
                while (myReader.Read())
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

The timeout won't be related to the using statement; wrapping the connection in using simply ensures that the connection is disposed when you are done with it. A timeout likely means that you have network connectivity issues between your machine and the database or you've supplied the wrong server name.

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