By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Improving C# code in Data reader.

P: 21
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

Expand|Select|Wrap|Line Numbers
  1. while (proreader.Read())
  2.  {
  3.    string profid = proreader["EMRID"].ToString();
  4.    string accid = proreader["accountid"].ToString();
  5.    string server = proreader["server"].ToString();
  6.    string userid = proreader["userid"].ToString();
  7.    string password = proreader["password"].ToString();
  8.    string mdbpath = proreader["mdbpath"].ToString();
  9.    string mdbuserid = proreader["mdbuserid"].ToString();
  10.    string mdbpassword = proreader["mdbpassword"].ToString();
  11. }
Dec 11 '08 #1
Share this Question
Share on Google+
6 Replies

Curtis Rutland
Expert 2.5K+
P: 3,256
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.

Dec 11 '08 #2

Expert 100+
P: 221
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.GetValues() method, which could save you assigning each column value to a seperate variable....
Dec 11 '08 #3

P: 21
Thanks for the suggestion.Below 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.
Dec 11 '08 #4

Expert 100+
P: 221
Well, you'd just carry on doing exactly what you are but replace the separate variables with an object array:

Expand|Select|Wrap|Line Numbers
  1. object[] values;
  2. while (proreader.Read()) 
  3.  { 
  4.    values = new object[proreader.FieldCount];
  5.    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.
Dec 11 '08 #5

Curtis Rutland
Expert 2.5K+
P: 3,256
Or if they're all strings, you can use a List<string> from the System.Collections.Generic namespace.
Dec 11 '08 #6

P: 21
Hi thank you so much will try it today.
Dec 12 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.