<?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>things &#38; stuff &#187; ASP.Net</title>
	<atom:link href="http://www.curlybrace.com/words/category/technology/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.curlybrace.com/words</link>
	<description>Pictures.  Now with more words!</description>
	<lastBuildDate>Mon, 05 Jul 2010 21:47:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Unable to Connect to Localhost SQL Server Express Instance</title>
		<link>http://www.curlybrace.com/words/2008/10/14/unable-to-connect-to-localhost-sql-server-express-instance/</link>
		<comments>http://www.curlybrace.com/words/2008/10/14/unable-to-connect-to-localhost-sql-server-express-instance/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 20:17:57 +0000</pubDate>
		<dc:creator>Jeff Fitzsimons</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[ADO]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Express]]></category>

		<guid isPermaLink="false">http://www.curlybrace.com/words/?p=380</guid>
		<description><![CDATA[Problem Connecting programmatically or by using osql failed with the following, generic error: error: 40 - Could not open a connection to SQL Server My programmatic attempt looked like this: string cs = "Data Source=localhost;" + "Trusted_Connection=true;" + "Initial Catalog=dbname;"; SqlConnection connection = new SqlConnection(cs); connection.Open(); Other variants I tried for Data Source were (local), [...]]]></description>
			<content:encoded><![CDATA[<h3>Problem</h3>
<p>Connecting programmatically or by using <tt>osql</tt> failed with the following, generic error:</p>
<blockquote><p><tt>error: 40 - Could not open a connection to SQL Server</tt></p></blockquote>
<p>My programmatic attempt looked like this:</p>
<blockquote><pre>string cs = "Data Source=localhost;" +
            "Trusted_Connection=true;" +
            "Initial Catalog=dbname;";
SqlConnection connection = new SqlConnection(cs);
connection.Open();</pre>
</blockquote>
<p>Other variants I tried for <tt>Data Source</tt> were <tt>(local)</tt>, <tt>127.0.0.1</tt>, and the fully-qualified name of my local server.</p>
<p>My <tt>osql</tt> attempt looked like this:</p>
<blockquote><pre>c:\>osql -E -S localhost</pre>
</blockquote>
<h3>Solution</h3>
<p>The solution, at least for the basic connection problem, turned out to be a missing instance name.  The instance name can be found by looking in Programs | Microsoft SQL Server 2005 | Configuration Tools | SQL Server Configuration Manager, under SQL Server 2005 Services.  In my case, the instance is named &#8220;<tt>SQLEXPRESS</tt>&#8220;, so I tacked this onto my connection strings for a (more) successful login.</p>
<p>Programmatic:</p>
<blockquote><pre>string cs = "Data Source=localhost\\sqlexpress;" +
            "Trusted_Connection=true;" +
            "Initial Catalog=dbname;";
SqlConnection connection = new SqlConnection(cs);
connection.Open();</pre>
</blockquote>
<p><tt>osql</tt>:</p>
<blockquote><pre>c:\>osql -E -S localhost\sqlexpress
1> </pre>
</blockquote>
<h3>Authorization</h3>
<p>Actually, the above code didn&#8217;t quite work.  I still get an authorization error:</p>
<blockquote><pre>Cannot open database "dbname" requested by the login. The login failed.
Login failed for user 'DOMAIN\username'.</pre>
</blockquote>
<p>This was actually because I hadn&#8217;t yet created the database, nor a user!  I followed <a href="https://secure.codeproject.com/KB/database/ConfigureMSDE.aspx?display=PrintAll&#038;fid=126703&#038;df=90&#038;mpp=25&#038;noise=3&#038;sort=Position&#038;view=Quick&#038;fr=76&#038;select=985953">this handy guide</a> in order to create a database, a user, and grant the user access to the new database.</p>
<p>In the end, my connection code looks like this:</p>
<blockquote><pre>string cs = "Data Source=localhost\\sqlexpress;" +
            "Trusted_Connection=true" +
            "Initial Catalog=dbname;" +
            "user id=username;password=userpass;";
SqlConnection connection = new SqlConnection(cs);
connection.Open();</pre>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.curlybrace.com/words/2008/10/14/unable-to-connect-to-localhost-sql-server-express-instance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
