簡體   English   中英

如果網站與數據庫位於同一服務器上,如何構建數據庫連接字符串?

[英]How do I build the database connection string if the website is on the same server as the db?

我的連接字符串有問題,我在網上找不到任何可以幫助我的東西。 因此,我有一個測試網站,可以在該網站上連接到不在同一服務器上的數據庫。 在那里一切正常,正在正確讀取數據並且連接成功。

現在,當我在與數據庫位於同一服務器上的網站上嘗試相同操作時,它將無法正常工作。 因此,我想我不能像從另一台服務器那樣連接到數據庫服務器,如果我已經在該服務器上,就像這樣:

<connectionStrings>
    <add name="nameOfConnString" connectionString="Data Source=serverName;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>

有人可以幫我嗎?

如果您需要更多信息,請詢問。

提前致謝!

[編輯]

哦,我忘了...這是我收到的錯誤消息:

建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:命名管道提供程序,錯誤:40-無法打開與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>
<connectionStrings>
    <add name="nameOfConnString" connectionString="Data Source=.;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>

上面的兩個答案都是正確的,但是,如果您安裝了SQL Express(我假設我們在談論MS SQL),那么您的連接字符串必須是這樣的:

<connectionStrings>
    <add name="nameOfConnString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM