데이터베이스 조회시 사용하는 Reader 를 데이터셋으로 변환하는 함수입니다
Public Function DRtoDS(ByVal reader As SqlClient.SqlDataReader) As DataSet
Dim dataSet As DataSet = New DataSet
Dim dataRow As DataRow
Dim columnName As String
Dim column As DataColumn
Dim schemaTable As DataTable
Dim dataTable As DataTable
Do
' Create new data table
schemaTable = reader.GetSchemaTable
dataTable = New DataTable
If Not IsDBNull(schemaTable) Then
' A query returning records was executed
Dim i As Integer
For i = 0 To schemaTable.Rows.Count - 1
dataRow = schemaTable.Rows(i)
' Create a column name that is unique in the data table
columnName = dataRow("ColumnName")
'Add the column definition to the data table
column = New DataColumn(columnName, CType(dataRow("DataType"), Type))
dataTable.Columns.Add(column)
Next
dataSet.Tables.Add(dataTable)
'Fill the data table we just created
While reader.Read()
dataRow = dataTable.NewRow()
For i = 0 To reader.FieldCount - 1
dataRow(i) = reader(i)
Next
dataTable.Rows.Add(dataRow)
End While
Else
'No records were returned
column = New DataColumn("RowsAffected")
dataTable.Columns.Add(column)
dataSet.Tables.Add(dataTable)
dataRow = dataTable.NewRow()
dataRow(0) = reader.RecordsAffected
dataTable.Rows.Add(dataRow)
End If
Loop While reader.NextResult()
Return dataSet
End Function
'Programming' 카테고리의 다른 글
DevExpress NavGroup 에 아이템추가하기 (0) | 2010.10.22 |
---|---|
데이터뷰(DataGridView) 의 내용을 탭으로분리된 파일로 저장하기 (0) | 2009.06.15 |
사용자정의 컨트롤 속성만들때 기본값 설정하는방법 (0) | 2008.03.20 |
콤보박스 목록구성 방법 (0) | 2008.03.20 |
파포인트 콤보박스 목록 코드로 생성 (0) | 2008.03.07 |