locked
Connection strings RRS feed

  • Question

  • User1754860325 posted

    Hi Is there a way to fetch a connection string strait from web config instead of doing the following:

     

    using(SqlConnection conn3 = new SqlConnection(@"Data Source=GMBOAT-PC\EMS_AFRICA;Integrated Security=True; User ID=****; password=****"))

    Tuesday, September 3, 2013 4:53 PM

Answers

  • User2103319870 posted

    Try like this

    Add the connection string in your webconfig

    <connectionStrings>
    
        <add name="dbconnectionstring" connectionString="Data Source=GMBOAT-PC\EMS_AFRICA;Integrated Security=True; User ID=****; password=****"
          providerName="System.Data.SqlClient" />
          </connectionStrings>

    Add a reference to this namespace in your page

    using System.Configuration;

    Read you connection string using the below code

    string MysqlConnectionString = ConfigurationManager.ConnectionStrings["dbconnectionstring"].ConnectionString;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 3, 2013 4:57 PM

All replies

  • User2103319870 posted

    Try like this

    Add the connection string in your webconfig

    <connectionStrings>
    
        <add name="dbconnectionstring" connectionString="Data Source=GMBOAT-PC\EMS_AFRICA;Integrated Security=True; User ID=****; password=****"
          providerName="System.Data.SqlClient" />
          </connectionStrings>

    Add a reference to this namespace in your page

    using System.Configuration;

    Read you connection string using the below code

    string MysqlConnectionString = ConfigurationManager.ConnectionStrings["dbconnectionstring"].ConnectionString;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 3, 2013 4:57 PM
  • User-1360095595 posted

    Yes indeed. You use the <connectionstrings> section for that. 

    Refer: 

    http://www.connectionstrings.com/store-connection-string-in-webconfig/

    Tuesday, September 3, 2013 5:00 PM
  • User244637016 posted

    Hi 

    Replace below with your db details

    <add name="ConnStringName" connectionString="Server=InstanceName;User ID=DBInstanceUserName;Password=DBIsntancePassword;Database=DBName;Trusted_Connection=False;" providerName="System.Data.SqlClient" />

    Wednesday, September 4, 2013 1:00 AM