Groups | Blog | Home
all groups > dotnet ado.net > april 2007 >

dotnet ado.net : SQLCommand ParameterDirection.ReturnValue not what it was


Tom Garth
4/26/2007 4:46:04 PM
With classic ADO you could attach a ReturnValue parameter to a command
containing inline parameters.
Ex:

Cmd.CommandString = "PubsInsert('G. B. Shaw', 'I lost my Rifle')"
Dim pRET As New ADODB.Parameter
pRET.Type = adInteger
pRET.Direction = adParamReturnValue
Cmd.Parameters.Append pRET
Cmd.Execute

lngNewID = pRET.Value
etc., etc,.

In this case the T-SQL procedure ends with something like RETURN @@IDENTITY.

I can't find a way to get a ReturnValue type parameter to actually return
the RETURN value with ADO.NET.

Is this no longer possible?

Tom Garth
--
William (Bill) Vaughn
4/26/2007 10:38:30 PM
It's pretty easy.
Private Sub BuildCommand()
Try
cmd = New SqlCommand("ReturnAuthorCountByYearBorn", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@YearWanted", 1947)
cmd.Parameters.Add("@AuthorCount",SqlDbType.Int)
cmd.Parameters("@AuthorCount").Direction _
= ParameterDirection.Output
cmd.Parameters.Add("@ReturnValue",SqlDbType.BigInt)
cmd.Parameters("@ReturnValue").Direction _
= ParameterDirection.ReturnValue

Dim dr As SqlDataReader
cn.Open()
dr = cmd.ExecuteReader()
Dim dt As New DataTable
dt.Load(dr)
Dim intReturnValue As Integer
intReturnValue = CInt(cmd.Parameters("@ReturnValue").Value)
Catch exsql As SqlException
MessageBox.Show(exsql.ToString)
Catch ex As Exception
Debug.Assert(False, ex.ToString)
Finally
cn.Close()
End Try
End Sub

There are a couple of issues going on in the code. I assume SqlClient. This
provider supports named parameters. Not all do. Note that I have fetched all
of the rows in the resultset before trying to fetch the output parameter or
return value parameter.

This is discussed in greater detail in my book.

hth


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

[quoted text, click to view]

AddThis Social Bookmark Button