In the
VB.NET book, chapter 16, there are two sample programs that use DataAdapters two different ways. I just need to validate my thinking since I either missed it in the book, or it is not clearly stated.
---- Code using objConnection.Open()---
Page 543 shows code that uses the objDataAdapter.SelectCommand properties such as:
objDataAdapter.SelectCommand = New SqlCommand
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = 'some SQL command here..."
objDataAdapter.SelectCommand.CommandType = CommandType.Text
after setting up these objDataAdapter properties the next line of code opens the connection to the database with objConnection.open()
--- Code without objConnection.Open() ---
Page 555 - 556 shows code that defines the objConnection using a and uses it as an argument in the data adapter constructor. So the data adapter is built as:
objDataAdapter as SqlDataAdapter = New SqlDataAdapter ("SQL code here...",objConnection)
then the code goes on to define the objDataSet, objDataView and objConnectionManager objects. The objConnection.Open() function is never called.
I tested the code without, and with the objConnection.Open() commands and the code works the same either way. So my thinking is that using the dataAdapter constructor that takes the objConnection string as an argument automatically deals with the Open and Close calls. I am confident my thinking is clear, BUT here is the rub. The database connection is obviously opening since the dataview is populated, but when is the database closed? - after the objDataAdapter.Fill as shown on page 556 perhaps?
Thanks in advance for your feedback.