I suggest that you use your SqlConnection
and SqlCommand
to use the blocks to ensure their correct utility.
In addition, if I am not mistaken, the output parameters are available only after a complete reading of the returned dataset.
Since you don't need it at all - why not just use .ExecuteNonQuery()
? Does this fix the problem?
using (SqlConnection con = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=answers;Integrated Security=True")) using (SqlCommand cmd = new SqlCommand("dbo.GetRowCount", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@count", SqlDbType.Int)); cmd.Parameters["@count"].Direction = ParameterDirection.Output; con.Open(); cmd.ExecuteNonQuery();
Also: since it seems like you're really interested in the number of lines - why not simplify your stored procedure like this:
ALTER PROCEDURE GetRowCount AS SELECT COUNT(*) FROM Emp WHERE age > 30;
and then use this C # code snippet:
con.Open(); object result = cmd.ExecuteScalar(); if(result != null) { int ans = Convert.ToInt32(result); } con.Close();
source share