views:

220

answers:

2

When trying to connect to a .mdf databse in ASP.NET (using c#) I am given this error:

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Does this mean my connection string is wrong? What should it be?

Aside from that, I am very new to ASP.NET. I am trying to connect to this database in the same way I would in c# normally (using data adapters and SqlCommands). Is this the right way to go about it, or is there a different way? When I started a new website adding user accounts worked (there was a wizard or something?) but I couldn't work out how to add more user information. What is the best way to connect to an SQL database and add user accounts with login and personal details in ASP.NET?

EDIT: My Connection String is data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Users.mdf;User Instance=true

A: 

If I read your connection string properly, you're never specifying a database to use!

data source=.\SQLEXPRESS;Integrated Security=SSPI;
  AttachDBFilename=|DataDirectory|\Users.mdf;User Instance=true

Add the database to the connection string - and also, loose the extra backslash after the |DataDirectory|:

server=.\SQLEXPRESS;database=YourDatabase;Integrated Security=SSPI;
  AttachDBFilename=|DataDirectory|Users.mdf;User Instance=true
marc_s
ok, now I have a new error :P `Unable to open the physical file "c:\users\...App_Data\Users.mdf". Operating system error 32: "32(failed to retrieve text for this error. Reason: 15105)".``Cannot attach the file 'c:\users\...Users.mdf' as database 'Users'.'`
Sir Graystar
Is that file really there? Do you have permissions to read that file?
marc_s
A: 

OK, I've fixed it. For reference for anyone else:

The correct connection string was given in the Web.config file. The connection string I needed was Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;User Instance=True

Sir Graystar
Clearly it's not what I thought it might have been then - I was about to post an answer! What changed for it to start working? I can't see a difference between the connection string here and the one in your original question.
PhilPursglove
Yeah, I don't quite know. Just checked web.config and pasted it in.
Sir Graystar