使用 SQL (ADO) 将 Excel 拉入 C# 时,如何排除空行而不排除空单元格?

发布于 2024-08-20 03:58:43 字数 698 浏览 5 评论 0原文

我有一个如下查询:

SELECT * FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Excel Driver (*.xls)};DBQ=D:\test.xls','SELECT * FROM Sheet1$]')

如果曾经编辑然后删除,这会返回全部为空的行。我想排除这些,但仍然包含具有良好数据但可能为空单元格的行。

我的第一反应是执行“WHERE 每列不为空”AND 操作,如下所示:

SELECT * FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Excel Driver (*.xls)};   DBQ=D:\test.xls', 'SELECT * FROM Sheet1$]')
WHERE 
(
  Col1 IS NOT NULL 
  AND Col2 IS NOT NULL 
  AND Col3 IS NOT NULL 
  AND Col4 IS NOT NULL 
)

这有效地消除了空行,但由于某种原因,也消除了 Col4 具有空条目的行。我尝试了在 WHERE 子句周围使用或不使用括号的情况。

有谁知道我可能做错了什么,或者取而代之的是,有一个可以用来实现相同结果的不同方法的建议吗?

目前正在使用 ADO 在 C# 中实现此功能,但我正在使用 Sql Server Management Studio 2008 测试该查询。

I have a query like the following:

SELECT * FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Excel Driver (*.xls)};DBQ=D:\test.xls','SELECT * FROM Sheet1$]')

This brings back rows that are all null if they were ever edited and then deleted. I want to exclude those, but still include rows that have good data but possible null cells.

My first instinct was to do a "WHERE each column IS NOT NULL" ANDed together, like so:

SELECT * FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Excel Driver (*.xls)};   DBQ=D:\test.xls', 'SELECT * FROM Sheet1$]')
WHERE 
(
  Col1 IS NOT NULL 
  AND Col2 IS NOT NULL 
  AND Col3 IS NOT NULL 
  AND Col4 IS NOT NULL 
)

This effectively eliminates the null rows, but for some reason, also eliminates a row where Col4 had a null entry. I tried this with and without parens around the WHERE clause.

Does anyone see what I might be doing wrong, or in lieu of that, have a suggestion of a different method I can use to achieve the same results?

This is currently being implemented in C# using ADO, but I am testing the query using Sql Server Management Studio 2008.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-08-27 03:58:43
WHERE 
(
  Col1 IS NOT NULL 
  OR Col2 IS NOT NULL 
  OR Col3 IS NOT NULL 
  OR Col4 IS NOT NULL 
)

WHERE Coalesce(Col1,Col2,Col3,Col4) IS NOT NULL(oledb/excel没有合并功能)

WHERE 
(
  Col1 IS NOT NULL 
  OR Col2 IS NOT NULL 
  OR Col3 IS NOT NULL 
  OR Col4 IS NOT NULL 
)

or

WHERE Coalesce(Col1,Col2,Col3,Col4) IS NOT NULL (no coalesce function for oledb/excel)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文