vedi GetSchemaTable,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataidatareaderclassgetschematabletopic.asp
implementato in ogni IDataReader.
HTH M.rkino
--
Marco Barzaghi - [MVP - MCP]
http://mvp.support.microsoft.com - http://italy.mvps.org
UGIDotNet - User Group Italiano .NET, http://www.ugidotnet.org
Read my WebLog: http://blogs.ugidotnet.org/markino
Sempre se ho capito bene , con il metodo GetSchemaTable
devo partire da dbcommand e un datareader ,
Percio dovrei eseguire di nuovo la select ?
Grazie per l'attenzione
"Marco Barzaghi" ha scritto:
beh fare due volte la chiamata mi sembra controproducente...
puoi usare il metodo protetto "DbDataAdapter.Fill Method (DataTable, IDataReader)"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatacommondbdataadapterclassfilltopic4.asp
e agire così:
implementi una classe di utilità ( "MyDataAdapter" ) come quella qui descritta:
http://blogs.ugidotnet.org/markino/archive/2003/09/20/575.aspx (è scritta in c# ma non è difficile
da tradurre). La classe manca di un metodo che serve a te:
public class MyDataAdapter: DbDataAdapter
public shared sub Fill(dt as datatable, dr as IDataReader)
dim adp as MyDataAdapter = new MyDataAdapter()
adp.Fill(dt, dr)
end sub
'...
end class
Public Function GetDataTable(ByVal strsql As String, ByVal cn As OracleConnection, ByRef DataTable
schemTable) As DataTable
Try
If cn.State = ConnectionState.Closed Then cn.Open()
Dim command as OracleCommand = new OracleCommand()
command.CommandText = strsql)
dim reader as IDataReader = command.ExecuteReader()
schemaTable = reader.GetSchemaTable()
dim dt as new DataTable()
MyDataAdapter.Fill(dt, reader)
reader.Close()
Return dt
Catch ex As Exception
Throw ex 'ritorna l'errore al chiamante
End Try
End Function
per usarla:
dim schemtable as Datatable 'Questo sarà il parametro di output della procedura
dim dt as DataTable = MyDataAccess.GetDataTable("SELECT * FROM TABLE", myConn, byRef schemaTable)
PS: scusa se ho messo qualche errore di sintassi in vb ma uso principalmente c# :-p
Public Class MyDataAdapter
Inherits DbDataAdapter
'Definisco e implemento un metodo statico che dato un IDataReader
popola
'un DataSet a partire da una certa posizione (startRecord) per un
certo numero
'di occorrenze (maxRecords)
Public Shared Sub Fill(ByVal dt As DataTable, ByVal dr As IDataReader)
Dim adp As MyDataAdapter = New MyDataAdapter()
MyDataAdapter.Fill(dt, dr)
End Sub
'DbDataAdapter è una classe astratta che richiede l'implementazione di
'alcuni metodi, darò loro una inutile implementazione e impedirò la
creazione di istanze
'di questa classe definendo esplicitamente privato il costruttore...
in fondo a me servirà
'esclusivamente la il metodo Fill statico sopra definito.
Protected Function CreateRowUpdatedEvent() As RowUpdatedEventArgs
'{...}
Throw New NotSupportedException()
End Function 'CreateRowUpdatedEvent
Protected Function CreateRowUpdatingEvent() As RowUpdatingEventArgs
'{...}
Throw New NotSupportedException()
End Function 'CreateRowUpdatingEvent
Protected Sub OnRowUpdated() '{...}
Throw New NotSupportedException()
End Sub 'OnRowUpdated
Protected Sub OnRowUpdating() '{...}
Throw New NotSupportedException()
End Sub 'OnRowUpdating
'definizione esplicita del costruttuttore
Private Sub New()
End Sub 'New
End Class 'MyDataAdapter
ed il mio metodo :
Public Function GetDataTable(ByVal strsql As String, ByVal cn As
SqlConnection, ByRef schemaTable As DataTable) As DataTable
Try
If cn.State = ConnectionState.Closed Then cn.Open()
Dim command As New SqlCommand
command.CommandText = strsql
Dim reader As IDataReader
command.Connection = cn
reader = command.ExecuteReader()
schemaTable = reader.GetSchemaTable()
Dim dt As New DataTable()
MyDataAdapter.Fill(dt, reader)
reader.Close()
Return dt
Catch ex As Exception
Throw ex 'ritorna l'errore al chiamante
End Try
End Function
il problema e che qundo arrivo all'istruzione MyDataAdapter.Fill(dt, reader)
mi solleva l'eccezione StackOverflowException
di e non so come risolverla
Grazie Mille
Il problema è che la fill da te implementata continua a richiamarsi ricorsivamente... non devi usare
il metodo stattico ma qllo di istanza...
ho dato un un okkio al dbdataadapeter e ho visto che nella 2.0 ci sono meno metodi astratti da
implememntare rispetto come alla versione 1.1
prova a includere nella tua classe il metodo così definito:
Public Class DbDataAdapterUtils
Inherits System.Data.Common.DbDataAdapter
Public Shared Sub FillFromReader(ByVal dt As DataTable, ByVal dr As IDataReader)
Dim adp As DbDataAdapterUtils = New DbDataAdapterUtils()
adp.Fill(dt, dr)
End Sub
End Class
Ho chiamato il metodo "FillFromReader" per non essere costretto a usare la keywords "Shadows"
perchè esiste già un metodo simile (per nome) nella classe base.
grazie alla tuoa spiegazione ora mi e chiaro
Grazie & Ciao