{"id":380,"date":"2008-10-14T12:17:57","date_gmt":"2008-10-14T20:17:57","guid":{"rendered":"http:\/\/www.curlybrace.com\/words\/?p=380"},"modified":"2008-10-14T12:17:57","modified_gmt":"2008-10-14T20:17:57","slug":"unable-to-connect-to-localhost-sql-server-express-instance","status":"publish","type":"post","link":"https:\/\/www.curlybrace.com\/words\/2008\/10\/unable-to-connect-to-localhost-sql-server-express-instance\/","title":{"rendered":"Unable to Connect to Localhost SQL Server Express Instance"},"content":{"rendered":"<h3>Problem<\/h3>\n<p>Connecting programmatically or by using <tt>osql<\/tt> failed with the following, generic error:<\/p>\n<blockquote><p><tt>error: 40 - Could not open a connection to SQL Server<\/tt><\/p><\/blockquote>\n<p>My programmatic attempt looked like this:<\/p>\n<blockquote>\n<pre>string cs = \"Data Source=localhost;\" +\r\n            \"Trusted_Connection=true;\" +\r\n            \"Initial Catalog=dbname;\";\r\nSqlConnection connection = new SqlConnection(cs); \r\nconnection.Open();<\/pre>\n<\/blockquote>\n<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>\n<p>My <tt>osql<\/tt> attempt looked like this:<\/p>\n<blockquote>\n<pre>c:\\>osql -E -S localhost<\/pre>\n<\/blockquote>\n<h3>Solution<\/h3>\n<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>\n<p>Programmatic:<\/p>\n<blockquote>\n<pre>string cs = \"Data Source=localhost\\\\sqlexpress;\" +\r\n            \"Trusted_Connection=true;\" +\r\n            \"Initial Catalog=dbname;\";\r\nSqlConnection connection = new SqlConnection(cs);\r\nconnection.Open();<\/pre>\n<\/blockquote>\n<p><tt>osql<\/tt>:<\/p>\n<blockquote>\n<pre>c:\\>osql -E -S localhost\\sqlexpress\r\n1> <\/pre>\n<\/blockquote>\n<h3>Authorization<\/h3>\n<p>Actually, the above code didn&#8217;t quite work.  I still get an authorization error:<\/p>\n<blockquote>\n<pre>Cannot open database \"dbname\" requested by the login. The login failed.\r\nLogin failed for user 'DOMAIN\\username'.<\/pre>\n<\/blockquote>\n<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>\n<p>In the end, my connection code looks like this:<\/p>\n<blockquote>\n<pre>string cs = \"Data Source=localhost\\\\sqlexpress;\" +\r\n            \"Trusted_Connection=true\" +\r\n            \"Initial Catalog=dbname;\" +\r\n            \"user id=username;password=userpass;\";\r\nSqlConnection connection = new SqlConnection(cs);\r\nconnection.Open();<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Problem Connecting programmatically or by using osql failed with the following, generic error: error: 40 &#8211; Could not open a connection to SQL Server My programmatic attempt looked like this: string cs = &#8220;Data Source=localhost;&#8221; + &#8220;Trusted_Connection=true;&#8221; + &#8220;Initial Catalog=dbname;&#8221;; &hellip; <a href=\"https:\/\/www.curlybrace.com\/words\/2008\/10\/unable-to-connect-to-localhost-sql-server-express-instance\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[194,15],"tags":[195,307,177,196,197],"class_list":["post-380","post","type-post","status-publish","format-standard","hentry","category-aspnet","category-technology","tag-ado","tag-aspnet","tag-c","tag-sql-server","tag-sql-server-express"],"_links":{"self":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/comments?post=380"}],"version-history":[{"count":17,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/380\/revisions"}],"predecessor-version":[{"id":397,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/380\/revisions\/397"}],"wp:attachment":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/media?parent=380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/categories?post=380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/tags?post=380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}