简体   繁体   中英

Stored procedure to return multiples rows to ASP.NET MVC application

I'm trying to return multiples row which contains values from the same user. I created a stored procedure that returns 3 outputs.

I need to retrieve all the rows values, not just the first one.

What I 'm returning are names and locations.

Stored procedure:

ALTER PROCEDURE [dbo].[GetCountries]
    @UserID int,
    @Name nvarchar(10) output,
    @Latitude float output,
    @Longitude float output
AS
BEGIN
    SET NOCOUNT ON;

    SET @Nombre = (SELECT p.Name
                   FROM Places AS p 
                   INNER JOIN Places_Details AS pd ON p.Name = pd.Name 
                   WHERE p.Id_User = @UserID)

    SET @Latitude = (SELECT pd.Latitude 
                     FROM Places AS p 
                     INNER JOIN Places_Details AS pd ON p.Name = pd.Name
                     WHERE  p.Id_User = @UserID)

    --set @Longitude = (SELECT pd.Longitude
    --              FROM Places as p INNER JOIN Places_Details as pd ON p.Name = pd.Name WHERE p.Id_User = @UserID)

    SELECT @Name, @Latitude, @Longitude
END

And in the ASP.NET MVC:

public List<PlacesModel> getMyPlaces()
{
        PlacesList = new List<PlacesModel>();

        try
        {
            using (SqlConnection sqlConnection = DbConnection.OpenConnection())
            {
                using (SqlCommand sqlCommand = new SqlCommand("GetCountries", sqlConnection))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    SqlCommandBuilder.DeriveParameters(sqlCommand);

                    sqlCommand.Parameters["@UserID"].Value = 415;//UserRepository.getInstance().getUserId();
                    sqlCommand.Parameters["@Name"].Value = "";
                    sqlCommand.Parameters["@Latitude"].Value = 415;
                    sqlCommand.Parameters["@Longitude"].Value = 415;

                   sqlCommand.ExecuteNonQuery();

                    string name = (string) sqlCommand.Parameters["@Nombre"].Value;
                    double lat= (double)sqlCommand.Parameters["@Latitude"].Value;
                    double lon = (double)sqlCommand.Parameters["@Longitude"].Value;

                    if(!name.Equals("") & lat > 0 && long> 0)
                    {
                        PlacesModel placesm = new LugaresModel();
                        placesm.Place = name;
                        placesm.Latitude = Convert.ToString(lat);
                        placesm.Longitude = Convert.ToString(lon);
                        PlacesList.Add(placesm);
                    }
                }

                sqlConnection.Close();
            }
        }
        catch (Exception e)
        {
            e.GetBaseException();
        }

        return PlacesList;
    }
}

I'm getting this error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Any solution? Thanks

One of your queries is returning more than one row. If you run the Stored Procedure with that ID without using ASP.NET, just straight through SQL Server Management Studio (or Visual Studio Data Tools) you will probably see the same error. I suspect your Places_Details has more than one row for a given Id.

Looking at what you're doing, it seems your query should really be something like the following.

SELECT p.Name, pd.Latitude, pd.Longitude
FROM Places as p INNER JOIN Places_Details as pd ON p.Name = pd.Name
WHERE p.Id_User = @UserID

This will return the structure you're looking for, where you have a LAT and LONG for the user. You'll remove your error and find that you have multiple rows getting returned for the same user Id. You'll have a denormalized table which you can then use a Linq GroupBy in C# to group results (or loop over results and handle the one to many items).

the only other option is to have three procedures, one which returns the person, one the latitude and one the longitude. But I think you'll find getting all the data back at once and flattening it yourself will probably be faster.

The only other option I know of is to use something like Dapper ORM, where you can chain three stored procedure calls together so they are all returned in on connection, and it will map a single entity to multiple rows. But that's certainly outside this questions.

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