Reading SQL OUTPUT Parameter
Mar 13, 2011 at 2:41pm Mar 13, 2011 at 2:41pm UTC
Hello. I am having great difficulty in extracting the value from a SQL Server 2008 R2 Stored Procedure using Managed C++.
In my database I have a procedure defined like this.
1 2 3 4 5 6 7
CREATE PROCEDURE SelectDataRange
@From VARCHAR(12), @To VARCHAR(12), @RowCount INT = -1 OUTPUT
AS
BEGIN
...
SET @RowCount = @@ROWCOUNT ;
END
Within Management Studio this returns the number of rows, however from in my code it is another matter. Here is what I have.
1 2 3 4 5 6 7 8 9 10
Int32 Rows = 0 ;
SqlCommand ^ m_cmd = m_Connection->CreateCommand();
m_cmd->CommandText = "SelectDataRange" ;
m_cmd->CommandType = ::CommandType::StoredProcedure ;
m_cmd->Parameters->AddWithValue("@From" , '2011-01-01' ) ;
m_cmd->Parameters->AddWithValue("@To" , '2011-01-04' ) ;
SqlParameter^ param = m_cmd->Parameters->Add("@RowCount" , SqlDbType::Int);
param->Direction = ParameterDirection::Output ;
SqlDataReader ^ m_Reader = m_cmd->ExecuteReader() ;
Rows = Convert::ToInt32(m_cmd->Parameters["@RowCount" ]->Value) ;
I have tried many many variations of this but regardless of how I do it the Rows variable is either 0 or the application crashes on the Convert:: line.
Changing the procedure and code to deal with a RETURN parameter yields the same result or error. I have read the content on many a posting on the Internet but I simply cannot see what or where I am going wrong.
Can anyone help me please?
--
Bill
Mar 13, 2011 at 2:46pm Mar 13, 2011 at 2:46pm UTC
You should probably search for help elsewhere as well, this is a C++ forum and few people here actually write stuff in C++/CLI.
Mar 13, 2011 at 3:30pm Mar 13, 2011 at 3:30pm UTC
OK, thanks for the reply, but is there anywhere you or anyone else could recommend where I should post this?
C++/CLI Forum seems to be very thin on the ground.
--
Bill
Topic archived. No new replies allowed.