Sql Database connections

Millkind
Millkind used Ask the Experts™
on
In VB.net I have used the Data area in the toolbar to set up a dataconnection, dataadapter and dataset to a sql database.  In the code I use patronsda.Fill(Patronsds1) to fill my datasets.  Does this hold a connection open?  I see alot of code with conn.close and conn.open i havn't had to use any of them.  Just want to make sure I'm not hurting the database.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jason EvansSenior Software Developer

Commented:
Hi there.

I just tried the following code:

Using sqlConn As New SqlConnection("Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=True")
      Dim sqlCmd As New SqlCommand("SELECT * FROM Defects", sqlConn)
      Dim sqlDA As New SqlDataAdapter(sqlCmd)
      Dim ds As New DataSet

      sqlConn.Open()

      sqlDA.Fill(ds)

      Console.WriteLine(sqlConn.State.ToString)

    End Using

In the console window it shows that the connection is still Open when at that point in the code. After passing the End Using line, the connection state will be Closed, since the End Using line does the cleaning up for you. If you use the Using... End Using syntax then you won't have to worry about the connection being left open.

Hope this helps.
Jas.

Author

Commented:
I'm not setting up the connection in the code, its all in the design area.  All I do in the code area is patronsda.Fill(Patronsds1)

I went into the code and added

MsgBox(SqlConnection1.State.ToString)

after my

patronsda.Fill(Patronsds1)

and it said the connection was closed.

So I guess it does close the connection as soon as its done
Senior Software Developer
Commented:
Hi again.

Cool stuff, as long as the connection is closed then I guess you have nothing to worry about. It's always worth checking stuff like that out yourself, just in case connections are left open when they shouldn't be.

Cheers.
Jas.
Guys,

as far as i remember, the database disconnected method is the key role in vb.net especially using and setting database objects in the design time. Database Objects in .Net is quite powerful and smart, hard coded is not useful anymore.


So MillKind, your initiative in examining the connection behavior is quite a good.......

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial