Write the Connection String in app.config and Read it in the Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yahya30
    New Member
    • May 2007
    • 10

    Write the Connection String in app.config and Read it in the Code

    Hi all,
    I want to know how to write the connection string in app.config and read it in the code, note that I'm using Vb.net desktop application.

    Kind regards,
    Yahya
  • yahya30
    New Member
    • May 2007
    • 10

    #2
    Originally posted by yahya30
    Hi all,
    I want to know how to write the connection string in app.config and read it in the code, note that I'm using Vb.net desktop application.

    Kind regards,
    Yahya

    I have found it, thanks...
    The following code must be written in app.config in order to save the connection string in a common place outside the code:-

    <add key="Conn" value="workstat ion id=YourServerNa me;packet size=4096;integ rated security=SSPI;d ata source=YourSqlS erverName;persi st security info=False;init ial catalog=YourDat aBaseName" />

    Note that istead of YourServerName written above you must write down your server name (The machine that is holding the Sql Server being used for your application), this server might be your machine.

    istead of YourSqlServerNa me you must write down your Sql Server name as defined on the server/your machine.

    istead of YourDataBaseNam e you must write down the name of your database as defined in the Sql Server.

    Then to read the connection string in the vb.net code I wrote:-

    Private sConn As String
    Private Conn As System.Data.Sql Client.SqlConne ction

    sConn = System.Configur ation.Configura tionSettings.Ap pSettings("Conn ")
    Conn = New System.Data.Sql Client.SqlConne ction(sConn)

    Note that I got the value available in app.config by using System.Configur ation.Configura tionSettings.Ap pSettings("Conn "), note that I gave a key called "Conn" for this value in app.config, and put that key between brackets in System.Configur ation.Configura tionSettings.Ap pSettings("Conn ").

    You can name this key in app.config with any name you want but you have to call it with the same name in the code.
    I mean that if you name it as "MyConn" then you have to call it like this in the code:-
    sConn = System.Configur ation.Configura tionSettings.Ap pSettings("MyCo nn")
    Conn = New System.Data.Sql Client.SqlConne ction(sConn)

    So you will not be able to call it as:-
    sConn = System.Configur ation.Configura tionSettings.Ap pSettings("Conn ")
    Conn = New System.Data.Sql Client.SqlConne ction(sConn)


    If you have any other suggestions please let me know.

    Kind regards,
    Yahya

    Comment

    • yahya30
      New Member
      • May 2007
      • 10

      #3
      I should have said that this connection string is not a standard one because it might differ if the Sql Server is set to be started and run on a specific account rather than system account. But if you set the Sql Server to start and run on System Account then I think the connection string will be the same as the one written herebefore.
      To check this setting, just open the Sql Server Enterprise Manager-> right click on your Sql Server in the tree view, select Properties -> Select Security Tab -> and check for the Startup service account.

      Kind regards,
      Yahya

      Comment

      • danielgithahu
        New Member
        • Mar 2013
        • 1

        #4
        Awesome, i spent three hours looking for something that actually works. Thanks

        Comment

        Working...