0

这是一个非常简单的问题,但我找不到解决方案(甚至不确定要在这里谷歌搜索什么问题)。

这是我创建命令的代码:

var targetStartDate = "October 2014";//ConfigurationManager.AppSettings["StartDateString"].ToString(CultureInfo.InvariantCulture);
var acceptedStatusCodes = 4;//ConfigurationManager.AppSettings["ImportStatusIds"].ToString(CultureInfo.InvariantCulture);
using (var connection = new SqlConnection(connString))
{
    using (var cmd = connection.CreateCommand())
    {
        cmd.CommandText = @"
                            SELECT
                            ai.FirstName, 
                            ai.LastName, 
                            ai.Email, 
                            ai.SSN, 
                            ai.Phone,
                            ai.Program, 
                            r.FirstName + ' ' + r.LastName AS RepName, 
                            d.Name AS StartDateName, 
                            s.CvId AS CvSySchoolStatusId,
                            ai.CampusId as SyStudentId
                            FROM     
                            dbo.aspnet_Users AS u LEFT OUTER JOIN
                            dbo.ApplicantInfo AS ai ON u.UserId = ai.UserId LEFT OUTER JOIN
                            dbo.AdmissionReps AS r ON ai.AdmissionsRepId = r.RepId LEFT OUTER JOIN
                            dbo.ProgramStartDate AS d ON ai.StartDateId = d.CvStartDateId LEFT OUTER JOIN
                            dbo.CvSySchoolStatus AS s ON ai.CvSySchoolStatusId = s.CvId
                            where d.Name = '@StartDate'
                            and CvSySchoolStatusId in (@Statuses)";
        cmd.Parameters.AddWithValue("@StartDate", SqlDbType.VarChar).Value = (object) targetStartDate;
        cmd.Parameters.AddWithValue("@Statuses", SqlDbType.Int).Value = (object) acceptedStatusCodes;
    ...
    }
....
}

读者可以很好地发送命令,我可以通过 SQL 分析器看到它:

exec sp_executesql N'
SELECT
ai.FirstName, 
ai.LastName, 
ai.Email, 
ai.SSN, 
ai.Phone,
ai.Program, 
r.FirstName + '' '' + r.LastName AS RepName, 
d.Name AS StartDateName, 
s.CvId AS CvSySchoolStatusId,
ai.CampusId as SyStudentId
FROM     
dbo.aspnet_Users AS u LEFT OUTER JOIN
dbo.ApplicantInfo AS ai ON u.UserId = ai.UserId LEFT OUTER JOIN
dbo.AdmissionReps AS r ON ai.AdmissionsRepId = r.RepId LEFT OUTER JOIN
dbo.ProgramStartDate AS d ON ai.StartDateId = d.CvStartDateId LEFT OUTER JOIN
dbo.CvSySchoolStatus AS s ON ai.CvSySchoolStatusId = s.CvId
where d.Name = ''@StartDate''
and CvSySchoolStatusId in (@Statuses)'
,N'@StartDate nvarchar(12),@Statuses int'
,@StartDate=N'October 2014'
,@Statuses=4

这个查询给了我 0 个结果,但是如果我采用这个确切的查询并粘贴参数并只运行查询文本,我会得到预期的结果。知道我在哪里出错了吗?

4

2 回答 2

5

您需要在字符串查询中删除参数周围的单引号:

where d.Name = '@StartDate'

应该:

where d.Name = @StartDate

没有单一参数。

于 2013-11-11T17:36:28.720 回答
1

也许是 startdate 周围的额外单引号。我想你可能想删除那些单引号,然后再试一次。应该看起来像:

...其中 d.Name = @StartDate

于 2013-11-11T17:39:52.120 回答