Database connection per page or per action?

Results 1 to 2 of 2

Thread: Database connection per page or per action?

  1. #1
    Join Date
    Dec 1969

    Default Database connection per page or per action?

    Hi,<BR><BR>I need to connect to my database on almost every page of my application. At the moment I create a connection object for every stored proc I run on a page and retrieve the string from my web.config.<BR><BR>Now I read, that I can check the cache for the existence of the connection string rather than having to IO the web.config every time.<BR><BR>On my classic asp pages, I opened the db-connection on top of the scripting area via include file, used it on every stored proc I had and closed it after that. Is something similar possible with VB.NET,too? Does it make sense this way or is there another way, I just did not discover, yet.<BR><BR>TIA<BR>Thomas

  2. #2
    Join Date
    Dec 1969

    Default RE: Database connection per page or per action?

    General rule is open late, close early.<BR><BR>You only have to look in the web.config to get the connection string once per page tho&#039;. Define the connection string with scope for the whole of the class - then all methods used later can use it:<BR><BR>Imports System.Collections.Specialized<BR><BR>Public Class YourClass<BR>Dim nvc As NameValueCollection = CType(Context.GetConfig("appSettings"), NameValueCollection)<BR> Dim strConnect As String = CStr(nvc("SQLconnStr"))<BR><BR>However the best way to do this type of thing is to use the microsoft application building blocks - these abstract out your database code and you end up not having to write the same database access code everytime you want to hit the database (perhaps just changing the stored proc name or whateber)<BR><BR>I would really recommend these two articles - this one is a Best Practices guide for ADO.NET - <BR><BR><BR>and this one describes the application buildinmg blocks<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts