使用 SQL (ADO) 将 Excel 拉入 C# 时,如何排除空行而不排除空单元格?
我有一个如下查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或WHERE Coalesce(Col1,Col2,Col3,Col4) IS NOT NULL(oledb/excel没有合并功能)orWHERE Coalesce(Col1,Col2,Col3,Col4) IS NOT NULL(no coalesce function for oledb/excel)