简体   繁体   English

从ASP.NET(C#)连接到.mdf时出错

[英]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: 当尝试连接到ASP.NET中的.mdf数据库(使用c#)时,出现以下错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.The server was not found or was not accessible. 建立与SQL Server的连接时发生与网络有关或特定于实例的错误。找不到或无法访问该服务器。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

Does this mean my connection string is wrong? 这是否表示我的连接字符串错误? What should it be? 应该是什么

Aside from that, I am very new to ASP.NET. 除此之外,我对ASP.NET还是很陌生。 I am trying to connect to this database in the same way I would in c# normally (using data adapters and SqlCommands). 我试图以与正常使用c#时相同的方式(使用数据适配器和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? 什么是连接到SQL数据库,并与ASP.NET登录和个人信息添加用户帐户的最佳方式?

EDIT: My Connection String is data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\\Users.mdf;User Instance=true 编辑:我的连接字符串是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| 将数据库添加到连接字符串-并在|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. Web.config文件中给出了正确的连接字符串。 The connection string I needed was Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Users.mdf;Integrated Security=True;User Instance=True 我需要的连接字符串是Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Users.mdf;Integrated Security=True;User Instance=True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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