开发者

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开发者_如何学Go 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜