A lot depends on how you use HasColumn. Do you call it only once or twice or several times in a loop? Perhaps the column is there or unknown in advance?
Setting the row filter is likely to scan the table every time. (Also theoretically, GetSchemaTable () can generate a completely new table every time it is called, which would be even more expensive - I donโt think SqlDataReader does it, but at the IDataReader level, who knows?) But if you only call it once or twice. I cannot imagine that this is a big part of the problem (unless you have thousands of columns or something else).
(I would, however, at least save the result of GetSchemaTable () in a local var inside the method, to avoid double-calling in quick succession, if you did not cache it somewhere by accident, that your specific IDataReader will regenerate it.)
If you know in advance that under normal circumstances the column that you request will be present, the exclusion method will be a little more acceptable (because a column that does not exist is essentially an exceptional case). Even if it is not, it may work a little better, but then again, if you do not name it repeatedly, you should ask yourself if performance really is of great concern.
And if you name it many times, you probably have to consider a different approach differently, for example: call GetSchemaTable () after the start, skip the table and load the field names into the dictionary or some other structure that is designed for quick search.
Eric Rosenberger
source share