1

I am trying to connect to a local host sql server 2008 express server. I am running my c# asp.net code locally as well (testing before I connect to dev servers). I cannot get the code to connect to the database. I copied the connection string from the properties of the database created within visual studio 2010, and tried using that, but it did not work. Then I used:

//Build the connection 
    SqlConnectionStringBuilder bldr = new SqlConnectionStringBuilder();

    //Put your server or server\instance name here.  Likely YourComputerName\SQLExpress
    bldr.DataSource = "(localhost)/SQLEXPRESS;";

    //Attach DB Filename
    bldr.AttachDBFilename = "C:/Documents and Settings/1091912/My Documents/Visual Studio 2010/WebSites/BrokerBuy/App_Data/BrokerBuy.mdf";

    //User Instance
    bldr.UserInstance = true;

    //Whether or not a password is required.
    bldr.IntegratedSecurity = true;

    SqlConnection connectionString = new SqlConnection(bldr.ConnectionString);
    connectionString.Open();

But this does not work either. I have also tried ./SQLEXPRESS for my datasource name, but this did not work either. The error that comes up is :

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"

I have also checked to ensure sql server express is running (it is) and that all connections but via are enabled. I cannot get the connection to work. Any ideas?

9
  • bldr.DataSource = "(local)/SQLEXpress;"; maybe? Commented Dec 8, 2011 at 19:09
  • No, unfortunately I have the same issue... Commented Dec 8, 2011 at 19:12
  • You can try .\\SQLEXPRESS too Commented Dec 8, 2011 at 19:14
  • Ok, I used '.\\SQLEXPRESS' and it seems to have gotten connected, but now i have this error: An attempt to attach an auto-named database for file C:/Documents and Settings/1091912/My Documents/Visual Studio 2010/WebSites/BrokerBuy/App_Data/BrokerBuy.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. Commented Dec 8, 2011 at 19:19
  • The file exists? Also, only recent versions of Windows support forward slashes in pathnames, maybe try backslashes just in case? Commented Dec 8, 2011 at 19:25

2 Answers 2

4

Your DataSource is incorrect:

"(localhost)/SQLEXPRESS;";

It should either be (local)\SQLEXPRESS or localhost\SQLEXPRESS. You can refer to this MSDN Blog Post for more information. Also, traditionally it's a backslash, not a slash (so make sure it's escaped if you need to).

(local), including the parens is a special indicator meaning local machine.

localhost is the network name for the local machine.

You can use either one to connect to the local instance.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I used .\\SQLEXPRESS and it seems to have gotten connected, but now i have this error: An attempt to attach an auto-named database for file C:/Documents and Settings/1091912/My Documents/Visual Studio 2010/WebSites/BrokerBuy/App_Data/BrokerBuy.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
+1 he solved your first problem which is enough I think to give him a +1. Your second comment should probably be another question.
1

There's a few problems with your code.

The first is (localhost) is not a valid token. You can use (local) or even better yet just ..

The second is your path is using forward-slashes instead of backslashes. Though newer versions of Windows support this for *NIX compatibility, the database drivers may or may not depending on how they parse the path internally.

Here's some example code that should work:

//Build the connection 
SqlConnectionStringBuilder bldr = new SqlConnectionStringBuilder();

//Put your server or server\instance name here.  Likely YourComputerName\SQLExpress
bldr.DataSource = ".\\SQLEXPRESS";

//Attach DB Filename
bldr.AttachDBFilename = bldr.AttachDBFilename = @"C:\Documents and Settings\1091912\My Documents\Visual Studio 2010\WebSites\BrokerBuy\App_Data\BrokerBuy.mdf";

//User Instance
bldr.UserInstance = true;

//Whether or not a password is required.
bldr.IntegratedSecurity = true;

SqlConnection connectionString = new SqlConnection(bldr.ConnectionString);
connectionString.Open();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.