将sqldatareader转变成datatable的vb.net代码

80酷酷网    80kuku.com

  Public Function GetTable(ByVal _reader As System.Data.SqlClient.SqlDataReader) As System.Data.DataTable
            On Error Resume Next
            Dim _table As System.Data.DataTable = _reader.GetSchemaTable()
            Dim _dt As System.Data.DataTable = New System.Data.DataTable()
            Dim _dc As System.Data.DataColumn
            Dim _row As System.Data.DataRow
            Dim _al As System.Collections.ArrayList = New System.Collections.ArrayList()
            Dim i = 0
            For i = 0 To _table.Rows.Count() - 1
                _dc = New System.Data.DataColumn()
                If (Not _dt.Columns.Contains(_table.Rows(i)("ColumnName").ToString())) Then
                    _dc.ColumnName = _table.Rows(i)("ColumnName").ToString()
                    _dc.Unique = Convert.ToBoolean(_table.Rows(i)("IsUnique"))
                    _dc.AllowDBNull = Convert.ToBoolean(_table.Rows(i)("AllowDBNull"))
                    _dc.ReadOnly = Convert.ToBoolean(_table.Rows(i)("IsReadOnly"))
                    _dc.ReadOnly = Convert.ToBoolean(_table.Rows(i)("IsReadOnly"))
                    _dc.ReadOnly = Convert.ToBoolean(_table.Rows(i)("IsReadOnly"))
                    _dc.ReadOnly = Convert.ToBoolean(_table.Rows(i)("IsReadOnly"))
                    _al.Add(_dc.ColumnName)
                    _dt.Columns.Add(_dc)
                End If
            Next
            While _reader.Read()
                _row = _dt.NewRow()
                For i = 0 To _al.Count
                    _row(CStr(_al(i))) = _reader(CStr(_al(i)))
                Next
                _dt.Rows.Add(_row)
            End While
            Return _dt
        End Function

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: