简体   繁体   中英

Error when connecting to .mdf from ASP.NET (C#)

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM