Link to home
Start Free TrialLog in
Avatar of bradbritton
bradbritton

asked on

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.

Thanks,

Brad
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

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?
ASKER CERTIFIED SOLUTION
Avatar of AUmidh
AUmidh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bradbritton
bradbritton

ASKER

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?

Thanks,

Brad
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")
WriteToFile(strTime,eNo,eCode)
Next


Private Sub WriteToFile(ByVal strTime As String,Byval empNo as String,byval earnCode as string)
      Dim strW As String = empNo + "-----" + strTime + "-------" + earnCode
        sw.WriteLine(strW)
        sw.Flush()
End Sub
private sub CloseWriter()
  sw.Close()
End Sub