using connection stirng

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aradhanathawait
    New Member
    • Mar 2008
    • 36

    #1

    using connection stirng

    hi....

    How to use the connection string specified in app.config file in a windows form in c#. Please help.

    Regards,
    Aradhana
  • teddarr
    New Member
    • Oct 2006
    • 143

    #2
    include using System.Configur ation;

    Then put your connection string in the web.config file in the connectionStrin gs node.

    example:

    <add name="nameOfCon String" connectionStrin g="Server=serve rName;Database= DbName;" providerName="S ystem.Data.SqlC lient"/>

    After that you will need to add a reference to either Windows.Configu ration or System.Configur ation.

    After that add a line in your class to give a global variable, or declare it in each method, or the constuctor...wh ichever you prefer.

    The line is:

    Dim conn As New SqlConnection(C onfigurationMan ager.Connection Strings("nameOf ConString").Con nectionString)

    or

    SqlConnection conn = ConfigurationMa nager.Connectio nStrings("nameO fConString").Co nnectionString;

    It's a very handy technique that you will find yourself using from here on out.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      in C# that would be:
      Code:
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["name"].ConnectionString;

      Comment

      • teddarr
        New Member
        • Oct 2006
        • 143

        #4
        see what vb is doing to my brain.

        Thanks for the correction. :)

        Comment

        Working...