Chiamata a Stored di insert tramite ODBC. Non riesco a valutare il @RE...

martedì 20 gennaio 2009 - 10.33

Giobbetto Profilo | Newbie

Salve a tutti.
Ho un problema con VB.NET 2008 sotto XP sp3 con la chiamata ad una stored di insert.
Inizialmente usavo la SQLCommand in questo modo:

***************************************************************************
Dim oCmd as new SQLCommand
Dim oCon as New SQLConnection("MiaConn")

oCmd.CommandText = "MiaStored"
oCmd.CommandType = CommandType.StoredProcedure
oCmd.Connection = oCon
SQLCommandBuilder.DeriveParameter(oCmd)

oCmd.Parameter("@Param1").Value = MioValore1
oCmd.Parameter("@Param2").Value = MioValore2

oCmd.ExecuteNonQuery

IF oCmd.Parameter(0).Value = 0 Then 'Oppure uso .Parameter("@RETURN_VALUE")
Throw new Exception("Errore non scritto nulla")
EndIf
***************************************************************************
Cosi ha sempre funzionato tutto.
Poi ho avuto la necessità di usare ODBC.

Quindi ho convertito tutto in ODBC in questo modo:
***************************************************************************
Dim oCmd as new ODBCCommand
Dim oCon as New ODBCConnection("MiaConn")

oCmd.CommandText = "MiaStored"
oCmd.CommandType = CommandType.StoredProcedure
oCmd.Connection = oCon
ODBCCommandBuilder.DeriveParameter(oCmd)

oCmd.Parameter("@Param1").Value = MioValore1
oCmd.Parameter("@Param2").Value = MioValore2

oCmd.ExecuteNonQuery

IF oCmd.Parameter(0).Value = 0 Then 'Oppure uso .Parameter("@RETURN_VALUE")
Throw new Exception("Errore non scritto nulla")
EndIf
***************************************************************************
Sono iniziati i problemi.
All'esecuzione della query va in errore perchè dice che il @Param1 non è stato passato.

Allora ho trovato la soluzione sostituendo il codice con questo:
***************************************************************************
Dim oCmd as new ODBCCommand
Dim oCon as New ODBCConnection("MiaConn")

oCmd.CommandText = "{call MiaStored(?,?)}"
oCmd.CommandType = CommandType.StoredProcedure
oCmd.Connection = oCon

oCmd.Parameter.Add("@Param1", OdbcType.VarChar).Value = MioValore1
oCmd.Parameter.Add("@Param2", OdbcType.VarChar).Value = MioValore2

oCmd.ExecuteNonQuery
***************************************************************************
E la stored funziona. Ma c'è un modo per usare l'ODBCCommandbuilder per derivare i parametri? Boh!
Io non ci sono riuscito...

Volendo testare il @Return_Value ho aggiunto il parametro:
oCmd.Parameter.Add("@Return_Value", OdbcType.Int).Direction = ParameterDirection.ReturnValue
senza modificare il numero di ? nel command text perchè altrimenti da errore dicendo che sono stati passati
troppo parametri.
Dopo ExecuteNonQuery ho aggiunto il test del parametro "@Return_Value" ma torna sempre DBNULL quando in
realtà eseguendo la stored da SQL il Return Value è valorizzato.
Non volevo aggiungere un parametro di OUTPUT a tutte le stored per ovviare a questo problema.
Avete qualche idea?
Magari anche su come usare il DeriveParameter?

Grazie in anticipo
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5