I'm having an issue when connecting to a remote host. I am able to connect to my local server with a copy of the database.
I'm trying to connect to the XenForo DB on my web host and get some information. All is working on localhost.
private static MySqlConnection _connection =
new MySqlConnection("Server=ip; database=ls-v_forum; UID=ls-v_forum; password=pass");
public static int? FetchUserId(string emailoruser)
{
MySqlCommand userCommand = new MySqlCommand("SELECT * FROM xf_user WHERE username='" + emailoruser + "'", _connection);
MySqlCommand emailCommand = new MySqlCommand("SELECT * FROM xf_user WHERE email='" + emailoruser + "'", _connection);
_connection.OpenAsync();
}
That's the code and it's throwing this error
Connection must be valid and open. at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception) at MySql.Data.MySqlClient.MySqlCommand.CheckState() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
EDIT
public int? FetchUserId(string emailoruser)
{
using (var _connection = new MySqlConnection("server=ip; database=ls-v_forum; UID=ls-v_forum; password=pass"))
{
MySqlCommand userCommand = new MySqlCommand("SELECT * FROM xf_user WHERE username='" + emailoruser + "'", _connection);
MySqlCommand emailCommand = new MySqlCommand("SELECT * FROM xf_user WHERE email='" + emailoruser + "'", _connection);
_connection.Open();
MySqlDataReader userReader = userCommand.ExecuteReader();
int? userId = null;
while (userReader.Read())
{
userId = userReader.GetInt32("user_id");
}
userReader.Close();
if (userId == null || userId == 0)
{
MySqlDataReader emailReader = emailCommand.ExecuteReader();
while (emailReader.Read())
{
userId = emailReader.GetInt32("user_id");
}
emailReader.Close();
}
_connection.Close();
return userId;
}
}
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. at MySql.Data.MySqlClient.NativeDriver.Open() at MySql.Data.MySqlClient.Driver.Open() at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at MySql.Data.MySqlClient.MySqlPool.GetConnection() at MySql.Data.MySqlClient.MySqlConnection.Open()