How do I read database values into a file?

I need to read the values from a dataset into a group of variables. I am assuming that I would use some type of logic to read each row in the dataset, but I am unsure about how code this. I am using VS 2008 as well for this.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Can you explain a bit more fully what you're trying to do? Your title talks about a file, but you don't mention that in your text. What is the purpose of the "group of variables"? Do you realize that a dataset can be loaded directly into a database using a DataAdapter?
Your Question need more explanation as mention by ElrondCT.

Each Row of a dataset is ...

For Each dt as DataTable in dataset.Tables
   For Each dr as DataRow in dt.rows

private Sub WriteToFile(DataRow dr)
' Write to file Logic.
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bradbrittonAuthor Commented:
Sorry about not being clear enough. What have in my dataset is records pretianing to start and end times for employees. Each record will have the following information:

Employee Number
Start Time
End Time
Earning Code

What I would like to do is extract these values, calculate the total time for that employee record then write this to a file. I am needing to pull out the time values to calculate the time correct? Or is there an easier way that I am not seeing?


What kind of an output file do you want to have? Text file, Access database, SQL table?

Yes, you'll need to reference the time values to get the time for the employee. But it's generally easiest just to process a single row at a time, as AUmidh's code suggests. So you could calculate

   dim TimeTotal as TimeSpan
   TimeTotal = dr.EndTime.Subtract(dr.StartTime)

and then output TimeTotal to your output file, along with whatever other data you want (employee number, certainly, and maybe all the other fields). How you output it will depend on what type of output file you want.
if you want to write to a text file then use the following code.
Dim sw As StreamWriter = New StreamWriter(yourFilePath)

   For Each dr as DataRow in ds.Tables("tableName").rows
Dim strTime as String= dr("EndTime").Subtract(dr("StartTime") ' as ElrondCT suggested.
Dim eNo as String=dr("Employee Number")
Dim eCode as String=dr("Earning Code")

Private Sub WriteToFile(ByVal strTime As String,Byval empNo as String,byval earnCode as string)
      Dim strW As String = empNo + "-----" + strTime + "-------" + earnCode
End Sub
private sub CloseWriter()
End Sub
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.