尝试连接到远程地址数据库时出现超时错误

时间:2014-02-25 00:19:06

标签: c# mysql

我一直收到超时错误,只有我的代码工作了几次。我正在阅读这个论坛,我必须在mysql之前使用“使用”。所以我尝试添加它,但仍然没有运气。也许我用它错了?在没有“使用”的情况下离开了我与数据库的最后一次连接,所以你们可以看到它最初是如何的

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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

超时与using语句无关;在using中包装连接只会确保在完成连接后处理连接。超时可能意味着您的计算机与数据库之间存在网络连接问题,或者您提供的服务器名称错误。