You can pass a text batch of statements to the database. It can be quite efficient.
Instead of creating a SqlCommand
of CommandType.StoredProcedure
and taking a single stored procedure name and set of parameters – which, as you suspect, will perform poorly if you round-trip to the database for each record – you can instead create a SqlCommand
of CommandType.Text
, and then construct a text batch containing multiple SQL statements (which would be invocations of your stored procedure.) Separate each statement with a semi-colon.
Another advantage of a text batch is that your stored procedure can be kept simple and just process a single record at a time.
But, be careful: You need to ensure your parameters are properly quoted / escaped, because creating a plain text batch instead of using CommandType.StoredProcedure
(with parameters) opens you up to SQL-injection type vulnerabilities.