search engine optimization   Programming blog
        SEO        Link Building        Services        Genie Magic        Web Design        Contact Us        SEO Tools
 
   
Guidelines to handle Db connection, Friday, October 10, 2008
   
 

Please confirm that you close the original connection to a database all the time and that you are using connection pooling to get better processing speed of database connection creation etc. Here are some tips for you:

1. Use web.config to store up the connection string:

2. Use this connection string forever and do not make any changes to it. If you were not doing this (for example using different DSNs) you will disable the connection pooling feature:
string dsn = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

3. Do not perform any tricks to try to deal with connections etc, just proceed and use it (no wrapper classes or so involved):
SqlConnection conn = new SqlConnection(dsn);

4. Formulate the time between conn.Open() and conn.Close() as small as possible. E.g. don't do this:
SqlConnection conn = new SqlConnection(dsn);
Conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(...);

//and further cmd parameters actions and so on

SqlDataReader reader = cmd.ExecuteReader();

//use the reader and do a bundle of additional tasks, not relying on the database

conn.Close();

But rather, use:
SqlConnection conn = new SqlConnection(dsn);
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(...);

//and more cmd parameters actions etc

conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

//use the reader
conn.Close();
//perform a batch of other work, not relying on the database

5. Always close the connection when you are done, even when an exception occurs. To do this, use the try...finally pattern:
SqlConnection conn = new SqlConnection(dsn);
try
{
//some init depending on conn
conn.Open();
//minimum number of lines of code depending on the open connection
}
finally
{
if (conn.ConnectionState == ConnectionState.Open)
conn.Close();
}
or the using pattern in C#:
using (SqlConnection conn = new SqlConnection(dsn))
{
//...
conn.Open();
}
This will close the db connection repeatedly while leave from the using block.

                                Earthlink Netscape Netvouz RawSugar Shadows Sphinn StumbleUpon Yahoo MyWeb

 
   
 

posted by power @ 4:24 AM permanent link   |

Post a Comment

|

 

1 Comments:

Home

 

Categories
 

 
Archives
 
Previous posts
 
 
PageRank 10 sites
Search Engine Optimization SEO Blog
Search Engine
Optimization SEO News
SEO Copywriting Blog
Web Design Blog
Link Building Blog
Pay Per Click (PPC) Blog
Programming Blog
Search EngineGenie Blog
Lara's Personnal Blog
Search Engine
Optimization SEO Forum
SEO Comics
Webmaster & Search Events
 
search engine optimization
search engine marketing
SEO consulting
SEO plans
SEO services USA
search engine optimization SEO forum
SEO comics
Webmaster & Search Events
SEO Faqs
link popularity
strategies of link building
link building services
link cost
link request quote
link building blog
search engine genie company
our team
our celebrations
our experience
SEO
why us

web design
web designing services
dynamic website
web design and marketing
simple e-commerce website
complex e-commerce website
search engine friendly site
web design blog
web design
link building
internet marketing
ecommerce implementation
pay per click services
shopping feeds optimization
shopping cart customization
product development
online forms & database integration
programming services
PHP programming services
programming services Java,J2EE
.NET application development programming services
business process outsourcing
offshore outsourcing
Google Products(froogle feeds)
search engine optimization articles
google articles
yahoo articles
miscellenous articles
search engine optimization SEO blog
search engine optimization SEO news
SEO copywriting blog
web design blog
link building blog
pay per click (PPC) blog
programming blog
lara personnal blog
Search Engine Genie Blog
Google Tools
Yahoo Tools
MSN Tools
Comparison Tools
Link Popularity Tools
Search Engines Tools
Site Tools
Keyword tools

contact us
support
our guarantee
events