Datarow to string

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

How can I save all values in a system.data.datarow to a string? I need this
for error logging purposes.

Thanks

Regards
 
Hi

How can I save all values in a system.data.datarow to a string? I need this
for error logging purposes.

Thanks

Regards

I'm assuming you have access to the datacolumns that are part of that
datarow correct?

Private Function ConvertDataRowToString(ByVal dr As DataRow, ByVal
columns As System.Data.DataColumnCollection) As String
Dim dataRowBuilder As New System.Text.StringBuilder(100)

For Each dc As DataColumn In columns
dataRowBuilder.AppendFormat("{0} = {1}", dc.ColumnName,
dr(dc.Ordinal))
dataRowBuilder.AppendLine()
Next

Return dataRowBuilder.ToString
End Function

Dim rowData As String = ConvertDataRowToString(yourDataRowGoesHere,
yourDataTable.Columns)



Let me know if you don't understand...
 
chris,

I would not use the name dataRowBuilder in this case, I had to look twice
before I saw what you was doing. That is not only for me, but probably for
everybody even you who has to maintain it after some months.




Cor

<[email protected]> schreef in bericht
Hi

How can I save all values in a system.data.datarow to a string? I need
this
for error logging purposes.

Thanks

Regards

I'm assuming you have access to the datacolumns that are part of that
datarow correct?

Private Function ConvertDataRowToString(ByVal dr As DataRow, ByVal
columns As System.Data.DataColumnCollection) As String
Dim dataRowBuilder As New System.Text.StringBuilder(100)

For Each dc As DataColumn In columns
dataRowBuilder.AppendFormat("{0} = {1}", dc.ColumnName,
dr(dc.Ordinal))
dataRowBuilder.AppendLine()
Next

Return dataRowBuilder.ToString
End Function

Dim rowData As String = ConvertDataRowToString(yourDataRowGoesHere,
yourDataTable.Columns)



Let me know if you don't understand...
 
Hi all,

I was currious what would be quicker, the stringbuilder or the stringwriter.
The results were almost the same, where the stringbuilder was slightly
faster.

The results of the actions are of course exactly the same

For those who are interested bellow the code

\\\
''To Build a example row
Dim dt As New DataTable
For i = 0 To 100000
Dim dc As New DataColumn
dt.Columns.Add(dc)
Next
Dim dr = dt.NewRow
For i = 0 To 100000
dr(i) = i.ToString()
Next
dt.Rows.Add(dr)

Dim stw As Stopwatch
Dim stw2 As Stopwatch
Dim Cycles = 100


For i = 1 To 10
stw = New Stopwatch
stw.Start()
For y = 1 To Cycles
'To Serialize it using StringBuilder
Dim sb As New System.Text.StringBuilder()

For Each dc As DataColumn In dt.Columns
sb.Append(dr(dc))
sb.AppendLine()
Next

Dim sbString = sb.ToString
Next
stw.Stop()




stw2 = New Stopwatch
stw2.Start()
For y = 1 To Cycles
'To Serialize it using StringWriter
Dim sw As New System.IO.StringWriter
For Each dc As DataColumn In dt.Columns
sw.WriteLine(dr(dc))
Next

Dim swString = sw.ToString
Next
stw2.Stop()

Debug.WriteLine("SB: " & stw.Elapsed.ToString & " SW: " &
stw2.Elapsed.ToString)
Next
///

Cor
 
Back
Top