Results 1 to 12 of 12

Thread: Closing connection and datasets

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126

    Closing connection and datasets

    Would there be any reason why you would still need to close a connectection when creating a dataset. For some reasons my connections arent closing on the database. Though i was under the impression that you didnt need to close them.
    "All those who wonder are not lost" -j.r.r tolkien

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    Which client are you using & are you opening the connection yourself or are you leaving that to the adapter?

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Though i was under the impression that you didnt need to close them.
    And why exactly would you think that?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Post your code if you want and we'll help you get it straight, but if you opened it then you need to close it.

    *Kinda sounds like something a mother would say.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126
    Here is the method that i created im using the Provider OraOLEDB.Oracle.1; Though i plan to go to the oracle client eventually. I though you didnt have to close the connection because it open and closed automatically with the fill method is this not the case i added the close and the dispose code just now



    Public Function MakeDs(ByVal strSql As String, ByVal strTable As String) As DataSet

    Dim strConn As String = "DbConnection"
    Dim ds As New DataSet()
    Dim Conn As New OleDb.OleDbConnection(strConn)
    Dim adptDs As New OleDb.OleDbDataAdapter(strSql, Conn)
    Try

    adptDs.Fill(ds, strTable)

    Catch err As Exception
    Trace.Write(err.Message)
    End Try
    adptDs.Dispose()
    Conn.Close()
    Return ds

    End Function
    "All those who wonder are not lost" -j.r.r tolkien

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    .Fill doesnt disconnect the database. I think you mistook the fact that it fills a dataset which is disconnected.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126
    Wow that is a big mess up on my part. I think in a lot of code samples you dont see the close method on the connection being called.


    i grabed this from ms web site

    The Fill method retrieves the data from the data source using a SELECT statement. The IDbConnection object associated with the select command must be valid, but it does not need to be open. If the IDbConnection is closed before Fill is called, it is opened to retrieve data, then closed. If the connection is open before Fill is called, it remains open.


    http://msdn.microsoft.com/library/de...filltopic4.asp

    am i misunderstanding this it seems to say to me the connection if not opened opens and then closes after filling the dataset yes/no
    Last edited by robdotnet00; May 27th, 2003 at 01:42 PM.
    "All those who wonder are not lost" -j.r.r tolkien

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126
    So is my code the problem or is this my misunderstanding the dataadapter? Or could it be some otehr reason why users sessions arent being closed on teh database.
    "All those who wonder are not lost" -j.r.r tolkien

  9. #9
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Ok, if you are using a DataAdapter, you dont need to explicitly call the open method of the connection object. Once you call the Fill method of the DataAdapter, the DataAdapter will open the connection, fill the DataSet then close the connection.

    Now what you got from MS is basically saying that if you call

    conn.Open() then call da.Fill(ds, "mytable") the DataAdapter wont close the connection, you have to explicitly close it by calling conn.Close().

    Hope that helps.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126
    That is what I had believed however the connections are still staying open on the oracle server. And defintly not a good thing. Hopefully closing the connection again and disposing of the object does something Im thinking does this sound potentially like a soulution. Im also think that it might be a prob with mdac however v 2.7 is installed.
    "All those who wonder are not lost" -j.r.r tolkien

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126

    Question

    So what is the proper way to handle a dataadpter that makes a dataset do we make sure to close the connection again. Dispose of the dataadapter and set the connection to null and the dataadpter to null as well. And if we do this why do we do this?
    "All those who wonder are not lost" -j.r.r tolkien

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126
    I think potentially that the connection wasnt the problem with how the fill method works i think possible I wasnt calling the dispose method for the datadapter and this was occuring


    A Worst-Case Scenario
    Imagine a server component that uses a database connection and does not have a Dispose method. On a server with a large amount of memory, you might create and release many instances of the component without having much impact on free memory. In this case, garbage collection might not destroy the components for some time after the references to them are released.

    Eventually, all of the available database connections could be tied up by components that had been released but not destroyed. Even though the server had no shortage of memory, it might be unable to respond to user requests.

    ms-help://MS.VSCC/MS.MSDNQTR.2003JAN.1033/vbcon/html/vbconInitializationTerminationOfComponents.htm
    "All those who wonder are not lost" -j.r.r tolkien

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