【问题标题】:Stored procedure with input & output parameters and 2 recordsets具有输入和输出参数和 2 个记录集的存储过程
【发布时间】:2021-04-14 22:27:56
【问题描述】:

我尝试从我的存储过程中提取 c# asp.net 中的结果,但它有 2 个记录集。第一个有 1 行,第二个有很多行作为日期。

代码

public string penth_slqDAYS(string connectionString)
{
    string sMonth = DateTime.Now.Month.ToString();
    string syear = DateTime.Now.Year.ToString();

    try
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command1 = new SqlCommand("penthhmera_proc", connection);                
            /////////////////////////////
            SqlParameter param1;
            param1 = command1.Parameters.Add("@prs_nmb_pen", SqlDbType.VarChar, 7);
            param1.Value = prs_nmb_lb1.Text.Trim();
            SqlParameter param2;
            param2 = command1.Parameters.Add("@month_pen", SqlDbType.Int);
            param2.Value = sMonth;
            SqlParameter param3;
            param3 = command1.Parameters.Add("@year_int", SqlDbType.Int);
            param3.Value = syear;
            /////////////////////////
            command1.Parameters.Add("@days_out", SqlDbType.Int);
            command1.Parameters["@days_out"].Direction = ParameterDirection.Output;
            command1.Parameters.Add("@message_out", SqlDbType.VarChar,50);
            command1.Parameters ["@message_out"].Direction = ParameterDirection.Output;
            command1.Parameters.Add("@dateess_out", SqlDbType.Date);
            command1.Parameters["@dateess_out"].Direction = ParameterDirection.Output;
            ///////////////////////////   
            connection.Open();
            command1.CommandType = CommandType.StoredProcedure;
            command1.ExecuteNonQuery();               
            days_penthwork_tx.Text = Convert.ToString(command1.Parameters["@days_out"].Value);
            message_tx.Text = Convert.ToString(command1.Parameters["@message_out"].Value);
            ///the above parameter contains rows with dates
            Label12.Text = Label12.Text + Convert.ToString(command1.Parameters["@dateess_out"].Value);          

            connection.Close();//close connection 
        }
        
        return "success";
    }
    catch (Exception e)
    {
        return e.ToString();
    }
}

我的 SQL Server 存储过程:

the results

以及c#运行代码时的查询

declare @p4 int
set @p4 = 3

declare @p5 varchar(50)
set @p5 = 'some text'

declare @p6 date
set @p6 = NULL

exec penthhmera_proc @prs_nmb_pen = '274484',
                     @month_pen = 1,
                     @year_int = 2021,
                     @days_out = @p4 output,
                     @message_out = @p5 output,
                     @dateess_out = @p6 output

select @p4, @p5, @p6

我认为这样@p6 总是为空。

最后,我想将第二个记录集中的所有值加载到 Gridview 或类似表格的位置,以便在我的网络表单中显示。

有什么想法吗?

提前致谢

【问题讨论】:

  • 你不应该使用输出参数。看到这个link
  • 这个问题是关于 c# 和 ADO.net 或实体框架或 asp 请不要使用 sql server 标签
  • 你必须发布你的存储过程脚本。这是最重要的。
  • 这个问题是针对 asp.net c# 的。我正在构建一个网络表单。
  • 输出参数有什么问题?他们肯定会返回错误的结果吗? @MahdiRahimi 这是一个带有 SQL Server 的 C# 问题,而 ASP 与它无关。为什么他不应该在这里使用输出参数?对于单个值,这是非常标准的

标签: asp.net sql-server stored-procedures


【解决方案1】:

ExecuteReader 就是答案。谢谢查理脸。

connection.Open();
            command1.CommandType = CommandType.StoredProcedure;

            SqlDataReader dr = command1.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    //some code
                }
                dr.NextResult();
                while (dr.Read())
                {
                   //some code
                }
            }
            else
            {
                Console.WriteLine("No data found.");
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    相关资源
    最近更新 更多