[英]MySQL database to C# connection timeout
stackoverflow community! stackoverflow社区! 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). 我的问题是:我制作了一个ac#应用程序,用于在Byethost(SecureSignup)托管的MySQL数据库中注册帐户。 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."
一分钟后,我可以提交查询,PhpMyAdmin会将其显示为写在数据库中的蜜蜂,但是几分钟后,当我尝试完全相同的操作时,遇到了各种超时错误,其范围从“无法从传输连接读取数据:A连接尝试失败,因为连接的一方在一段时间后未正确响应,或者建立的连接失败,因为连接的主机未能响应。” 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.
到“超时IO操作” ...这是我的代码,也许拥有它可以帮助您理解为什么它有时会表现出这种奇怪的行为,因为我确定不能。
(I have granted my IP remote MySQL access from the cpanel, and the login data are 100% correct) (我已从cpanel授予我的IP远程MySQL访问权限,并且登录数据100%正确)
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.
您应该使用
parameters
来防止SQL injection.
Also use the using
statement to properly dispose your MySQL
objects. 还可以使用
using
语句正确处理MySQL
对象。
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. 我一直在使用
C#
来使用MySql
,没有任何问题,所以我认为这是您的托管问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.