System.Data.SqlClient.SqlException:无效的列名'conferID'

Est*_*r B 0 c# sql-server

我试图从数据库中检索会议的特定日期以显示在网页上.阅读ConferenceID,如下所示:

conferID = reader["ConferenceID"].ToString();
Run Code Online (Sandbox Code Playgroud)

我希望页面只显示ConferenceID ='3'的日期,但是当我从下面的代码中删除(ConferenceID = conferID)时,我也从ConferenceID ='1'和ConferenceID ='2'获取日期.但是,如果我将(ConferenceID = conferID)添加回代码,则页面会中断.如果我将这些代码放在SQL中来测试和更改(ConferenceID = conferID)到(ConferenceID ='3'),它就可以了.我现在迷路了.任何帮助表示赞赏.

SqlCommand GetDates = new SqlCommand(@"
WITH x AS (
             select MAX(ConferenceID) as ConferenceID, row_number() over(order by D.Dates) as SN, D.Dates 
             from Conference as T 
                  inner join master..spt_values as N 
                  on N.number between 0 and datediff(day, T.ConferenceBeginDate, T.ConferenceEndDate) 
                  cross apply (select dateadd(day, N.number, T.ConferenceBeginDate)) as D(Dates) 
                  where N.type ='P' AND (ConferenceID = conferID) group by ConferenceID, D.Dates
                  )
SELECT ConferenceID, SN, Dates
FROM x
WHERE SN <> (Select MAX(SN) from x) AND (ConferenceID = conferID)
GROUP BY ConferenceID, SN, Dates", conn);
Run Code Online (Sandbox Code Playgroud)

Saa*_*cky 5

您需要添加conferID作为Parameter.试试这个

SqlCommand GetDates = new SqlCommand(@"
WITH x AS (
             select MAX(ConferenceID) as ConferenceID, row_number() over(order by D.Dates) as SN, D.Dates 
             from Conference as T 
                  inner join master..spt_values as N 
                  on N.number between 0 and datediff(day, T.ConferenceBeginDate, T.ConferenceEndDate) 
                  cross apply (select dateadd(day, N.number, T.ConferenceBeginDate)) as D(Dates) 
                  where N.type ='P' AND (ConferenceID = @ConferenceID) group by ConferenceID, D.Dates
                  )
SELECT ConferenceID, SN, Dates
FROM x
WHERE SN <> (Select MAX(SN) from x) AND (ConferenceID = @ConferenceID)
GROUP BY ConferenceID, SN, Dates", conn);

GetDates.Parameters.Add(new SqlParameter("@ConferenceID", conferID));
Run Code Online (Sandbox Code Playgroud)