Close a Database Connection

nawal

Member
Joined
Oct 17, 2010
Messages
6
Programming Experience
3-5
Hello to you all :)
I am new here and I wish to learn a lot from this forum

I have a question:
I created a VB.NET Windows Forms app using VS 2008.
My application connects to an Oracle database server.

I created the database connection using the Wizard: Data -> Add new Data Source
and I connected successfully and I developed my app

At the end, I want to close the connection when I exit from the app
So, from the Server Explorer, I got the name of the connection : ofdmp.Simah
But when I tried to type: ofdmp.Simah.close() in the code Editor, I got the error: Name ofdmp is not declared !!!

Can any one tell me why is this happening? and How to solve the issue ?

Thanks
 
There is no connection close. Basically, just do nothing and you're fine.

When you add a Data Source to your project the wizard will generate a typed DataSet, including TableAdapters. The TableAdapters are the bridge between your local data store, i.e. the DataSet and its DataTables, and the database. When you want to get data from the database into a DataTable or send changes from a DataTable back to the database, you create an appropriate TableAdapter and call Fill or Update. That's it. Internally, the TableAdapter opens a connection, moves the data and then closes the connection again. That's how ADO.NET is designed to work: open the connection as late as possible, do just the work that needs to be done and then close the connection as soon as possible.
 
Thanks jmcilhinney
If the connection will be closed automatically, that's great

I thought it will be opened forever and this will cause a load into the database server
Because when I see the code samples that define a connection and do the work, finally they close the connection for that reason.

Thanks again
 
Those code samples that close a connection would have opened that connection in the first place, e.g.
VB.NET:
connection.Open()

Dim reader As New SqlDataReader = command.ExecuteReader()

Do While reader.Read()
    '...
End While

reader.Close()
connection.Close()
Like I said, open the connection as late as possible, do your work and then close the connection as early as possible. When you call Fill or Update on a TableAdapter, code similar to what's above is execute internally.
 
Back
Top