Hi

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

Thanks

Regards

Re: Datarow to string by chrisrock2

chrisrock2
Fri Mar 14 23:01:49 CDT 2008

On Mar 14, 10:07=A0pm, "John" <J...@nospam.infovis.co.uk> wrote:
> Hi
>
> How can I save all values in a system.data.datarow to a string? I need thi=
s
> 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} =3D {1}", dc.ColumnName,
dr(dc.Ordinal))
dataRowBuilder.AppendLine()
Next

Return dataRowBuilder.ToString
End Function

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



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

Re: Datarow to string by Cor

Cor
Sat Mar 15 01:53:12 CDT 2008

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

<chrisrock2@gmail.com> schreef in bericht
news:6422721d-b9dc-4c59-9f68-a862364aabbf@t54g2000hsg.googlegroups.com...
On Mar 14, 10:07 pm, "John" <J...@nospam.infovis.co.uk> wrote:
> 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...


Re: Datarow to string by Cor

Cor
Sat Mar 15 03:02:34 CDT 2008

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