【问题标题】:OleDB Select command not working with multiple OR statements (C#)OleDB Select 命令不适用于多个 OR 语句(C#)
【发布时间】:2019-05-16 13:44:06
【问题描述】:

我正在使用下面的 select 语句从 excel 中提取数据并将其放入数据表中。那里有一个我不想接的坏行,所以这就是这部分 [Plan] 'PLAN_BO' 但是它不适用于其他两个 OR 语句。 [Plan] 应该是一个日期时间列,但有一个额外的文本行。

[Plan] 'PLAN_BO' 或 [Plan] 的组合是 null 一起工作,但不是三个一起工作。

 comm.CommandText = "Select Category, [Project ID], Package, [Design ID], LLW, Bldg, School, [Project Description], [Doc Avail], [Plan] from [ca_rpt$] where [Plan] <> 'PLAN_BO' or [Plan] is null or [Plan] >= DATE() order by Category, [Project ID], Package, [Design ID], LLW";

有没有办法抵消第一行,因为那是坏行,但不是列标题。我在语句末尾尝试了 Offset 1 Row,但它不起作用。

编辑:当前状态

(ISDATE([Bid Open Plan]) and [Bid Open Plan] >= DATE()) or [Bid Open Plan] 为空

[开标计划] >= DATE() 被忽略。

 using (OleDbConnection conn = new OleDbConnection())
            {
                System.Data.DataTable dt = new System.Data.DataTable();
                conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";" + "Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";
                using (OleDbCommand comm = new OleDbCommand())
                {
                    comm.CommandText = "Select Category, [Project ID], Package, [Design ID], LLW, Bldg, School, [Project Description], [Pkg#], [Doc Available], [Bid Open Plan] from(Select Category, [Project ID], Package, [Design ID], LLW, Bldg, School, [Project Description], [Pkg#], [Doc Available], [Bid Open Plan] from [ca_rpt$] where ISDATE([Bid Open Plan]) or [Bid Open Plan] is null) Where [Bid Open Plan] >= DATE() order by Mincat, Minse, [Pkg#], [Project ID], Package, [Design ID], LLW";
                    comm.Connection = conn;
                    using (OleDbDataAdapter da = new OleDbDataAdapter())
                    {
                        da.SelectCommand = comm;
                        da.Fill(dt);

【问题讨论】:

    标签: c# sql excel sqlcommand


    【解决方案1】:

    您可能需要检查列值是否为有效日期。您可以使用ISDATE 检查该值是否为有效日期并仅返回有效日期。

      Select Category, [Project ID], Package, [Design ID], LLW, Bldg, School, 
        [Project Description], [Doc Avail], [Bid Open Plan] 
    
        FROM(
              Select Category, [Project ID], Package, [Design ID], LLW, Bldg, School, [Project Description], [Doc Avail], [Bid Open Plan]
              from [ca_rpt$]
              where ISDATE([Bid Open Plan]) =1 or [Bid Open Plan] is null 
        )
        WHERE [Bid Open Plan]>=Date()
        order by Category, [Project ID], Package, [Design ID], LLW
    

    查看此链接Documentation on ISDATE()

    【讨论】:

    • 好的,我使用了 ISDATE([Plan]),谢谢,但我也有 [Plan] >= DATE() 条件并且它被忽略了。 (ISDATE([Bid Open Plan]) and [Bid Open Plan] >= DATE()) or [Bid Open Plan] 为空
    • 你想用 [plan]>=Date() 达到什么目的?
    • 我只需要日期尚未过去的记录。所以从今天开始加上任何带有空值的东西。
    • 您可能需要将字符串值转换为日期时间,然后将其与今天的日期进行比较。所以它会被转换([Bid Open Plan] as datetime)>=getdate()
    • 写一个外部查询来得到你需要的东西。更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2016-12-06
    • 2021-04-03
    • 1970-01-01
    • 2021-02-23
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多