如何从选择的存储过程中获取返回值,即返回的记录数。
我所追求的值是RETURN(SELECT COUNT(*)FROM #Eligible_Ids);
我能够做到的唯一方法如下:
DECLARE @NumberOfResults INT
SET @NumberOfResults =(SELECT COUNT(*)FROM #Eligible_Ids)
然后在select语句中我有NumberResults = @NumberOfResults,但这会为重新调整的数据增加一行。
我在课堂上的代码是
using (SqlCommand cmd = new SqlCommand(spName, cn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@strSearchPhrase", SqlDbType.VarChar, 100));
cmd.Parameters.Add(new SqlParameter("@SearchMode", SqlDbType.Int, 4));
cmd.Parameters.Add(new SqlParameter("@intPageNumber", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@intRecordsPerPage", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@intTotalRecordsReturned", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int));
cmd.Parameters["@RETURN_VALUE"].Direction = ParameterDirection.ReturnValue;
cmd.Parameters["@strSearchPhrase"].Value = q;
cmd.Parameters["@SearchMode"].Value = 1;
cmd.Parameters["@intPageNumber"].Value = pagenumber;
cmd.Parameters["@intRecordsPerPage"].Value = 10;
cmd.Parameters["@intTotalRecordsReturned"].Value = 10;
cn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.Default))
{
if (rdr.HasRows)
{
while (rdr.Read())
{
Data.Add(new DisplaySearchResults
{
Title = (string)rdr["PageTitle"],
Description = (string)rdr["PageParagraph"],
URL = (string)rdr["PageURL"],
returnvalue = (int)rdr["NumberResults"]
});
}
}
return Data;
任何以正确方式执行此操作的帮助都将受到赞赏
乔治
答案 0 :(得分:2)
要获取返回值,请添加一个方向ReturnValue的参数。既然你已经这样做了,那就是:
var val = cmd.Parameters["@RETURN_VALUE"].Value;
重要提示:您只能在阅读了阅读器中的所有结果行后才能阅读 :参数值和返回值位于TDS流的末尾。
答案 1 :(得分:2)
请参阅this
在您的具体情况下,请在c#Coding中更改此内容
cmd.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int));
cmd.Parameters["@RETURN_VALUE"].Direction = ParameterDirection.ReturnValue;
到这个
cmd.Parameters.Add(new SqlParameter("@NumberOfResults", SqlDbType.Int));
cmd.Parameters["@NumberOfResults"].Direction = ParameterDirection.Output;
并在SotoredProcedure中而不是声明
DECLARE @NumberOfResults INT
添加输出参数
@NumberOfResults INT output
然后你可以像这样
读取@NumberofResults参数中的值TextBox1.Text = cmd.Parameters["@NumberOfResults"].Value.ToString();
答案 2 :(得分:0)
--Simple T-Sql Proc
CREATE PROCEDURE usp_OutputRowExample
@Count INT OUTPUT
AS
BEGIN
SELECT * FROM <table>; --Get your entities
SELECT @Count = @@ROWCOUNT; --@@ROWCOUNT returns the amount of
--affected rows from the last query
END
//From C#
public Tuple<IEnumerable<object>, int>> GetObjects()
{
using(var connection = new SqlConnection(_connectionString))
{
connection.Open();
using(var command = sqlConnection.CreateCommand())
{
command.CommandText = "usp_OutputRowExample";
command.CommandType = CommandType.StoredProcedure;
var outputParameter = new SqlParameter("@Output", SqlDbType.Int)
{ Direction = ParameterDirection.Output };
command.Parameters.Add(outputParameter);
using(var reader = command.ExecuteReader())
{
var entities = new List<entities>();
while(reader.Read())
{
//Fill entities
}
}
var outputCount = outputParameter.Value as int? ?? default(int);
return new Tuple<IEnumerable<object>, int>(entities, outputCount);
}
}
}