【问题标题】:How should I get the values from the select query of the stored procedure in c#我应该如何从 C# 中存储过程的选择查询中获取值
【发布时间】:2020-09-19 11:02:29
【问题描述】:

我想要选择查询中的日期和名称,如果我按正常查询运行,我会得到结果,但是当我尝试在 C# 中获取结果时,我得到的只是count=0。谁能告诉我我做错了什么?

这里是 C# 代码

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

这里是存储过程:

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

【问题讨论】:

  • 次要注意:这里不需要临时表;只需从源表中选择?

标签: c# asp.net sql-server


【解决方案1】:

您正在返回刚刚设置为 new List&lt;CertificationSummary&gt;()lastAccessedResult。此列表没有项目,因此计数为 0。

使用ExecuteReader 代替ExecuteNonQuery,然后您可以读取返回的数据并将它们存储到您的lastAccessedResult 列表中。

阅读here了解更多信息。

【讨论】:

    【解决方案2】:

    ExecuteNonQuery 不会返回结果,并且只应在您不希望返回行时使用。这对于 UPDATE 语句很常见。

    由于您对读取存储过程返回的行感兴趣,请使用 ExecuteReader,例如 var reader = cmd.ExecuteReader();

    更多信息请看这里: https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldatareader?view=dotnet-plat-ext-3.1

    【讨论】:

      【解决方案3】:

      您正在使用ExecuteNonQuery,它会丢弃查询中的所有网格。您需要使用ExecuteReader 来使用网格,但这很麻烦和仪式——API 很冗长。坦率地说,我会推荐一个像“Dapper”这样的工具(在 NuGet 上免费提供),然后就变成了

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

      【讨论】:

        猜你喜欢
        • 2021-07-23
        • 1970-01-01
        • 2017-06-10
        • 2014-06-17
        • 1970-01-01
        • 1970-01-01
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多