<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Guidelines to connect Db &#8211; Programming blog &#8211; website programming blog, blog on website programming .net, java , php and mor</title>
	<atom:link href="https://www.searchenginegenie.com/programming-blog/tag/guidelines-to-connect-db/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.searchenginegenie.com/programming-blog</link>
	<description></description>
	<lastBuildDate>Fri, 21 Sep 2012 10:26:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.5</generator>
	<item>
		<title>Guidelines to handle Db connection</title>
		<link>https://www.searchenginegenie.com/programming-blog/guidelines-to-handle-db-connection/</link>
					<comments>https://www.searchenginegenie.com/programming-blog/guidelines-to-handle-db-connection/#respond</comments>
		
		<dc:creator><![CDATA[Camilla]]></dc:creator>
		<pubDate>Fri, 10 Oct 2008 11:24:00 +0000</pubDate>
				<category><![CDATA[Programmer]]></category>
		<category><![CDATA[Guidelines to connect Db]]></category>
		<guid isPermaLink="false">http://www.searchenginegenie.com/programming-blog/guidelines-to-handle-db-connection/</guid>

					<description><![CDATA[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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>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:</p>
<p>1. Use web.config to store up the connection string:</p>
<p>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:<br />
string dsn = System.Configuration.ConfigurationSettings.AppSettings[&#8220;dsn&#8221;];</p>
<p>3. Do not perform any tricks to try to deal with connections etc, just proceed and use it (no wrapper classes or so involved):<br />
SqlConnection conn = new SqlConnection(dsn);</p>
<p>4. Formulate the time between conn.Open() and conn.Close() as small as possible. E.g. don&#8217;t do this:<br />
SqlConnection conn = new SqlConnection(dsn);<br />
Conn.Open();<br />
SqlCommand cmd = new SqlCommand(query, conn);<br />
cmd.Parameters.Add(&#8230;);</p>
<p>//and further cmd parameters actions and so on</p>
<p>SqlDataReader reader = cmd.ExecuteReader();</p>
<p>//use the reader and do a bundle of additional tasks, not relying on the database</p>
<p>conn.Close();</p>
<p>But rather, use:<br />
SqlConnection conn = new SqlConnection(dsn);<br />
SqlCommand cmd = new SqlCommand(query, conn);<br />
cmd.Parameters.Add(&#8230;);</p>
<p>//and more cmd parameters actions etc</p>
<p>conn.Open();<br />
SqlDataReader reader = cmd.ExecuteReader();</p>
<p>//use the reader<br />
conn.Close();<br />
//perform a batch of other work, not relying on the database</p>
<p>5. Always close the connection when you are done, even when an exception occurs. To do this, use the try&#8230;finally pattern:<br />
SqlConnection conn = new SqlConnection(dsn);<br />
try<br />
{<br />
//some init depending on conn<br />
conn.Open();<br />
//minimum number of lines of code depending on the open connection<br />
}<br />
finally<br />
{<br />
if (conn.ConnectionState == ConnectionState.Open)<br />
conn.Close();<br />
}<br />
or the using pattern in C#:<br />
using (SqlConnection conn = new SqlConnection(dsn))<br />
{<br />
//&#8230;<br />
conn.Open();<br />
}<br />
This will close the db connection repeatedly while leave from the using block.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.searchenginegenie.com/programming-blog/guidelines-to-handle-db-connection/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: www.searchenginegenie.com @ 2024-06-26 09:13:24 by W3 Total Cache
-->