【发布时间】: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