Retrieve the connection string of an SQL Azure database that is linked to a Windows Azure Web Site with C#.NET

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Solution

Programmatically retrieve the connection string as follows:

connString = 
    Environment.GetEnvironmentVariable("PREFIX_myConnStringName");

Explaination

The Azure connection strings become environmental variables. Documentation explains that Azure creates the variables with the prefixes as follows:

SQL Server: SQLCONNSTR_myConnStringName

MySQL: MYSQLCONNSTR_myConnStringName

SQL Database: SQLAZURECONNSTR_myConnStringName

Custom: CUSTOMCONNSTR_myConnStringName

SQL Azure: SQLAZURECONNSTR_myConnStringName

Knowing that, we can retrieve the desired connection string with the following code:

connString = 
    Environment.GetEnvironmentVariable("SQLAZURECONNSTR_myConnStringName");

Other Option

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

Note: we might not have to include the providerName attribute.

Further Research

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>

That’s all for now.

Method 2

Getting the connection string as an environmental variable this way would seem to be the wrong way. You would define it under the connection string and then just call GetConnectionString from the IConfiguration interface, without making it more complex and prefixing with database strings.

Try the below this makes it much simpler

IConfiguration config = new ConfigurationBuilder()
    .AddEnvironmentVariables()
    .Build();

var connection = _config.GetConnectionString("DatabaseConnection");


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x