Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, March 24, 2017 11:03 AM
I want to terminate or free all the resources or connections(especially database connection) when the project is terminated or close. I know form_closing event, but in my project I am externally CLOSE other forms when I am working on main form, so freeing the resources in form_closing event cannot will generate errors for me .
I want to know an alternative way to close the connections of database.
Can anyone please help me to solve this problem?
Thank you in advance.
All replies (6)
Saturday, March 25, 2017 2:44 AM âś…Answered
Can you please give me a sample code for closing connection during application exit using Application.ApplicationExit .
The sample code is in the link that was provided:
https://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit(v=vs.110).aspx
' Handle the ApplicationExit event to know when the application is exiting.
AddHandler Application.ApplicationExit, AddressOf OnApplicationExit
...
Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
' When the application is exiting, close the database connection
cn.close
cn = Nothing
End Sub
Friday, March 24, 2017 11:41 AM
I want to know an alternative way to close the connections of database.
Take a look at the events in the Application class - that enables you to do at the application level what you might otherwise do at the form level. See:
https://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit(v=vs.110).aspx
Friday, March 24, 2017 11:54 AM
There is an old myth that keeping your connections open as long as a program runs is best practice.
However, in this century there is always advised to open and close your data connection as soon as your transactions are done.
For instance the DataAdapter has standard (if you don't use the open separate) an open and close in it.
The using keyword is made also to do that. End using a connection closes that connection.
And now you ask: What to do it if I do it in a non advised way.
Like asking; 'What to do if I drive in a road with this road sign in the beginning and I see a policeman?".
Are you not with me afraid that others will think that the way you do is normal?
Success
Cor
Friday, March 24, 2017 12:08 PM
I think that Cor is pretty much correct on this. You should not maintain persistent connections to the database. Open and close connections as you need them. Connection resource pooling will take care of the rest.
Paul ~~~~ Microsoft MVP (Visual Basic)
Friday, March 24, 2017 9:40 PM
Take a look at the events in the Application class - that enables you to do at the application level what you might otherwise do at the form level. See:
https://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit(v=vs.110).aspx
Thank you for your response!!!
I went through the link which you gave, and it was really helpful.
When my application exits , I just want 2 lines of code to execute, ie.,
cn.close
cn=nothing
This code will close my connection. I just know basics of VB.net and Visual studio, so don't know to write code for Application.ApplicationExit event handler, and also in which form should we write this event handler.
Can you please give me a sample code for closing connection during application exit using Application.ApplicationExit .
Please it is a request.
Waiting for your response
Thank you in advance
Saturday, March 25, 2017 5:18 AM
Thank you