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


«dicembre»
domlunmarmergiovensab
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678