Retrieve a number from a stored procedure
621482Feb 21 2008 — edited Feb 25 2008I need to retrieve the number of rows returned from a query in a stored procedure. This is what I currently have:
PACKAGE PART_DATA AS
FUNCTION GetPartDataRowCount(Company IN PART_DATA.PART1_CO_SITE%TYPE) RETURN NUMBER;
END;
PACKAGE BODY PART_DATA AS
FUNCTION GetPartDataRowCount
(
Company IN PART_DATA.PART1_CO_SITE%TYPE
)
RETURN NUMBER
IS
RecordCount NUMBER;
BEGIN
SELECT COUNT(*)
INTO RecordCount
FROM PART_DATA
WHERE PART1_CO_SITE = Company;
RETURN RecordCount;
END GetPartDataRowCount;
END;
And the .NET part:
Public Function GetPartDataRowCount(ByVal Company As String) As Integer
Dim con As New OracleConnection(MyConnectionString)
'Open the connection
con.Open()
'Set the command text, type and connection
Dim cmd As New OracleCommand("PART_DATA.GetPartDataRowCount", con)
cmd.CommandType = CommandType.StoredProcedure
'Create parameter objects
Dim returnValue As New OracleParameter
returnValue.Direction = ParameterDirection.ReturnValue
returnValue.DbType = DbType.Int32
cmd.Parameters.Add(returnValue)
Dim pCompany As New OracleParameter
pCompany.Direction = ParameterDirection.Input
pCompany.DbType = DbType.AnsiStringFixedLength
pCompany.Value = Company
cmd.Parameters.Add(pCompany)
'Execute the query
cmd.ExecuteNonQuery()
'Capture the returned value
Dim rowCount As Integer = Convert.ToInt32(returnValue.Value)
'Return the returned value
Return rowCount
'Clean up objects
returnValue.Dispose()
cmd.Dispose()
con.Dispose()
End Function
I am using this as the SelectCountMethod on an ObjectDataSource for custom paging a GridView. Everything is working but because I'm new to Oracle I can't help thinking that there is a better way to achieve what I need, also I am confused as to whether I should be using a Function or a Procedure in my Package for this.
I would be grateful if someone could either confirm that what I am doing is the best/correct method or show me what I should be doing instead.
Any help much appreciated.