Improving C# code in Data reader.

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

    Improving C# code in Data reader.

    Dear all,
    I have the application which works fine according to the specs but I would like to structure my code efficiently .I am actually reading the data and will update or insert into mdb file according to the output.Is there any other way to simplify the below code?.Thanks in advance



    Code:
    while (proreader.Read())
     {
       string profid = proreader["EMRID"].ToString();
       string accid = proreader["accountid"].ToString();
       string server = proreader["server"].ToString();
       string userid = proreader["userid"].ToString();
       string password = proreader["password"].ToString();
       string mdbpath = proreader["mdbpath"].ToString();
       string mdbuserid = proreader["mdbuserid"].ToString();
       string mdbpassword = proreader["mdbpassword"].ToString();
    }
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question). Code tags preserve indention and uses a monospaced font.

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [CODE] [/CODE] tags in future.

    MODERATOR

    Comment

    • nukefusion
      Recognized Expert New Member
      • Mar 2008
      • 221

      #3
      I can't personally think of any ways to really condense the code sample you've posted. It's pretty much how I would do it.
      I guess depending on what you're doing there may be the opportunity to just call the SqlDataReader.G etValues() method, which could save you assigning each column value to a seperate variable....

      Comment

      • andyehi
        New Member
        • Nov 2008
        • 20

        #4
        Hi
        Thanks for the suggestion.Belo w is my progrm flow.
        1.connect to mysql db and select fields
        2.Store these fields using variables.(from data reader)
        3.Again ill connect to mdb db and check with 'MRN' field of mdb db.
        4.If exist I will update the value stored in variable to mdb else insert in mdb
        Basically I will insert or update the fields captured in variable to mdb.So Can you exactly tell me how I would approach this so that I dont need to use more variables.Thank you.

        Comment

        • nukefusion
          Recognized Expert New Member
          • Mar 2008
          • 221

          #5
          Well, you'd just carry on doing exactly what you are but replace the separate variables with an object array:

          Code:
          object[] values;
          while (proreader.Read()) 
           { 
             values = new object[proreader.FieldCount];
             proreader.GetValues(values);
          }
          When adding or updating the data to the other database you would just reference the items in the array instead of the separate variables you had before.

          It's only going to save you 5 or 6 lines of code but it's the only way I can think of to condense it, if you really wanted to. In terms of efficiency I wouldn't have thought there was an awful lot of difference either way.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Or if they're all strings, you can use a List<string> from the System.Collecti ons.Generic namespace.

            Comment

            • andyehi
              New Member
              • Nov 2008
              • 20

              #7
              Thank you

              Hi thank you so much will try it today.

              Comment

              Working...