OleDbContext to keep OleDbConnection alive I'm writing some automated tests that read and write data from/to excel (so end-users can define their test cases themselves). I use dotTratce to optimize the algorithms and found that the worst bottleneck was writing to excel. It turns out that OleDb does not have connection pooling and opening/disposing it takes a lot of time. Even with only 200 writes it sums up to 80 seconds (opening 40sec and dispose 40sec). To fix this I wrote this simple OleDbContext that I pass to methods that write to excel so that they don't open/close a connection for each write.

public class OleDbContext : IDisposable
{
    private OleDbConnection _connection;

    public OleDbContext(string fileName)
    {
        FileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
    }

    public string FileName { get; }

    public void Connect()
    {
        if (_connection == null)
        {
            _connection = new OleDbConnection(CreateConnectionString(FileName));
            _connection.Open();
        }
    }

    public int Execute(Func&lt;OleDbConnection, int&gt; query)
    {
        if (_connection == null) { throw new InvalidOperationException("There is no open connection yet. You need to connect first."); }
        return query(_connection);
    }

    private static string CreateConnectionString(string fileName)
    {
        return $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={fileName};Extended Properties='Excel 12.0 Xml;HDR=No;'";
    }

    public void Dispose()
    {
        if (_connection != null)
        {
            _connection.Dispose();
            _connection = null;
        }
    }
}


Usage example:

public int Write(string text, string columnLetter, int rowNumber, OleDbContext oleDbContext)
{
    return oleDbContext.Execute(conn =&gt;
    {
        var range = $"{columnLetter}{rowNumber}";
        using (var update = new OleDbCommand($"UPDATE [{SheetName}${range}:{range}] SET [F1]='{text}'", conn))
        {
            return update.ExecuteNonQuery();
        }
    });
}


With this improvement the execution time dropped to under 2 seconds which is very good. I was just wondering if there's more I can improve - performance wise I guess I did what is technically possible so how about other things?
It turns out that OleDb does not have connection pooling and
  opening/disposing it takes a lot of time.  


I have read somewhere, that as long as you keep one connection (for sure with the exact same connection string) open the connection pooling works, but don't count on it, just test it. You could at test initialize open this said connection and close it at test terminate.



Now let us look at your code.... well, there isn't much to say. It is easy to read, you are using braces {}, fields, methods and method parameters are well named.  

Just some little nitpicks:  


what happens if the constructor argument is an empty string ? 
you are testing excel files, why don't you ensure that the passed fileName has at least the correct file extension ?