How to check if the connection to the bbdd is open or not

How to check if the connection to mysql database is open or not?

Hi,

ConnectionState state = connection.State;
if (state == ConnectionState.Open)
{
return true;
}
else
{
connection.Open();
return true;
}
side note:connection can be Broken, Connnecting, Executing, Fetching
please import namespace: system.data.sqlclient, system.data.sql

Hi, first of all sorry for my bad english!

I have the next situation:

but I don’t find an option to check or to get “connection.State”
How can I do?

Thanks in advance

Hi ,
Its a method you can use if you import namespace from import tab as shown and use if condition to check the condition.

Yes, but in my case, I have 2 variables: “myConnection” (Type: DatabaseConnection) and "“myConnState” (type: ConnectionState).

I think I need to associate or link both variables, but I have no idea.

No doubt I’m doing something wrong but I do not realize that.

Hi @mbianchi @ddpadil,

Do you have solution for this,i am facing issue in using if condition to check database connection.

If
{
myConnection.State.ToString.Equals(“Open”)
}

It works for me :slight_smile:

2 Likes