Open and close the connections on the code behind file or class file is fast

Hi Friends,

Method1: I am opening, performing some task and closing the connections from the sql server, no of time based on requirements on the code behind file. 

Method2: Also I have gone through this post http://forums.asp.net/t/1193643.aspx and it is mentioned that fast and better performance we should open and close the connection on the class file.

Please let me know for the fast execution of the .net application which method should I chose 1 or 2

 

Thanks,

Farooq

 


Mark as answer if this post helpfull to you.

Don't be afraid to be wrong; otherwise you'll never be right.
0
susain
4/23/2009 5:18:24 AM
asp.net.object-datasource 16182 articles. 0 followers. Follow

1 Replies
582 Views

Similar Articles

[PageSpeed] 26

Technically, method 1 does indeed feature opening and closing connections within a Class file.  It's just that the class is an instance of the System.Web.UI.Page class.  The only reason you would create a separate class file to encapsulate your data access routines is to organise your code better.  If you application grows, and you open connections within pages, any change to the connection string could conceivably leave you having to alter all of your page class files.

From a performance point of view, the rule is simple: wherever you perform data access, whether that's in a spearate class, library or within the code-behind file, make sure you open the connection as late as you can, and close it immediately you are done with it.  You don't have to explicitly call the Close() method either.  If you put your data access code within a Using statement, it will be closed and disposed of automatically.Oh, and make sure that the connection string is identical whenever you use it. 

http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx

 


Regards Mike
[MVP - ASP/ASP.NET]
My site
0
Mikesdotnetting
4/23/2009 8:27:56 AM
Reply:

Similar Artilces:

How do you close a connection opened in a Class file?
Hi - if I return a datareader from a function in a class file, such as the Commerce Starter Kit: <code> Public Function GetProductCategories() As SqlDataReader ' Create Instance of Connection and Command Object Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) Dim myCommand As SqlCommand = New SqlCommand("ProductCategoryList", myConnection) ' Mark the Command as a SPROC myCommand.CommandType = CommandType.StoredProcedure ' Execute the command ...

declaring class objects in code behind files
I 'm trying to declare a class object in a code behind file.  The Product class is held in a separate file called product.cs.  The code behind file is held in another file order.aspx.cs.  For some reason order.aspx.cs does not recognize the declarartion of  the Product class. Here is some of the code from order.aspx.cs:   using System;using System.Data;using System.Web;using System.Configuration;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Co...

Pls post code to start/open an Excel file and then close the file
Pls post :- (1) Codes to start/open a particular Excel file (eg xyz.XLS in path c:\abc) and(2) Codes to save this XLS and(3) codes to close this XLS file preferably in VB. I want to just keep the Excel file opened and saved at the background (ie without notice to users. Thanks a lot.Thanks in advance for the help. I will try to credit the ones who helped but most important is we really do sincerely thanks to all who have helped. Please help !!!Thanks in advance for the help. I will try to credit the ones who helped but most important is we really do sincerely thanks ...

File open/lock Monitor on one file closes when user accesses this file
Hello, we are running a DBase 4.1.1 MS-DOS application at Netware OES 6.5 SP4 (Novell Small Business Suite Server). Sometimes we are experiencing a slow down of the application when the server CPU ha about 5% and the disks are also not running heavy. There is nothing to find in the network too. What we found today is, that when a user accesses ex. file.dbf and we open the "File open/lock activity" Monitor on this file, the monitor closes immediately. We can shortly see the users session (nolock) before it closes. Are there any hints we can follow to find the backgrou...

How to register a CSS file to the page from code in the code-behind file?
Hello, all! How to register a CSS file to the page from code in the code-behind file? Thank you.                                                                                  Marcos I a...

store a regular expression in config file or some other file and have a value associated to it, and use it in code behind file.
How do i store all the regular expressions in a configuration file and use them in code behind file. i.e i need to store a regular expressions with some value associated to it either in config file or any external file .   when i compare that particular regular espression with the input string it should return the value associated to it. ex:System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\d{1,}"); if (regex.IsMatch(input))return "integer"; here we are hard coding instead have to compare the regular expression t...

Object tag element opened with "Open File" -- Local File Security
Hi, I'm trying to open an html file with the object tag in it from a local file (file://) in Firefox 3.0 The html file is the following: <html> <body> <object type="audio/mpeg" data="media/where_to_begin.mp3" width="200" height="20"> <param name="src" value="media/where_to_begin.mp3" /> <param name="autoplay" value="false" /> alt : <a href="media/where_to_begin.mp3">media/where_to_begin.mp3</ a> </object> </body> <...

calling a javascript file in the code behind file
Hi All!I'm trying to call a javascript file into my default.aspx.cs. Can anyone tell that what I'm trying to do is possible or not. If possible then how can it be done? Thanking you in anticipation. Please remember to mark the replies as answers if they help and unmark them if they provide no help. Hi, I'm sorry but your question's not very clear to me. Can you please elaborate on it a bit more? Are you trying to insert code into the code behind that can call a javascript method in the browser once the webform got rendered there? Something like ClientScriptManag...

Opening file...replace text...close file
HI, I'm new to this list. I would like to know how I can open an existing file on the server (windows). Search for some text and once found replace it with something else and then save/close the file. Thanks --------------------------------------------------- Colonel Nathan R. Jessop Commanding Officer Marine Ground Forces Guatanamo Bay, Cuba --------------------------------------------------- FlashGuy wrote: >HI, > >I'm new to this list. I would like to know how I can open an existing file on the server (windows). Search for some text and ...

how to call javascript function written within js file from server side code (code behind file) ?
Hi, I have js file containing all javascript functions required for my page. I am using ScriptManager.RegisterStartupScript to call javascript from code behind file. But for this i must  specify all javascript code from code behind file only. For eg.  if i use Page.ClientScript.RegisterStartupScript(typeof(System.String), this.ClientID + "_Init", strScript); where strScript contains <script>alert("hi");</script>, on page_Init event, this script is called. But when same script is embeded within a function  and i try to give function name in ...

Accessing the VB File in the Bin from a Code Behind File
I hope I typed that correctly.  I have a VB file in the bin.  I want to use the functions in that file in another Codde Behind file.  For example, on my home page I have this at the top<%@ Page language="vb" AutoEventWireup="false" Inherits="MyHomePage" Src="MyHome.vb"%>   This works!  But inside of the MyHomePage.vb file I want to access the MyWebsite.vb file in the bin.  The NameSpace used in the MyWebsite.vb works when inheriting from the page level and looks like thisNamespace MySite.MyWebSite Public Class&nbs...

Codes to open WordTemplate files from a subfolder in an ASPX file
 I have WordTemplate folders and files in an ASPX file.  The WordTemplate files underthe Intranet folder directly have no problems to be opened.  However the files in the subfoldercan not.     With a msgbox, the href link seemed to be correct. Following are the codes from my Web form aspx file and VBScript classic asp file. Why the Word doc/dot files can not be opened?  TIA,Jeffrey *********************************************** Response.write line in my Forms.aspx: Response.Write("<a href='#' onClick=""w...

libnicm.so: cannot open shared object file: no such file or directory
I tried installing Nterprise Linux Services on SUSE SLES 9. But I cannot get iManager to come up on my browser. Restarting novell- httpd gave me the above error message. Can someone help with this? Leslie, It appears that in the past few days you have not received a response to your posting. That concerns us, and has triggered this automated reply. Has your problem been resolved? If not, you might try one of the following options: - Do a search of our knowledgebase at http://support.novell.com/search/kb_index.jsp - Check all of the other support tools and options ava...

Second code behind file to every .ascx file?
Why is there a second codebehind file to every .ascx file on my Vista and Visual Studio 2005 machine ? I have manually set up the dotnetnuke installation files but when adding a module I have used the module kit installed in the VS2005 as a template of some kind ... the templates that gets installed when installing the .vsi kit ... but then I get the file DesktopModules/ModuleName1/Settings.ascx and thats normal BUT behind that file there is 2 files: # Settings.ascx.designer.vb # Settings.ascx.vb What is this all about? What do you use the Settings.ascx.designer.vb file to ? /Jo...

Web resources about - Open and close the connections on the code behind file or class file is fast - asp.net.object-datasource

Connection - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

iMedia Connection: Interactive Marketing News, Features, Podcasts and Video - iMediaConnection.com
If you send more email, you might make more money. Then again, you might destroy your reputation and revenue stream. Here's how to know what ...

HTTP persistent connection - Wikipedia, the free encyclopedia
... tacked on to an existing protocol. If the browser supports keep-alive, it adds an additional header to the request: Following this, the connection ...

MOTHER’S DAY: Facebook Examines Connections Between Moms, Kids
With Mother’s Day on the calendar this coming Sunday, Facebook examined the relationships between mothers and their children on the social network, ...

Complex Regional Pain Syndrome: CPRS researchers study auditory connection
For 16 years Kerry Whyman ``secretly&quot; thought she had bone cancer and was dying secretly because she'd stopped telling doctors how much ...

Ben Garcia gives Penrith Panthers a new French connection
Should he jag a game in the NRL, Ben Garcia will become just the third genuine French import to do so.

Fictional Lawyers Use Their Family Connections Too
This film character had a family connection to his lawyer.

This Is How Facebook Is Working to Improve Experience On Slower Connections
... the globe, that’s not their situation . In recent years, Facebook has paid close attention to its user experience on 2G and other slow connections. ...

Addappt now lets you block contacts and connections
... gives you even more control than before. New features Block a current contact from connecting if they use Addappt down the road Block a connection ...

Facebook becomes more adept at dealing with crappy connections
... to get a decent phone signal to allow you to post a photo of your meal. Joking aside, in countries where people are struggling with 2G connections ...

Resources last updated: 12/14/2015 12:35:57 PM