简体   繁体   中英

How should I get the values from the select query of the stored procedure in c#

I want the date and the name from the select query which if I run as normal query I get the results but i when I try to get the results in C# all I get is count=0 . Can anyone tell me what wrong am I doing?

Here is the C# code

private List<CertificationSummary> GetLastAccessData (string taskOwner)
{
    List<CertificationSummary> lastAccessedResult = new List<CertificationSummary>();

    string connectionString = SqlPlusHelper.GetConnectionStringByName("MetricRepositoryDefault");

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlParameter[] sqlParams = new SqlParameter[1];
        sqlParams[0] = new SqlParameter("@taskOwner", SqlDbType.NVarChar);
        sqlParams[0].Value = taskOwner;

        connection.Open();

        SqlCommand cmd = connection.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetLastAccessedCertificationData";

        cmd.Parameters.AddRange(sqlParams);

        cmd.ExecuteNonQuery();
    }

    return lastAccessedResult;
}

And here is the stored procedure:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetLastAccessedCertificationData]
    (@taskOwner nvarchar(255))
AS
BEGIN
    DECLARE @name nvarchar(100)
    DECLARE @lastAccessedDate [datetime] 

    SELECT @name = Name 
    FROM CertificationReviewCycles 
    INNER JOIN UserReviewCycleAccess ON CertificationReviewCycles.CertificationReviewCycleID = UserReviewCycleAccess.LastAccessedReviewCycleID 
    WHERE USERID = @taskOwner

    SELECT @lastAccessedDate = LastAccessedDate 
    FROM UserReviewCycleAccess 
    WHERE UserID = @taskOwner

    CREATE TABLE #tempTable
    (
         name [nvarchar](255) NULL, 
         [LastAccessedDate] [datetime] NULL,
    )

    INSERT INTO #tempTable VALUES (@name, @lastAccessedDate)

    SELECT TOP(1) name, LastAccessedDate 
    FROM #tempTable
END
GO

You are returning lastAccessedResult which is has just been set to new List<CertificationSummary>() . This list has no items, so it has a count of 0.

Use ExecuteReader instead of ExecuteNonQuery and you can then read the data returned and store them into your lastAccessedResult list.

Read here for more info.

ExecuteNonQuery will not return results, and should only be used when you don't expect rows back. This is common for UPDATE statements.

Since you're interested in reading the rows returned by the stored procedure, use ExecuteReader, eg var reader = cmd.ExecuteReader();

See here for more: https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldatareader?view=dotnet-plat-ext-3.1

You're using ExecuteNonQuery , which discards any grids from the query. You need to use ExecuteReader to consume grids, but it is a lot of mess and ceremony - the API is verbose. Frankly, I'd recommend a tool like "Dapper" (freely available on NuGet), then this becomes just

private List<CertificationSummary> GetLastAccessData (string taskOwner)
{
    string connectionString = SqlPlusHelper.GetConnectionStringByName("MetricRepositoryDefault");
    using var connection = new SqlConnection(connectionString);
    return connection.Query<CertificationSummary>(
        "GetLastAccessedCertificationData",
        new { taskOwner }, // <== parameters
        commandType: CommandType.StoredProcedure).AsList();
}

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