continuous SqlCommand ExcuteNonQuery good idea?

  • Thread starter Thread starter Daniel Bass
  • Start date Start date
D

Daniel Bass

In the scenario where I have multiple entries (a couple of thousand on a
worst case) I'd like to add to a database via Stored Procedures, is it
proper procedure to:

- create a SqlCommand object for the insert
- add parameters etc to this sqlcom obj
- create a SqlConnection object to connect to the database
- create a SqlDataAdapter for populating the data etc
- open the connection
- populate the parameters
- call ExecuteNonQuery
- close the connection
- REPEAT the whole process for each entry

or do I

- create a SqlCommand object for the insert
- add parameters etc to this sqlcom obj
- create a SqlConnection object to connect to the database
- create a SqlDataAdapter for populating the data etc
- open the connection
+ for each entry I want to add
-- populate the parameters
-- call ExecuteNonQuery
- close the connection

To futher explain, the first instance I open and close the connection for
each entry, while in the second, all I do is open a connection, repopulate
the parameters for the insert command, call ExecuteNonQuery, then when all
the entries have been inserted, close the connection.

Are there other things I need to be aware of or look out for?

Thanks for your time.
Dan.
HAPPY NEW YEAR!
 
Back
Top