<?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>Nicolas is blogging &#187; Development</title>
	<atom:link href="http://blog.mastertheteam.net/tag/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mastertheteam.net</link>
	<description>Jand writes about team development and Microsoft.NET</description>
	<lastBuildDate>Fri, 24 Jul 2009 14:19:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Share ConnectionStrings between projects for each developer differently</title>
		<link>http://blog.mastertheteam.net/2008/10/share-connectionstrings-between-projects-for-each-developer-differently/</link>
		<comments>http://blog.mastertheteam.net/2008/10/share-connectionstrings-between-projects-for-each-developer-differently/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 09:24:54 +0000</pubDate>
		<dc:creator>Nicolas Mueggler</dc:creator>
				<category><![CDATA[Work]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://blog.mastertheteam.net/?p=33</guid>
		<description><![CDATA[Share ConnectionStrings
A customer of mine have a Visual Studio C# solution with more than one project which uses the same ConnectionString. So how can you define your ConnectionString(s) once and share them between projects?
This is how my sample solution looks like. The &#8220;Console&#8221; and &#8220;ServerConsole&#8221; projects should use the same ConnectionStrings.

I created a XML file ]]></description>
			<content:encoded><![CDATA[<p><strong>Share ConnectionStrings</strong></p>
<p>A customer of mine have a Visual Studio C# solution with more than one project which uses the same ConnectionString. So how can you define your ConnectionString(s) once and share them between projects?<br />
This is how my sample solution looks like. The &#8220;Console&#8221; and &#8220;ServerConsole&#8221; projects should use the same ConnectionStrings.</p>
<p><a href="http://blog.trivadis.com/blogs/nicolasmueggler/WindowsLiveWriter/ShareConnectionStringsbetweenprojectsfor_1121/image_2.png"><img style="border-width: 0px;" src="http://blog.trivadis.com/blogs/nicolasmueggler/WindowsLiveWriter/ShareConnectionStringsbetweenprojectsfor_1121/image_thumb.png" border="0" alt="image" width="244" height="116" /></a></p>
<p>I created a XML file called &#8220;connections.config&#8221; in the solution root folder and added it to the solution via the &#8220;Add Existing Item&#8221; dialog. This file contains all my ConnectionStrings I need.</p>
<p>Content of connections.config:</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>&lt;?xml version=<span class="str">"1.0"</span> encoding=<span class="str">"utf-8"</span>?&gt;</pre>
<pre><span class="lnum">   2:  </span>&lt;connectionStrings&gt;</pre>
<pre class="alt"><span class="lnum">   3:  </span>  &lt;add name=<span class="str">"DefaultDatabase"</span></pre>
<pre><span class="lnum">   4:  </span>   providerName=<span class="str">"System.Data.SqlClient"</span></pre>
<pre class="alt"><span class="lnum">   5:  </span>   connectionString=<span class="str">"Data Source=.\SQLEXPRESS;
             Initial Catalog=ShareConnection;
             Integrated Security=True"</span> /&gt;</pre>
<pre><span class="lnum">   6:  </span>&lt;/connectionStrings&gt;</pre>
</div>
<p>After that, I add the file to all projects, which are needing the same connections. You need to add the file as &#8220;Add As Link&#8221; to ensure using the file in the shared location.</p>
<p><a href="http://blog.trivadis.com/blogs/nicolasmueggler/WindowsLiveWriter/ShareConnectionStringsbetweenprojectsfor_1121/image_6.png"><img style="width: 568px; height: 389px; border-width: 0px;" src="http://blog.trivadis.com/blogs/nicolasmueggler/WindowsLiveWriter/ShareConnectionStringsbetweenprojectsfor_1121/image_thumb_2.png" border="0" alt="image" width="644" height="463" /></a></p>
<p>Now you see the file in your project with a little arrow on the left bottom (linked file).</p>
<p><a href="http://blog.trivadis.com/blogs/nicolasmueggler/WindowsLiveWriter/ShareConnectionStringsbetweenprojectsfor_1121/image_4.png"><img style="border-width: 0px;" src="http://blog.trivadis.com/blogs/nicolasmueggler/WindowsLiveWriter/ShareConnectionStringsbetweenprojectsfor_1121/image_thumb_1.png" border="0" alt="image" width="241" height="103" /></a></p>
<p>To ensure the file is copied to the build output directory, you need to modify the &#8220;Copy to Output Directory&#8221; setting of the linked file to &#8220;Copy Always&#8221;.</p>
<p><a href="http://blog.trivadis.com/blogs/nicolasmueggler/WindowsLiveWriter/ShareConnectionStringsbetweenprojectsfor_1121/image_8.png"><img style="border-width: 0px;" src="http://blog.trivadis.com/blogs/nicolasmueggler/WindowsLiveWriter/ShareConnectionStringsbetweenprojectsfor_1121/image_thumb_3.png" border="0" alt="image" width="244" height="165" /></a></p>
<p>Now write the following &#8220;connectionStrings&#8221; section in the &#8220;App.config&#8221; of your projects:</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>&lt;?xml version=<span class="str">"1.0"</span> encoding=<span class="str">"utf-8"</span> ?&gt;</pre>
<pre><span class="lnum">   2:  </span>&lt;configuration&gt;</pre>
<pre class="alt"><span class="lnum">   3:  </span>  &lt;appSettings&gt;</pre>
<pre><span class="lnum">   4:  </span>    &lt;add key=<span class="str">"SpecificProjSetting"</span> <span class="kwrd">value</span>=<span class="str">"SomeValue"</span> /&gt;</pre>
<pre class="alt"><span class="lnum">   5:  </span>  &lt;/appSettings&gt;</pre>
<pre><span class="lnum">   6:  </span>  &lt;connectionStrings configSource=<span class="str">"connections.config"</span> /&gt;</pre>
<pre class="alt"><span class="lnum">   7:  </span>&lt;/configuration&gt;</pre>
</div>
<p>And that&#8217;s all. If you now write your code, you always get the shared ConnectionString e.g.:</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">string</span> GetDbConnection()</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">return</span> ConfigurationManager.ConnectionStrings[<span class="str">"DefaultDatabase"</span>].ConnectionString;</pre>
<pre><span class="lnum">   4:  </span>}</pre>
</div>
<p><strong>Individual ConnectionStrings for each developer</strong></p>
<p>My second requirements, enabling each developer to have his own ConnectionStrings (they have different Instance-Names), needs only a small addition.</p>
<p>In addition to the &#8220;connections.config&#8221; file which is under Source Control, each developer can create his own file at the same place with the convention &#8220;connections.config.{COMPUTERNAME}&#8221; which will not be checked in. In my example my computer&#8217;s name is &#8220;NIM-50&#8243;, so the file is named as &#8220;connections.config.NIM-50&#8243;.</p>
<p>Edit now each project&#8217;s (Pre-Build/Post/Build) property in the &#8220;Build Events&#8221; tab with the following command lines.</p>
<p>Pre-build event command line:<br />
<em>if exist $(TargetDir)connections.config del $(TargetDir)connections.config</em></p>
<p>Post-build event command line:<br />
<em>if exist $(SolutionDir)connections.config.$(COMPUTERNAME) copy $(SolutionDir)connections.config.$(COMPUTERNAME) $(TargetDir)connections.config</em></p>
<p>The &#8220;Pre-Build&#8221; checks if a &#8220;connections.config&#8221; allready exists in the drop location and deletes it. If the &#8220;Pre-Build&#8221; wouldn&#8217;t be run, then the old (existing) file will not be overwritten!<br />
The &#8220;Post-Build&#8221; checks if a specific developer configuration is found and copy/rename it to the drop location. If no one exists, the standard shared config will be used as defined in the project&#8217;s include.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mastertheteam.net/2008/10/share-connectionstrings-between-projects-for-each-developer-differently/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
