简体   繁体   中英

mysql asp.net returning multiple rows resultset from stored procedure

This is my MySQL SP to populate a asp.net4.0 gridview having more than 1 results. But it is returning only 1 row. please help how can i populate gridview with all the results.thank you all.

    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;

This is ASP.net routine to populate the datatable :

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;
    }

Your procedure returns as many resultsets as you have rows in plants table by issuing SELECT for each row.

Now there is absolutely no need in using CURSOR in your procedure. You boil it down to just

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

Or just use this SELECT to populate your grid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM