0

I need to clode the Connection (conn.Close();) ater reading SQL

Heres the Reader

public MySqlDataReader ExecuteReader(string sql)
{
    try
    {
        
        MySqlDataReader reader;

        MySqlCommand cmd = new MySqlCommand(sql, conn);
        reader = cmd.ExecuteReader();
        
        return reader;
        


    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return null;
}

Where do i need to put the Close line?

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
Sebastian
  • 17
  • 2

1 Answers1

1

Either load and return a DataTable using DataTable.Load(IDataReader) instead of a DataReader, and put the Connection in a using block, or use CommandBehavior.CloseConnection and when the DataReader is closed the Connection will be too.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67