我正在尝试使用环境变量从 Azure应用程序设置获取连接字符串,但似乎我在Azure App中放置的连接字符串的格式设置不正确。这是我的原始连接字符串在localhost中正常工作。
<add connectionString="Server=tcp:****.database.windows.net,1433;Initial Catalog=***;
Persist Security Info=False;User ID=******;Password=*********;MultipleActiveResultSets=False;
Encrypt=True;TrustServerCertificate=False;Connection Timeout=1200000;
Max Pool Size=500;Pooling=true;" name="Con_String"></add>
中将"Server=tcp:****.database.windows.net,1433;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=1200000;Max Pool Size=500;Pooling=true;"
作为 con_string 值
现在我在运行时获取Connection字符串,我将环境变量用作字符串ConnectionString = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_Con_String");
但在运行网络应用程序时,我收到异常消息不支持关键字:&#39;&#34;服务器&#39;
我尝试了这些方法 Retrieve and use Windows Azure's connection strings?和其他类似的帖子,但没有奏效。我在这里错过了一些愚蠢的东西吗?
答案 0 :(得分:1)
我在这里错过了一些愚蠢的东西吗?
如果您仍然获得Keyword not supported: '"Server'
或The ConnectionString property has not been initialized
。
在您添加WebApp appseting的连接字符串后,我假设您不保存appsetting 。
以下代码都在我身边。
ConnectionString = ConfigurationManager.ConnectionStrings["Con_String"].ConnectionString.
ConnectionString = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_Con_String")
测试结果:
注意:由于GauravMantri提到Azure WebApp appsetting中的连接字符串中不需要引号(&#34;)。
答案 1 :(得分:0)
在我的案例中,这个问题与一个愚蠢的错误有关:当将 copy / pasted 连接字符串复制到Notepad++
时,由于Notepad++
被包装,我以某种方式错过了换行符的出现文字或其他内容。
无论如何,我已经复制/粘贴到Notepad
,然后分别替换了User ID
和Password
,然后再次复制/粘贴到服务器,保存了设置,然后重新启动了服务器。
最后,解决问题。