Open and close a database connection to insert multiple records to table

Topics: Data Access Application Block
Apr 21, 2009 at 4:06 PM
I have a form in which I have multiple records that need to be either inserted or updated in the database.  I am using a stored procedure to save the
information.  Not using the Data Application Block I would open a connection once, save all of the records and then close the connection after
inserting the new records, in order to not create multiple database connections.

If I use the Data Application Block how do I open a connection and then close it manually?  Or does the Data Application Block do this automatically?

I want to be able to insert update new records efficiently.

Thank you,
Apr 22, 2009 at 2:23 AM
DAAB manages the opening and closing of connections most of the time.  When inserting records, you need not open and close connection manually.
Sample code:
Database db = DatabaseFactory.CreateDatabase();
DbCommand command = db.GetStoredProcCommand(spname);

There are times though that a connection is left open like in the case of returning a datareader.  In this case make sure to close or dispose the datareader in order to close the connection.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.