I started using this and found a problem. The data in the datarows is
converted but the datacolumn datatype stays as a byte array. If the dataset
is serialized for any reason (remoting was mine), there is an
invalidcastexce ption. Any suggestions on fixing this issue? I tried changing
the datacolumn.data type but got an error that it can't be changed after the
dataset is created. In the mean time, I rewrote the function as follows:
Private Function Raw16ToGuid(ByR ef dataSet As DataSet) As DataSet
' Creates a new dataset with GUID column and converted datatype
in place of byte arrays
Dim table As DataTable
Dim newDataSet As New DataSet
For Each table In dataSet.Tables
Dim newDataTable As New DataTable(table .TableName)
Dim columnIndices As New ArrayList
Dim dataColumn As DataColumn
For Each dataColumn In table.Columns
If dataColumn.Data Type Is
System.Type.Get Type("System.By te[]") Then
columnIndices.A dd(dataColumn.O rdinal)
Dim newDataColumn As New
DataColumn(data Column.ColumnNa me, System.Type.Get Type("System.Gu id"))
newDataTable.Co lumns.Add(newDa taColumn)
Else
Dim newDataColumn As New
DataColumn(data Column.ColumnNa me, dataColumn.Data Type)
newDataTable.Co lumns.Add(newDa taColumn)
End If
Next
Dim dataRow As DataRow
For Each dataRow In table.Rows
Dim index As Integer = 0
Dim newDataRow As DataRow = newDataTable.Ne wRow
For Each item As Object In dataRow.ItemArr ay
If columnIndices.C ontains(index) Then
newDataRow(inde x) = New Guid(CType(item ,
Byte())) ' convert guid
Else
newDataRow(inde x) = item
End If
index += 1
Next
newDataTable.Ro ws.Add(newDataR ow)
Next
newDataSet.Tabl es.Add(newDataT able)
Next
Return newDataSet
End Function
This creates a new dataset with the row translated. I am sure this is less
efficient but I can't find a way to cleanly translate the datatype in place.
Thanks for any feedback.
"Jim Shank" wrote:
I am adding support to my application for Oracle 10g and using Enterprise
Library Data Access Application Blocks and trying to determine the best way
to convert the GUID's which are stored as RAW(16) in Oracle (Byte Arrays)
back to GUID's in my data layer. My best guess is a conversion method that
the DataSet goes through before leaving the data access layer. Here is my
code:
Private Sub Raw16ToGuid(ByR ef dataSet As DataSet)
Dim columnIndices As New ArrayList
Dim dataColumn As DataColumn
For Each DataColumn In dataSet.Tables( 0).Columns
If dataColumn.Data Type Is System.Type.Get Type("System.By te[]")
Then
columnIndices.A dd(dataColumn.O rdinal)
End If
Next
Dim dataRow As DataRow
For Each dataRow In dataSet.Tables( 0).Rows
Dim index As Integer = 0
For Each index In columnIndices
dataRow(index) = New Guid(DirectCast (dataRow(index) , Byte()))
Next
Next
dataSet.AcceptC hanges()
End Sub
I know this will attempt to convert any byte array to a GUID but since I
don't use any other byte arrays currently, it seems better than checking each
dataItem for type and length before attempting the conversion. Any feedback
or suggestions would be appreciated.