This is my code for select data from table in MySql:
MySqlDataReader msdr;
MySqlConnection connect = new MySqlConnection(connectionStringMySql);
MySqlCommand cmd = new MySqlCommand();
string commandLine = "SELECT id,token FROM Table WHERE id = @id AND token = @token;";
cmd.CommandText = commandLine;
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@token", token);
cmd.Connection = connect;
cmd.Connection.Open();
msdr = cmd.ExecuteReader();
//do stuff.....
msdr.Close();
cmd.Connection.Close();
As you can see i close this two:
msdr.Close();
cmd.Connection.Close();
And i want to ask if i need to close this two? or it will be ok to close only the cmd.Connection.Close();
The reason i asked it it because sometimes i get this error when i try to select data in table:
Details: MySql.Data.MySqlClient.MySqlException: Too many connections
And i want to know if it's because i don't close this connections.