locked
How to access a connection string in IIS 7 from Visual Studio 2012 on local machine RRS feed

  • Question

  • User-1959374973 posted

    I have noticed that there are sections within IIS for Connection Strings.  I've created a few of these connection strings within my local machine's IIS.  How can I access those connection strings so I can use them in my project that I'm running locally with Visual Studio 2012?

    I tried using the following code but it seems to only be looking in my web.config:

    string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
    

    Ideally, I'd like to keep all of these connection strings in IIS and not have to worry about them in my web.config files.

    Anyone doing anything similar?


    Tuesday, September 24, 2013 4:05 PM

Answers

  • User-166373564 posted

    Hi

    Genaral Web.Config section with a ConnectionString:

    <connectionStrings>
        <remove name="LocalSqlServer" />
        <add name="LocalSqlServer"
            connectionString="Data Source=myDBServer;database=myDB;Integrated Security=True;"
            providerName="System.Data.SqlClient"/>
    </connectionStrings>
    

    If we want to set Connection string as a global then we can write connection string in web.config file using connectionStrings,

    After defining connection string here, we can easily get this string in page. For getting this string we have to use System.Configuration.ConfigurationManager.

    more information: http://www.nullskull.com/q/10296839/connection-string.aspx 

    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 26, 2013 10:47 PM