Fernando Rodríguez Fernando Rodríguez - 3 months ago 35
MySQL Question

MySQL Connection timeout on C#

I just check other posts on stack overflow about the connection timeout, but looks like everybody fixed it using

com.CommandTimeout = int.MaxValue;


dbaccess.ServerAddress = "Server=mysql;Database=MyDB;Connect Timeout=2147483;Uid=username;Pwd=mypassword";

Unfortunately, i still get this error message after 40 seconds:

The timeout period elapsed prior to completion of the operation or the server is not responding.

You can take a look at the two pictures.
MySQL Workbench works fine and it takes 48 seconds in executing the same query.



I can not use limits to reduce the subset adquired because i am centralizing information splitted in different tables. A store procedure can not be use since the information is stored in diferent mysql servers too.

Any feedback will be much apreciate.


This is the code. I posted the image to show the execution time, not the error msessage.

public DataSet ExecuteQuery(string[] Query, TableMap[] TableMappings)

//Mysql vars
MySqlConnection con;
MySqlCommand com;
MySqlDataAdapter adapter;
DataSet ds;

con = new MySqlConnection();
con.ConnectionString = _serveraddress;

com = con.CreateCommand();
com.CommandType = System.Data.CommandType.Text;
foreach (string st in Query)
com.CommandText = com.CommandText + st;

com.CommandTimeout = int.MaxValue;

adapter = new MySqlDataAdapter(com.CommandText, con);
foreach (TableMap tm in TableMappings)
adapter.TableMappings.Add(tm.SourceTable, tm.TableSet);

ds = new DataSet();

return ds;


C4u C4u

Change Pwd to password. Also the property server should be set to an ip or domain.

conn = "Server=[ip/domain];...password=mypassword";

Wrong credentials would result in a failed login, not a timeout. I guess it really tries to connect to mysql. If the server is running on your local computer just set localhost as server.