2009.06.14 ' --------------------------------------------------------------------------- ' DebugUtility: 主要目的是查詢 DataSet/DataTable/DataRow 的值. ' ' TONY.971001.COMMENT. ' --------------------------------------------------------------------------- Public Class DebugUtility Public Shared Function DataSetToStringAligned(ByVal vds As Object) As String If vds Is Nothing Then Return "DataSet is nothing" End If Dim ds As DataSet = CType(vds, DataSet) Dim s As String = String.Empty For Each dt As DataTable In ds.Tables s += DataTableToStringAligned(dt) + vbNewLine Next Return s End Function Public Shared Function DataTableToStringAligned(ByVal vdt As Object) As String If vdt Is Nothing Then Return "DataTable is nothing" End If Dim s As String = String.Empty Dim dt As DataTable = CType(vdt, DataTable) s += dt.TableName + vbNewLine ' 算每各 columnname & columnvalue 的最大長度. ------- Dim iColLengths() As Integer = UtilitySub._GetColumnMaxLen(dt) ' 取得欄位名稱 s += UtilitySub._BuildColumnNameString(iColLengths, dt) + vbNewLine ' 取得每個 row 的 string. For Each dr As DataRow In dt.Rows s += UtilitySub._BuildRowValueString(iColLengths, dr) + vbNewLine Next Return s End Function Public Shared Function DataRowToStringAligned(ByVal vdr As Object) As String If vdr Is Nothing Then Return "DataRow is nothing " End If Dim s As String = String.Empty Dim dr As DataRow = CType(vdr, DataRow) Dim dt As DataTable = dr.Table If dt Is Nothing Then Return "DataRow must be in Table !" End If s += dt.TableName + vbNewLine ' 算每各 columnname & columnvalue 的最大長度. ------- Dim iColLengths() As Integer = UtilitySub._GetColumnMaxLen(CType(vdr, DataRow)) ' 取得欄位名稱 s += UtilitySub._BuildColumnNameString(iColLengths, dt) + vbNewLine ' 取得每個 row 的 string. s += UtilitySub._BuildRowValueString(iColLengths, dr) Return s End Function Public Shared Function DataRowToString(ByVal vdr As Object) As String If vdr Is Nothing Then Return "DataRow is nothing " End If Dim s As String = String.Empty Dim dr As DataRow = CType(vdr, DataRow) Dim dt As DataTable = dr.Table s += dt.TableName + vbNewLine Dim bFirst As Boolean = True For Each dc As DataColumn In dt.Columns If bFirst = True Then bFirst = False s += "RowState " End If s += dc.ColumnName + " " Next s += vbNewLine bFirst = True For Each dc As DataColumn In dt.Columns If bFirst = True Then bFirst = False If dr.RowState = DataRowState.Added Then s += "Added " ElseIf dr.RowState = DataRowState.Deleted Then s += "Deleted " ElseIf dr.RowState = DataRowState.Detached Then s += "Detached " ElseIf dr.RowState = DataRowState.Modified Then s += "Modified " ElseIf dr.RowState = DataRowState.Unchanged Then s += "Unchanged " End If End If If dr.RowState <> DataRowState.Deleted Then If dr(dc) Is Nothing Or dr(dc) Is DBNull.Value Then s += "null " Else s += dr(dc).tostring + " " End If Else If dr(dc, DataRowVersion.Original) Is Nothing Or dr(dc, DataRowVersion.Original) Is DBNull.Value Then s += "null " Else s += dr(dc, DataRowVersion.Original).tostring + " " End If End If Next Return s End Function Public Shared Function DataTableToString(ByVal vdt As Object) As String If vdt Is Nothing Then Return "DataTable is nothing" End If Dim s As String = String.Empty Dim dt As DataTable = CType(vdt, DataTable) s += dt.TableName + vbNewLine Dim bFirst As Boolean = True For Each dc As DataColumn In dt.Columns If bFirst = True Then bFirst = False s += "RowState " End If s += dc.ColumnName + " " Next s += vbNewLine For Each dr As DataRow In dt.Rows bFirst = True For Each dc As DataColumn In dt.Columns If bFirst = True Then bFirst = False If dr.RowState = DataRowState.Added Then s += "Added " ElseIf dr.RowState = DataRowState.Deleted Then s += "Deleted " ElseIf dr.RowState = DataRowState.Detached Then s += "Detached " ElseIf dr.RowState = DataRowState.Modified Then s += "Modified " ElseIf dr.RowState = DataRowState.Unchanged Then s += "Unchanged " End If End If If dr.RowState <> DataRowState.Deleted Then If dr(dc) Is Nothing Or dr(dc) Is DBNull.Value Then s += "null " Else s += dr(dc).tostring + " " End If Else If dr(dc, DataRowVersion.Original) Is Nothing Or dr(dc, DataRowVersion.Original) Is DBNull.Value Then s += "null " Else s += dr(dc, DataRowVersion.Original).tostring + " " End If End If Next s += vbNewLine Next s += vbNewLine Return s End Function Public Shared Function DataSetToString(ByVal vds As Object) As String If vds Is Nothing Then Return "DataSet is nothing" End If Dim s As String = String.Empty Dim ds As DataSet = CType(vds, DataSet) For Each dt As DataTable In ds.Tables s += dt.TableName + vbNewLine Dim bFirst As Boolean = True For Each dc As DataColumn In dt.Columns If bFirst = True Then bFirst = False s += "RowState " End If s += dc.ColumnName + " " Next s += vbNewLine For Each dr As DataRow In dt.Rows bFirst = True For Each dc As DataColumn In dt.Columns If bFirst = True Then bFirst = False If dr.RowState = DataRowState.Added Then s += "Added " ElseIf dr.RowState = DataRowState.Deleted Then s += "Deleted " ElseIf dr.RowState = DataRowState.Detached Then s += "Detached " ElseIf dr.RowState = DataRowState.Modified Then s += "Modified " ElseIf dr.RowState = DataRowState.Unchanged Then s += "Unchanged " End If End If If dr.RowState <> DataRowState.Deleted Then If dr(dc) Is Nothing Or dr(dc) Is DBNull.Value Then s += "null " Else s += dr(dc).tostring + " " End If Else If dr(dc, DataRowVersion.Original) Is Nothing Or dr(dc, DataRowVersion.Original) Is DBNull.Value Then s += "null " Else s += dr(dc, DataRowVersion.Original).tostring + " " End If End If Next s += vbNewLine Next s += vbNewLine Next Return s End Function End Class Public Class UtilitySub Public Shared Function _GetColumnMaxLen(ByVal vdr As DataRow) As Integer() If vdr.Table Is Nothing Then Return Nothing End If Dim vdt As DataTable = vdr.Table Dim iLens(vdt.Columns.Count) As Integer iLens(0) = "RowState ".Length Dim idx As Integer = 1 For Each dc As DataColumn In vdt.Columns iLens(idx) = System.Text.Encoding.Default.GetByteCount(dc.ColumnName) + 1 idx += 1 Next Dim dr As DataRow = vdr idx = 0 Dim iLen As Integer = _GetRowStateString(dr.RowState).Length + 1 If iLen > iLens(idx) Then iLens(idx) = iLen End If idx += 1 For Each dc As DataColumn In vdt.Columns iLen = System.Text.Encoding.Default.GetByteCount(_GetCellValueString(dr.RowState, dr, dc)) + 1 If iLen > iLens(idx) Then iLens(idx) = iLen End If idx += 1 Next Return iLens End Function Public Shared Function _GetColumnMaxLen(ByVal vdt As DataTable) As Integer() Dim iLens(vdt.Columns.Count) As Integer iLens(0) = "RowState ".Length Dim idx As Integer = 1 For Each dc As DataColumn In vdt.Columns iLens(idx) = System.Text.Encoding.Default.GetByteCount(dc.ColumnName) + 1 idx += 1 Next For Each dr As DataRow In vdt.Rows idx = 0 Dim iLen As Integer = _GetRowStateString(dr.RowState).Length + 1 If iLen > iLens(idx) Then iLens(idx) = iLen End If idx += 1 For Each dc As DataColumn In vdt.Columns iLen = System.Text.Encoding.Default.GetByteCount(_GetCellValueString(dr.RowState, dr, dc)) + 1 If iLen > iLens(idx) Then iLens(idx) = iLen End If idx += 1 Next Next Return iLens End Function Public Shared Function _BuildRowValueString(ByVal iLens() As Integer, ByVal vdr As DataRow) As String Dim sb As New System.Text.StringBuilder sb.Append(_GetRowStateString(vdr.RowState)) sb.Append(CChar(" "), iLens(0) - _GetRowStateString(vdr.RowState).Length) Dim idx As Integer = 1 For Each dc As DataColumn In vdr.Table.Columns sb.Append(_GetCellValueString(vdr.RowState, vdr, dc)) sb.Append(CChar(" "), iLens(idx) - System.Text.Encoding.Default.GetByteCount(_GetCellValueString(vdr.RowState, vdr, dc))) idx += 1 Next Return sb.ToString End Function Public Shared Function _BuildColumnNameString(ByVal iLens() As Integer, ByVal vTable As DataTable) As String Dim sb As New System.Text.StringBuilder sb.Append("RowState") sb.Append(CChar(" "), iLens(0) - "RowState".Length) Dim idx As Integer = 1 For Each dc As DataColumn In vTable.Columns sb.Append(dc.ColumnName) sb.Append(CChar(" "), iLens(idx) - System.Text.Encoding.Default.GetByteCount(dc.ColumnName)) idx += 1 Next Return sb.ToString End Function Public Shared Function _GetCellValueString(ByVal vRowState As System.Data.DataRowState, ByVal vdr As DataRow, ByVal vdc As DataColumn) As String If vRowState = DataRowState.Deleted Then If vdr(vdc, DataRowVersion.Original) Is DBNull.Value Then Return "null" Else Return vdr(vdc, DataRowVersion.Original).ToString.TrimEnd End If Else If vdr(vdc) Is DBNull.Value Then Return "null" Else Return vdr(vdc).ToString.TrimEnd End If End If End Function Public Shared Function _GetRowStateString(ByVal vRowState As System.Data.DataRowState) As String Dim s As String = String.Empty If vRowState = DataRowState.Added Then s = "Added" ElseIf vRowState = DataRowState.Deleted Then s = "Deleted" ElseIf vRowState = DataRowState.Detached Then s = "Detached" ElseIf vRowState = DataRowState.Modified Then s = "Modified" ElseIf vRowState = DataRowState.Unchanged Then s = "Unchanged" End If Return s End Function End Class