using stream reader to read and store it in a variable in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andyehi
    New Member
    • Nov 2008
    • 20

    using stream reader to read and store it in a variable in C#

    Hi Everyone,
    I am having a txt file config.txt on application start up path.I need to read that file line by line using a delimiter ; or , and store the values in a variable to connect to the database. Currently I am hardcoding the initial database connection.

    Code:
    //code
    // Mysql database initialize and connection to Profile    
                string strprofile = "DRIVER={MySQL ODBC 5.1 Driver};" + "SERVER=192.168.1.9;" + "DATABASE=profile;" + "UID=root;" + "PASSWORD=selva123;" + "OPTION=3";
    //code
    I have to read the following from txt file and store in varable so that I can pass this to the connection string.If anyone have any other way of doing this will be great.Thanks in advance.

    Server:
    Database:
    UID:
    Password:
    Last edited by RedSon; Dec 29 '08, 05:22 PM. Reason: Adding code tags.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Use a StreamReader, then ReadLine() then split on ":" ?

    Comment

    • andyehi
      New Member
      • Nov 2008
      • 20

      #3
      HI A simple example will help me more.I am really new to this stuff.

      Comment

      • mldisibio
        Recognized Expert New Member
        • Sep 2008
        • 191

        #4
        If you are writing the application in .NET, then you can add a config file to your project (Project -> Add New Item -> Config file).

        Within this file you can add a connectionStrin gs element with all this information in text/xml format. It will be copied to your output directory and you can change it as needed without recompiling your application. .Net knows how to read this file and create your database connection from it.

        See: ConnectionStrin gSettings Class for examples and explanation.

        Comment

        • andyehi
          New Member
          • Nov 2008
          • 20

          #5
          I am developing a windows application and I dont have that option.Please give me an example to read and store in variable with the delimiter ; .Thanks

          Comment

          • mldisibio
            Recognized Expert New Member
            • Sep 2008
            • 191

            #6
            Yes you do. The new item is called "Applicatio n Configuration File." This is the preferred way to store connection string data and passwords. Sometimes the terminology is different for web application or windows applications, but the concept and practice is exactly the same for both.

            Please slow down and read the above MSDN section and some of the links it contains. If you are "really new to this stuff" as you say, then now is a good time to learn the correct way to store and retrieve connection string items.

            Comment

            • mldisibio
              Recognized Expert New Member
              • Sep 2008
              • 191

              #7
              Here is a better link for starting with connection strings:

              Working with Connection Strings.

              Comment

              • andyehi
                New Member
                • Nov 2008
                • 20

                #8
                Thanks everyone will do what you suggested

                Comment

                Working...