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

datatable

#1

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


#2

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


#3

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


#4

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


#5

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.