获取存储过程的输出

时间:2017-11-10 08:24:51

标签: c# sql stored-procedures ado.net output

我有一个stored procedure,它返回sql中最近更新的行的id列表。我如何获得数组或列表?

存储过程:

CREATE procedure [dbo].[spUpdateResolutionStatus]
@SummaryId int,
@AlertType varchar(500),
@AlertServer varchar(250),
@Subject varchar(250)
as
Begin
declare @insertedIds table (id int)
    Update WCFLogs set ResolutionSummary_OID = @SummaryId, Status_oid = 2
    output inserted.log_oid into @insertedIds
where AlertType=@AlertType and AlertServer= @AlertServer and Log_Subject = @Subject;
select id from @insertedIds
end
GO

我的方法:

 public int[] UpdateIssueResolutionStatus(Int32 summaryId, string alerttype, string alertserver, string subject)
{
    int[] rowseffected;
    string cs = ConfigurationManager.ConnectionStrings["LogsData"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand("spUpdateResolutionStatus", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@SummaryId", summaryId));
        cmd.Parameters.Add(new SqlParameter("@AlertType", alerttype));
        cmd.Parameters.Add(new SqlParameter("@AlertServer", alertserver));
        cmd.Parameters.Add(new SqlParameter("@Subject", subject));
        con.Open();
        rowseffected =(Int)cmd.ExecuteScalar();
    }
    return rowseffected;
}

ExecuteScalar给我第一行的ID,我怎样才能获得整个列表?

1 个答案:

答案 0 :(得分:3)

public int[] UpdateIssueResolutionStatus(Int32 summaryId, string alerttype, string alertserver, string subject)
{
    int[] rowseffected;
    string cs = ConfigurationManager.ConnectionStrings["LogsData"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand("spUpdateResolutionStatus", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@SummaryId", summaryId));
        cmd.Parameters.Add(new SqlParameter("@AlertType", alerttype));
        cmd.Parameters.Add(new SqlParameter("@AlertServer", alertserver));
        cmd.Parameters.Add(new SqlParameter("@Subject", subject));
        con.Open();

        using (IDataReader reader = cmd.ExecuteReader())
        {
            List<int> ids = new List<int>();
            while (reader.Read())
            {
                ids.Add(reader.GetInt32(0));
            }

            return ids.ToArray();
        }
    }
}