Results 1 to 4 of 4

Thread: [RESOLVED] Database connections in VB

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Resolved [RESOLVED] Database connections in VB

    So I am just getting back into VB after a long hiatus. I spend a lot of time with web development, specifically PHP w/MySQL.

    When I connect to MySQL in PHP, the connection is opened when the page is loaded, and closed at the bottom of the page. Basically the connection is opened and closed on every single page load and all the queries I need are in between those connections.

    Getting into VB now will be my first time using databases with VB, I looked at JMC's tutorial for connecting to DBs and I was able to load what I wanted into my datagrid view, and into text boxes, etc. So thats all bueno.

    My question is, at what point are the database connections open. Do I connect to the database when I first load the program and keep it open until I close out. Or do I open and close the connection as needed when I need to interact with the db?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Database connections in VB

    It sounds like you are creating a Windows Forms app so the answer is that you open the connection as late as possible and close it as early as possible. It's important to realise that ADO.NET uses connection pooling so there are two levels of connection. The DbConnection object that you create is a fairly lightweight object. The actual database connection exists at a lower level. If you open a DbConnection and then close it, then open another DbConnection with the same connection string soon after, the same low-level database connection will be used both times. ADO.NET creates one database connection per pool, where a pool is created for each unique connection string. When you open your first DbConnection, a database connection is opened and that database connection persists after you close your DbConnection. If you go for some time without opening another DbConnection then ADO.NET will close the underlying connection, otherwise it will get used repeatedly.

    So, to answer your question, you open the database when you need to retrieve or save data and then you close it immediately afterwards. If you want to execute multiple queries in succession then you would open a connection before the first and close it after the last. You also don't have to use the same DbConnection object each time. Feel free to create and destroy DbConnection objects where they are used.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Database connections in VB

    OK that makes sense. So basically every time I need to query from the database I would need to have the following (taken from your example):

    As I am replying I am answering my own questions... I think I am going to play around a little bit and get back if I have any questions. Before I ask how, I'm going to try to create functions to keep my form code clean and keep database interaction in the class.

    Thanks!

    vb Code:
    1. Using connection As New SqlConnection("connection string here")
    2.     Using command As New SqlCommand("SELECT Quantity, Unit, Name FROM StockItem", _
    3.                                     connection)
    4.         connection.Open()
    5.  
    6.         Using reader As SqlDataReader = command.ExecuteReader()
    7.             While reader.Read()
    8.                 MessageBox.Show(String.Format("There are {0} {1} of {2} remaining in stock.", _
    9.                                               reader("Quantity"), _
    10.                                               reader("Unit"), _
    11.                                               reader("Name")))
    12.             End While
    13.         End Using
    14.     End Using
    15. End Using

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Database connections in VB

    That's basically correct. For instance, if you had a DAL that included a repository for the StockItem entity, it might have a GetData method that included code just like that and, as it read each record, packaged it into some other form and then returned a list containing those objects. Alternatively, the GetData method of the repository might use a data adapter and just return a populated DataTable, with a higher layer doing the repackaging. Either way, the lifetime of the DbConnection object would be that method alone.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width