Results 1 to 7 of 7

Thread: database connection

  1. #1

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    database connection

    is it possible to use one database connection throughout an application. The way I have it now I open a connection when I need it then close it.
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    you mean when the application is started the connection begins and remains connected until the application is closed???

    You should be able to declare that in the app.config or web.config file, depending on which type of application you are creating.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    This is how I was taught to do it from the book "Visual Basic .Net For Access Databases"

    Code:
    ' Dim and set connection string above control code
    Dim cnn1 As System.Data.OleDb.OleDbConnection = connectToNorthwind()
    
    
       Function connectToNorthwind() As System.Data.OleDb.OleDbConnection
            Dim str1 As String = ("Provider=Microsoft.Jet.OLEDB.4.0;")
            str1 += ("Data Source=C:\Visual Studio Projects\Access2k\Northwind.mdb")
            Dim cnn1 As New System.Data.OleDb.OleDbConnection
            cnn1.ConnectionString = str1
            Return cnn1
        End Function
    Now you should be able to use it from any control. I think you could use a variation on that for any type database / situation.

  4. #4
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    if you connect manualy the connection will stay open till you close it manualy ( like the exp above ), but if your using a dataadapter then the dataadapter will open and close the connection when needed

  5. #5
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    that's not completely true persianboy...with .NET's built in GC...any variables or references to variables that go out of scope with be collected by the GC and destroyed...so you can manually open the connection, but you don't have to manually close it, because one the connection loses it references, it is no longer in scope and the GC will come along and destroy it.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  6. #6
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    but when the connection is active , there is no reason for the GC to collect it, so it will stay open while the program is running. plz tell me if i got anything wrong, thanks

  7. #7
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    you still have to take in to consideration the connection timeout when the connection is idle. If the connection hasn't been referenced (used) within a certain amount of time it will release the references and destroy the connection.

    Why not just use connection pooling?
    Last edited by Memnoch1207; Oct 20th, 2003 at 10:53 AM.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

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