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
bradbrittonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AUmidhConnect With a Mentor Commented:
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
               WriteToFile(dr)
   Next
Next

private Sub WriteToFile(DataRow dr)
' Write to file Logic.
End Sub
0
 
ElrondCTCommented:
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?
0
 
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?

Thanks,

Brad
0
 
ElrondCTCommented:
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.
0
 
AUmidhCommented:
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
0
All Courses

From novice to tech pro — start learning today.