c# - 如果网站与数据库位于同一台服务器上,如何构建数据库连接字符串?

标签 c# asp.net database database-connection connection-string

我的连接字符串有问题,我在网上找不到任何可以帮助我的东西。所以我有一个测试网站,我在该网站上连接到不在同一服务器上的数据库。一切正常,数据被正确读取,连接成功。

现在,当我在与数据库位于同一服务器上的网站上尝试相同操作时,它不起作用。所以我想如果我已经在该服务器上,我不能像从另一台服务器那样连接到数据库服务器:

<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/

相关文章:

c# - Rails ActionController 类中的 around_filter 等价于 C# 的东西是什么?

c# - NLog 多文件目标问题

c# - 解析 Word (.doc/.docx) 文档

C# 图像生成器 - 性能缓慢

database - Actionscript-3 类 Hibernate 框架

c# - 从结果中过滤掉 id mongodb c#

c# - 在 C# 中检测 XML 片段

asp.net - 您将如何将 Entity Framework (1.0) 与 ASP.Net Membership 一起使用?

mysql - 错误 #1248 - 每个派生表必须有自己的别名

ruby-on-rails - 在 rails 中建模嵌套模型