Execute a bunch of commands in one fell swoop with ADO.NET
I have a series of T-SQL DELETE FROM TABLE and INSERT statements to run. I am using plain ADO.NET (and not the entity framework). Is there a way开发者_如何学Python I could execute all of these statements in one go rather than create a new SqlCommand object for each of them?
sqlCommand.CommandText =
@"DELETE FROM foo;
INSERT INTO foo (name) VALUES ('name1');
INSERT INTO foo (name) VALUES ('name2');
";
Yes, you can use a single SqlCommand with CommandType.Text to combine all the delete statements together in to a single block. Although it is possible to run both the INSERT and DELETE statements together, it may be easier to batch like commands if you want paramaterized queries.
Edit
I was going to add a code sample of how to create a batched parameterized query, but if other answer is all that you need, then I will leave it alone for now.
精彩评论