ConnectionString - best practices ques

Discussion in 'ASP .Net' started by G. Dean Blake, Jun 3, 2004.

  1. We currently distribute a web application to serveral servers for a
    customer. We have been putting the connection string in our web.config file
    under <appSettings> <add key="dbconn.Connectionstring" value="workstation
    id=server025....... etc.

    When we deploy the application we have to change the workstation id and
    source parameters to name the server where the database will be.

    We tried to simply specify localhost however VS.NET could not deal with this
    because the data connection in server explorer has to have the read dev box
    server name.

    Questions are:
    Is this the best place to put the connection string?
    Is there any way to specify "localhost" and still get VS.NET to work with
    that?
    Is there any way I can tweak this connectionstring in the windows installer?
    Thanks,
    Dean
    G. Dean Blake, Jun 3, 2004
    #1
    1. Advertising

  2. One possible solution. Use Environment.MachineName to get the name of the
    local machine. Use that in your Connection String.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "G. Dean Blake" <> wrote in message
    news:...
    > We currently distribute a web application to serveral servers for a
    > customer. We have been putting the connection string in our web.config

    file
    > under <appSettings> <add key="dbconn.Connectionstring" value="workstation
    > id=server025....... etc.
    >
    > When we deploy the application we have to change the workstation id and
    > source parameters to name the server where the database will be.
    >
    > We tried to simply specify localhost however VS.NET could not deal with

    this
    > because the data connection in server explorer has to have the read dev

    box
    > server name.
    >
    > Questions are:
    > Is this the best place to put the connection string?
    > Is there any way to specify "localhost" and still get VS.NET to work with
    > that?
    > Is there any way I can tweak this connectionstring in the windows

    installer?
    > Thanks,
    > Dean
    >
    >
    Kevin Spencer, Jun 3, 2004
    #2
    1. Advertising

  3. Here are few options for storing connection string,

    you can store the connecting string information in your web.config file in
    two ways:
    - Using <appSettings> Section
    - Using <CustomSection> Section.

    - Using <appSettings> Section:
    The predefined <appSettings> section can be placed in any web.config file
    or the machine.config file. This section is useful for storing
    name-value pairs of data. An example of the use of the <appSettings>
    section is as follows:
    <appSettings>
    <add key="ConnectionString" Value="my connection string" />
    </appSettings>

    To access the appSettings Value, you need to use
    ConfiugrationSttings.AppSetting["ConnectionString"]. This uses the
    NameValueFileSectionHandler, which returns a
    System.Collection.Specialized.NameValueCollection object. The collection
    implements the IEnumerable interface so you can enumerate the collection,
    or read values directly for any valid key,

    Accessing the appSettings Section
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
    l/cpgrfaccessingappsettingssection.asp

    - Using <CustomSection> Section:
    You can create your own custom sections in a configuration file. The
    easiest way to do this is to configure one of the
    pre-existing configuration section handlers, assuming your section uses one
    of the generic structures such as name-value
    data, or single tag data.
    In this particular case you might use the Name-Value data. To do this, you
    need to use the pre-defined section handlers.

    For example, you can define a section called <myNameValueSection>
    <myNameValueSection>
    <add key="ConnectionString" Value="my connection string" />
    <myNameValueSection>

    to read the data, you will need the following code snippet
    NameValuecollection config =
    ConfigurationSettings.GetConfig("myNameValueSection");
    Foreach (string key in config.keys)
    {
    label1.Text = "Key:" + key.toString();
    label2.Text = "Value:" + config[key];
    }



    --
    Saravana
    Microsoft MVP - ASP.NET
    www.extremeexperts.com



    "G. Dean Blake" <> wrote in message
    news:...
    > We currently distribute a web application to serveral servers for a
    > customer. We have been putting the connection string in our web.config

    file
    > under <appSettings> <add key="dbconn.Connectionstring" value="workstation
    > id=server025....... etc.
    >
    > When we deploy the application we have to change the workstation id and
    > source parameters to name the server where the database will be.
    >
    > We tried to simply specify localhost however VS.NET could not deal with

    this
    > because the data connection in server explorer has to have the read dev

    box
    > server name.
    >
    > Questions are:
    > Is this the best place to put the connection string?
    > Is there any way to specify "localhost" and still get VS.NET to work with
    > that?
    > Is there any way I can tweak this connectionstring in the windows

    installer?
    > Thanks,
    > Dean
    >
    >
    Saravana [MVP], Jun 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ted
    Replies:
    6
    Views:
    754
  2. Ted
    Replies:
    1
    Views:
    614
    Philip Freidin
    Feb 3, 2004
  3. karim
    Replies:
    0
    Views:
    437
    karim
    Jul 13, 2003
  4. John Dalberg
    Replies:
    3
    Views:
    557
    samuelhon
    Nov 16, 2006
  5. Chicken McNuggets

    Best book on C gotchas and best practices?

    Chicken McNuggets, Jul 31, 2013, in forum: C Programming
    Replies:
    9
    Views:
    254
    Fred J. Tydeman
    Aug 5, 2013
Loading...

Share This Page