How to obtain a connection string without hard coding?

    General discussion

  • Is there any way to obtain connection string directly through coding without obtaining it through properties window.

    This can be helpful while deploying the project so that I don't need to change it at client side....

    Any idea?

    Wednesday, January 30, 2013 5:37 PM

All replies

  • Hi,

    Thank you for posting in the MSDN forum.

    Based on your description, I’m afraid that it is not the correct forum for this issue, since this forum is to discuss:

    Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, Visual Studio Report Controls, and Visual Studio Editor.

    To help you find the correct forum for this issue, would you mind letting us know more information about this issue? Which language are you using? Which kind of app are you developing, the app or others? If it is related to the, you could post this issue to where experts live in.

    Best Regards,

    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 31, 2013 9:46 AM
  • A connection string is a STRING and you can set the connection string like any other string variable.  There are many different methods that use connection strings so it is hard to give any sample code without knowing exactly what method you are using.


    Thursday, January 31, 2013 10:10 AM
  • Hello,

    Thanks for reply,

    Actually I am developing a website using ASP.NET with C# under VS 2010.

    I have developed a database using SQL Server 2008 R2.

    I wanted a way to determine connection string through coding without getting it through property window, so that when I deploy my website on client side, I don't have a again modify it using VS.

    Is anything like that possible?


    Thursday, January 31, 2013 2:03 PM
  • Below is my connection string for a SQL Server 2008 R2 database.  We have the same database on multiple PCs.  The comboxbox will fill in the Server name.  Now if it is always on the PC where the application is running you could use LocalHost, or get the PC name using Dns.GetHostName();

    strCon = "Data Source=" + comboBox1.Text + "\\SQLEXPRESS;Initial Catalog=ABC;Integrated Security=SSPI";


    Thursday, January 31, 2013 3:00 PM
  • THNX .... joel

    Would try this....

    but if I set up client -server infrastructure, where my DB is located on server, then I have to manually put the server name, I guess.


    Friday, February 01, 2013 3:34 PM
  • You could have a menu option to add the server name.  Sometimes I like to have an invisible text box and store config info in the invisible box.  Another possiblilty is to load thje connection string into Control Panel - Admin tools - Data Sources (ODBC).


    Friday, February 01, 2013 4:54 PM