Sorry - my first answer was nonsense. You need to open the database in each content page. You could use an application level variable to hold a database connection, but I would advise against that. Everyone will share the same connection if you did that.
engcanada
Member
23 Points
147 Posts
Database open and close connections
May 06, 2012 09:57 PM|LINK
I have started working with razor cshtml pages.
1) Once I open the database in the Layout.cshtml page: var db = Database.Open("mydatabase");
Do I also need to open it in the other pages that refer to it?
Do I need to close the db similarly as I have been in my ASP pages?
db.Close(); doesn't seem to be closing the connection, because all references after it still work.
Regards
Mikesdotnett...
All-Star
154951 Points
19870 Posts
Moderator
MVP
Re: Database open and close connections
May 07, 2012 07:34 AM|LINK
No. Your content pages merge with the layout page so the database is available to them.
No. The WebPages runtime will take care of that for you.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
engcanada
Member
23 Points
147 Posts
Re: Database open and close connections
May 07, 2012 05:28 PM|LINK
Thank you,
If that is the case in my first question, why is it when I remove the open db line from the content page I get the follwoing error:
The name 'db' does not exist in the current context
Mikesdotnett...
All-Star
154951 Points
19870 Posts
Moderator
MVP
Re: Database open and close connections
May 07, 2012 06:12 PM|LINK
Sorry - my first answer was nonsense. You need to open the database in each content page. You could use an application level variable to hold a database connection, but I would advise against that. Everyone will share the same connection if you did that.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter