Link to home
Start Free TrialLog in
Avatar of Millkind
MillkindFlag for Afghanistan

asked on

Sql Database connections

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.
Avatar of Jason Evans
Jason Evans
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Millkind

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Jason Evans
Jason Evans
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial