Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Connecting Strings on C# (using visual studio) - making it general/generic

Connecting Strings on C# (using visual studio) - making it general/generic

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelpquestiondatabase
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Desmond Lim
    wrote on last edited by
    #1

    Hi, I have a problem that connects to my database (on the same machine) using the usual c#/vs connectionString. When I publish it, that string is still the same (i.e. the name of the machine doesn't change). Yea, I think I've hardcoded the connectionString. But how can I softcode it using VS? How can I make it so that the connectionString takes the name of the current machine it is residing on or better, the application would prompt for where the database is. Any help is appreciated? Or even just point me to articles. Thanks.

    C M P 3 Replies Last reply
    0
    • D Desmond Lim

      Hi, I have a problem that connects to my database (on the same machine) using the usual c#/vs connectionString. When I publish it, that string is still the same (i.e. the name of the machine doesn't change). Yea, I think I've hardcoded the connectionString. But how can I softcode it using VS? How can I make it so that the connectionString takes the name of the current machine it is residing on or better, the application would prompt for where the database is. Any help is appreciated? Or even just point me to articles. Thanks.

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      Just use the app.config - file to set up your connection-strings. It's easy to administrate and flexible (all you will need is a text-editor). If this is not what you want, use a static connection string and use string.replace to replace your computer name with the one you get from System.Net.Dns.GetHostName() (I don't recommend this - normaly another DB will require another login etc.)

      1 Reply Last reply
      0
      • D Desmond Lim

        Hi, I have a problem that connects to my database (on the same machine) using the usual c#/vs connectionString. When I publish it, that string is still the same (i.e. the name of the machine doesn't change). Yea, I think I've hardcoded the connectionString. But how can I softcode it using VS? How can I make it so that the connectionString takes the name of the current machine it is residing on or better, the application would prompt for where the database is. Any help is appreciated? Or even just point me to articles. Thanks.

        M Offline
        M Offline
        Mark Churchill
        wrote on last edited by
        #3

        Theres a ConnectionStringBuilder class in the framework somewhere that acts like a dictionary. Then you can add the machine name on at runtime.

        Mark Churchill Director Dunn & Churchill Diamond Binding: Zero to Data Layer in 3 mins

        1 Reply Last reply
        0
        • D Desmond Lim

          Hi, I have a problem that connects to my database (on the same machine) using the usual c#/vs connectionString. When I publish it, that string is still the same (i.e. the name of the machine doesn't change). Yea, I think I've hardcoded the connectionString. But how can I softcode it using VS? How can I make it so that the connectionString takes the name of the current machine it is residing on or better, the application would prompt for where the database is. Any help is appreciated? Or even just point me to articles. Thanks.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Specify localhost as the server? I use custom configuration files; if the program can't find the file it pops up a dialog to get the names of the server and database from the user then stores that information in the configuration file.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups