我的连接字符串有问题,我在网上找不到任何可以帮助我的东西。所以我有一个测试网站,我在该网站上连接到不在同一服务器上的数据库。一切正常,数据被正确读取,连接成功。
现在,当我在与数据库位于同一服务器上的网站上尝试相同操作时,它不起作用。所以我想如果我已经在该服务器上,我不能像从另一台服务器那样连接到数据库服务器:
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=serverName;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
有人可以帮我吗?
如果您需要更多信息,请询问。
提前致谢!
[编辑]
哦,我忘了...这是我收到的错误消息:
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)
最佳答案
尝试使用 localhost
而不是 server name
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=localhost;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
关于c# - 如果网站与数据库位于同一台服务器上,如何构建数据库连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933731/