检索链接到Windows Azure的Web站点与C#.NET的SQL Azure数据库的连接字符串 [英] Retrieve the connection string of an SQL Azure database that is linked to a Windows Azure Web Site with C#.NET
问题描述
一个Windows Azure的Web站点的配置页面有一个连接字符串一节。本节列出了链接资源的连接字符串。我们如何编程检索链接的SQL Azure数据库连接字符串?
The configuration page of a Windows Azure Web Site has a "connection strings" section. The section lists connection strings for linked resources. How do we programmatically retrieve the connection string for a linked SQL Azure Database?
推荐答案
解决方案
编程方式检索连接字符串如下:
Programmatically retrieve the connection string as follows:
connString =
Environment.GetEnvironmentVariable("PREFIX_myConnStringName");
释解
Azure的连接字符串成为环境变量。 <一href=\"http://www.windowsazure.com/en-us/manage/services/web-sites/how-to-configure-websites/#howtoconfigSQL\"相对=nofollow>文档解释说,天青用的 prefixes 如下:
The Azure connection strings become environmental variables. Documentation explains that Azure creates the variables with the prefixes as follows:
SQL服务器:SQLCONNSTR_myConnStringName
SQL Server: SQLCONNSTR_myConnStringName
MySQL的:MYSQLCONNSTR_myConnStringName
MySQL: MYSQLCONNSTR_myConnStringName
SQL数据库:SQLAZURECONNSTR_myConnStringName
SQL Database: SQLAZURECONNSTR_myConnStringName
自定义:CUSTOMCONNSTR_myConnStringName
Custom: CUSTOMCONNSTR_myConnStringName
SQL Azure的:SQLAZURECONNSTR_myConnStringName
SQL Azure: SQLAZURECONNSTR_myConnStringName
知道了,我们可以检索以下code所需的连接字符串:
Knowing that, we can retrieve the desired connection string with the following code:
connString =
Environment.GetEnvironmentVariable("SQLAZURECONNSTR_myConnStringName");
其他选项
作为另一种选择,此<一个href=\"http://stackoverflow.com/questions/13782337/retrieve-and-use-windows-azures-connection-strings\">related帖子有关如何通过web.config中访问连接字符串如下:
As another option, this related post about how to access the connection string through web.config as follows:
<add name="myConnStringName"
connectionString="you can leave this blank"
providerName="System.Data.SqlClient" />
请注意:我们可能没有包括的providerName属性
Note: we might not have to include the providerName attribute.
进一步研究
我们可以通过把这个code成的Razor视图查看所有可用的环境变量和连接字符串。警告:这会透露您的密码
We can view all the available environmental variables and connection strings by putting this code into a Razor view. Warning: this will reveal your password!
<ul>
@foreach (System.Collections.DictionaryEntry ev in Environment.GetEnvironmentVariables())
{
if (ev.Value.ToString().ToLower().Contains("data source"))
{
<li><strong>@ev.Key.ToString()</strong> @ev.Value.ToString()</li>
}
}
</ul>
<ul>
@foreach (System.Configuration.ConnectionStringSettings cs in System.Configuration.ConfigurationManager.ConnectionStrings)
{
<li><strong>@cs.Name</strong> @cs.ConnectionString</li>
}
</ul>
这就是现在。
这篇关于检索链接到Windows Azure的Web站点与C#.NET的SQL Azure数据库的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!