Estrazione dello schema di una tabella usando i datareader

Ho trovato un modo alternativo al metodo GetOledbSchema di OledbConnection.Penso che lo utilizzerò per la classe che sto costruendo per l'accesso ai database :


Public Function GetDataFromReader(ByVal cn As OleDbConnection, ByVal tabella As String, ByVal ls As ListBox)

Dim cmd As New OleDbCommand

Dim schemaTable As DataTable

Dim myReader As OleDbDataReader

Dim myField As DataRow

Dim myProperty As DataColumn

cn.Open()

cmd.Connection = cn

cmd.CommandText = "SELECT * FROM " & tabella

myReader = cmd.ExecuteReader()

'Retrieve column schema into a DataTable.

schemaTable = myReader.GetSchemaTable()

'For each field in the table...

For Each myField In schemaTable.Rows

'For each property of the field...

For Each myProperty In schemaTable.Columns

'Display the field name and value.

If myProperty.ColumnName = "ProviderType" Then

Dim tipo As OleDbType = GetTipo(myField(myProperty).ToString)

ls.Items.Add(myProperty.ColumnName & " = " & tipo.ToString)

Else

ls.Items.Add(myProperty.ColumnName & " = " & myField(myProperty).ToString())

End If

Next

Next

'Always close the DataReader and Connection objects.

myReader.Close()

cn.Close()

End Function

Private Function GetTipo(ByVal tipoCampo As OleDbType) As OleDbType

Return tipoCampo

End Function


posted @ mercoledì 22 dicembre 2004 16:20

Print
«novembre»
domlunmarmergiovensab
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567