mysql asp.net从存储过程返回多行结果集

时间:2013-09-16 08:13:23

标签: mysql asp.net gridview

这是我的MySQL SP,用于填充具有1个以上结果的asp.net4.0 gridview。 但它只返回1排。 请帮助我如何使用所有结果填充gridview。谢谢大家。

    CREATE DEFINER=`metro`@`localhost` PROCEDURE `p_get_plants_gridview`()
        LANGUAGE SQL
        NOT DETERMINISTIC
        MODIFIES SQL DATA
        SQL SECURITY DEFINER
        COMMENT 'procedure to populate plants gridview in plants.aspx page.'
    BEGIN
    DECLARE noMoreRow INT DEFAULT 0;
    DECLARE plantid         INT UNSIGNED;
    DECLARE plantname   VARCHAR(100);
    DECLARE plantcode   VARCHAR(10);
    DECLARE plantaddr1  VARCHAR(50);
    DECLARE plantaddr2  VARCHAR(50);
    DECLARE plantaddr3  VARCHAR(50) ;
    DECLARE O_PL_CURSOR  CURSOR FOR SELECT plant_id,
                                                        plant_name, 
                                                        plant_code,
                                                        plant_addr1,
                                                        plant_addr2,
                                                        plant_addr3
                                                FROM    plants;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET noMoreRow = 1;
    OPEN O_PL_CURSOR; 
    LOOPROWS: LOOP
          FETCH O_PL_CURSOR INTO plantid,plantname,plantcode,plantaddr1,plantaddr2,plantaddr3;
          IF noMoreRow  THEN

              LEAVE LOOPROWS;

          END IF;
          SELECT plantid,plantname,plantcode,plantaddr1,plantaddr2,plantaddr3;
        END LOOP;                                           
                  CLOSE O_PL_CURSOR;
    END;

这是填充数据表的ASP.net例程:

public DataTable GetPlants_Grid() 
   {
                MySqlConnection conn = new  
                                         MySqlConnection(Configuration.ConnectionInfo);
        DataTable dt = new DataTable();
        MySqlCommand cmd = new MySqlCommand("p_get_plants_gridview", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        conn.Open();
        dt.Clear();
    try
    {
        MySqlDataAdapter da = new MySqlDataAdapter();
        da.SelectCommand = cmd;
        da.Fill(dt);
    }
        catch (MySqlException ex) { string msg = ex.Message; }
        finally { conn.Close(); }
        return dt;
    }

1 个答案:

答案 0 :(得分:0)

通过为每一行发出plants,您的过程将返回与SELECT表中的行一样多的结果集。

现在绝对没有必要在您的程序中使用CURSOR。你把它归结为

CREATE PROCEDURE p_get_plants_gridview()
  SELECT plant_id,
         plant_name, 
         plant_code,
         plant_addr1,
         plant_addr2,
         plant_addr3
    FROM plants;

或者只使用此SELECT填充网格