gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:38:08 25 4
gpt4 key购买 nike

我不断收到超时错误,只有几次我的代码有效。我在这个论坛上读到我必须在 mysql 之前使用“using”。所以我尝试添加它,但仍然没有成功。也许我用错了?留下我最后一次与数据库的连接而不是“使用”,这样你们就可以看到它一开始是怎样的

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

最佳答案

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

关于c# - 尝试连接到远程地址数据库时出现超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22002027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com