C# 如何将带有 IP 地址的连接字符串放入 web.config 文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18639760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I put connection string with IP Address inside web.config file?
提问by Kenny
We are currently using the following connection string in our web.config file:
我们目前在 web.config 文件中使用以下连接字符串:
add name="DBConnectionString" connectionString="Data Source=ourServer;Initial Catalog=ourDB;Persist Security Info=True;User ID=theUserName;Password=thePassword" providerName="System.Data.SqlClient" />
This works fine. However, this app needs to be pushed to our external server. This means, according to our infrastructure team that we will need a connection string with ip address and port 1433 to be able to connect the app to our db which is not on the same external server.
这工作正常。但是,此应用程序需要推送到我们的外部服务器。这意味着,根据我们的基础设施团队的说法,我们需要一个带有 IP 地址和端口 1433 的连接字符串,以便能够将应用程序连接到我们不在同一外部服务器上的数据库。
I have tried any of the two connection strings below but they don't work.
我尝试了以下两个连接字符串中的任何一个,但它们不起作用。
The first one doesn't error out but just doesn't recognize the db. I suspect it is because of this bit
第一个不会出错,但只是无法识别数据库。我怀疑是因为这一点
tcp:10.database.windows.net
Do I need to replace it with IP Address? I am told I need the tcp in there.
我需要用 IP 地址替换它吗?我被告知我需要那里的 tcp。
<add name="DBConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=tcp:10.database.windows.net,1433;Initial Catalog=POLLWORKER_TRAINING;Integrated Security=False;User Id=pworker;Password=pworker;Encrypt=True;MultipleActiveResultSets=True" />
This last one is causing an error which says,
最后一个导致了一个错误,它说,
the element connectionStrings cannot contain text. List of possible elements expected, "add", "remove", "clear"
元素 connectionStrings 不能包含文本。预期的可能元素列表,“添加”、“删除”、“清除”
<add name="DBConnectionString" connectionString="Provider=sqloledb;Data=10.22.5.20,1433;Network Library=DBMSSOCN;Initial Catalog=ourDB;User ID=theUserName;Password=thePassword" />
采纳答案by Habib
This Data=10.22.5.20,1433
in your connection string should be:
这Data=10.22.5.20,1433
在您的连接字符串中应该是:
Data Source=10.22.5.20,1433
Data Source=10.22.5.20,1433
For more details see: SQL Connection Strings
有关更多详细信息,请参阅:SQL 连接字符串