【问题标题】:System.Data.SqlClient.SqlException: Invalid column name 'conferID'System.Data.SqlClient.SqlException:列名“conferID”无效
【发布时间】:2015-05-05 15:38:39
【问题描述】:

我正在尝试从数据库中检索会议的特定日期以显示在网页上。读取会议 ID,如下所示:

conferID = reader["ConferenceID"].ToString();

我希望页面仅显示 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);

【问题讨论】:

    标签: c# sql-server


    【解决方案1】:

    您需要将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));
    

    【讨论】:

    • 添加参数是对的,但我认为最后一条语句 `GetDates.Parameter...("ConferenceID") 应该是 ("@ConferenceID")。我将您的答案标记为有用。谢谢!
    【解决方案2】:

    请使用sql参数。

    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);
    
    GetDates.Parameters.AddWithValue("@conferId", conferId);
    

    这里的问题是您试图引用一个不存在的字段。我假设您正在尝试引用一个可用的字符串,因此您必须将其添加为命名参数,然后通过 this 进行分配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-14
      • 2019-11-02
      • 2016-04-22
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      相关资源
      最近更新 更多