Don't open a connection in Global.asax file
Created connection in data layer
Open connection, access data and close connection
Thats the best practice
CHeers,
Sansanwal
Main Topics
Browse All TopicsGreetings;
I understand there is a way to connect to a SQL database within the Application_Start section of the Global.asax file, however, I have not found a good example of this. I am looking for a sample connect and subsequent reference to the connect object name from VB page behind.
Please let me know if I'm on the wrong track in my thinking or if this is a commonly done coding practice and an example of how to do it showing the Global.asax code and a sample snippet of VB code in the code-behind for a page.
Much thanks ... David
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thank you greenguy and sansanwal.
To answer greenguy's question -- I am looking for a method to connect to the database one time when the application is opened and re-use the connection throughout the life of the application (which I understand ends when the web server is brought down or re-cycled). The solution I'm looking for is to minimize the number of times connections are created, opened, closed then disposed of on the basis the less this occurs the better performance will be.
To comment on sansanwal comment -- when you mention "data layer" I do have separate projects within the solution (Visual Studio .NET) which are classes (as opposed to web applications) that I interact with from the web application to facilitate connecting to the database, issuing a command object and loading a dataset. The dataset object I define in the web application, then pass reference to the dataset to the class, fill the dataset within the class and return control back to the web application which can then utilize the filled dataset. Is this what you mean by handling database functions in the data layer?
Much thanks ... David
I think you(david) elaborate right in sansanwal's comment. You should write dataaccess layer through class libraries and reference its dll in your UI project.
and for performance of connection, you can also use connection pooling, you can get more idea about that by visiting following
http://www.15seconds.com/i
you can implement connection pooling in data access layer.
Veriman - I believe that it is not possible to reuse a single open connection in your application.
Keep in mind that the DataAdapter.Fill method calls a Connection.Open as well as a Connection.Close behind the scenes for you, so even if you could theoretically reuse the same connection - every time you fill a dataset, it would close the connection.
However, I also don't think that you have to be worried too much about performance with regards to opening and closing connections, since ASP.NET enables connection pooling automatically.
The trick to using connection pooling is to use the exact same connection string every time. You can do this by storing the connection string in AppliationState or in the web.config file.
As such, connection pooling will only work if you are connecting to the database with the same userID and password. If you are using Impersonation, for example, your performance may decrease since pooling will not occur across users.
Is your performance an issue right now?
Business Accounts
Answer for Membership
by: greenguyPosted on 2005-08-25 at 18:39:58ID: 14757962
I'd be happy to answer your question - but first I wanted to clarify what you are trying to accomplish -
Do you need to load values from the database at Application_Start?
Are you just looking for a way to write code that you can reuse throughout your application?