So I have the following piece of code that is showing a strange behavior or I am missing some point here:
public async Task<IEnumerable<MyData>> GetMyData()
{
MySqlConnection conn;
IEnumerable<MyData> list;
string querystring = "SELECT * FROM tbl_mydata;";
using (conn = new MySqlConnection(mydbconnectionString))
{
//conn.Open();
ConnectionState check1 = conn.State;
list = await conn.QueryAsync<MyData>(querystring, commandType: CommandType.Text);
ConnectionState check2 = conn.State;
}
ConnectionState check = conn.State;
return list;
}
Now on running this, I can see that the check1
is Closed and I am still able to get the data from the database. In the above case both check1
and check
are in Closed state.
When I uncomment, the line: conn.Open();
, I can see that check1
is an Open state and getting the data from the database (expected). Upon coming out from the using
block, I can see that check
is set to Closed (expected).
My connection string looks like:
<add name="MyDBConnection" providerName="MySql.Data.MySqlClient" connectionString="server=127.0.0.1;port=3306;user id=root; password=root; database=ht_ep;Convert Zero Datetime=True;Allow Zero Datetime=False;Connection Timeout=10;pooling=true" />
So why I am still able to access the database without opening the connection? Am I missing something here?