Return multiple recordsets from stored proc in C#

后端 未结 4 1021
时光取名叫无心
时光取名叫无心 2020-12-05 20:38

I am having to convert an ASP classic system to C#

I have a stored procedure that can return up to 7 recordsets (depending on the parameters passed in).

I ne

4条回答
  •  感动是毒
    2020-12-05 21:03

    you can check if dr has any more recordset ot no by using if (dr.NextResult())

    and then loop again with dr.read

    try this

    string connStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    SqlConnection Con = new SqlConnection(connStr);
    
    try
    {
        string str1 = "select productid,productname from products;select VendorFName from vendor";
        SqlCommand com = new SqlCommand(str1, Con);
    
        com.Connection.Open();
    
        SqlDataReader dr = com.ExecuteReader();
    
        DropDownList1.Items.Add("Select Product Id");
        DropDownList2.Items.Add("Select Vendor Name");
    
        while(dr.Read())
        {
            DropDownList1.Items.Add(dr.GetValue(0).ToString());
        }
    
        if (dr.NextResult())
        {
            while (dr.Read())
            {
                DropDownList2.Items.Add(dr.GetValue(0).ToString());
            }
        }
    }
    catch (Exception ex)
    {
    }
    finally
    {
        if (Con.State == ConnectionState.Open)
        {
            Con.Close();
        }
    }
    

提交回复
热议问题