473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Improving C# code in Data reader.

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. }
  12.  
Dec 11 '08 #1
6 1843
Curtis Rutland
3,256 Expert 2GB
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
Dec 11 '08 #2
nukefusion
221 Expert 100+
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
andyehi
21
Hi
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
nukefusion
221 Expert 100+
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);
  6.  
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
3,256 Expert 2GB
Or if they're all strings, you can use a List<string> from the System.Collections.Generic namespace.
Dec 11 '08 #6
andyehi
21
Hi thank you so much will try it today.
Dec 12 '08 #7

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

Similar topics

9
by: Mike L | last post by:
I tried a sample of code in MSDN magazine, but now I'm stuck. What code has the best performance to populate a Data Grid with a SP? Below is the code I have, which might be completing the wrong...
5
by: Haydnw | last post by:
Hi, I have the code below as code-behind for a page which displays two images. My problem is with the second bit of code, commented as " 'Portfolio image section". Basically, the SQL query gets...
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
3
by: LS | last post by:
Hello, I'm building an rss reader, works fine for all rss feeds, except for this one (you can try the sample code) The program stops on myxml.LoadXml(content) Any ideas why this happens, I'm...
2
by: JR | last post by:
hi, The next code gives me no records back. if I do the same selection with acces I have many records, depending on the param LiedNaam Thanks Jan sConnectionString ==> standaard connection...
7
by: lmnorms1 | last post by:
Hello, I am trying to update an access database record date field that matches a specific date. The code is not working. Anyone have any advice? Here is the code: Dim gConnString As String =...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.