如何获取 DataReader 中列的序号

发布于 2024-12-17 15:31:56 字数 947 浏览 1 评论 0原文

如何查明 DataReader 的结果集中是否存在列?

我尝试:

int columnOrdinal = reader.GetOrdinal("LastName");
columnExists = (columnOrdinal < 0);

但是如果该列不存在,GetOrdinal 会引发异常。我的情况也不例外。事实恰恰相反。这是... 非凡的。


注意:与我的问题无关,但是,我想知道列是否存在的真正原因是因为我想获得序数位置列,如果该列不存在,则不会引发异常:

int columnOrdinal = reader.GetOrdinal("Lastname");

注意:与我的问题无关,但是,真正原因是我想知道某列是否存在,因为我想知道该列是否包含 null:

itIsNull = reader.IsDBNull(reader.GetOrdinal("Lastname"));

不幸的是IsDBNull 仅接受序数,GetOrdinal 抛出异常。所以我留下了:

if (ColumnExists(reader, "Lastname"))
{
   itIsNull = reader.IsDBNull(reader.GetOrdinal("Lastname"));
}
else
   itIsNull = false;

注意:与我的问题无关,但是,我想知道列是否存在的真正原因是因为有时该列不会出现在结果中设置,我不想抛出处理数据库结果的异常,因为它并不异常。

How can i find out if a column exists in a DataReader's results set?

i try:

int columnOrdinal = reader.GetOrdinal("LastName");
columnExists = (columnOrdinal < 0);

but GetOrdinal throws an exception if the column does not exist. My case is not exceptional. It's the opposite. It's...ceptional.


Note: Not related to my question but, the real reason i want to know if a column exists is because i want to get the ordinal position of a column, without throwing an exception if the column doesn't exist:

int columnOrdinal = reader.GetOrdinal("Lastname");

Note: Not related to my question but, the real reason i want to know if a column exists, because i want to know if the column contains null:

itIsNull = reader.IsDBNull(reader.GetOrdinal("Lastname"));

Unfortunately IsDBNull only takes an ordinal, and GetOrdinal throws an exception. So i'm left with:

if (ColumnExists(reader, "Lastname"))
{
   itIsNull = reader.IsDBNull(reader.GetOrdinal("Lastname"));
}
else
   itIsNull = false;

Note: Not related to my question but, the real reason i want to know if a column exists is because there will be times where the column will not be present in the results set, and i don't want to throw an exception processing database results, since it's not exceptional.

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

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

发布评论

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

评论(1

我早已燃尽 2024-12-24 15:31:56

由于 IDataReader 没有公开太多有用的信息,因此您可以做的事情是有限的。使用类似问题的答案中所示的循环

检查列名称在 SqlDataReader 对象中

您可以在处理的第一行中构建一个简单的字典,该字典以列名为键,序数作为值(如果您不关心序数,则构建一个 HashSet)值)。然后您可以使用 columnDictionary.ContainsKey("LastName") 作为测试。对于遇到的第一行,您只需构建一次字典,然后所有后续行都会很快。

但说实话,与数据库时间相比,在其他 stackoverflow 问题中使用原样解决方案所消耗的时间可能可以忽略不计。

编辑:此处的其他可能性:检查是否数据读取器中存在一列

There is a limit to what you can do since the IDataReader doesn't expose much that helps. Using the loop as shown in the answer to a similar question

Check for column name in a SqlDataReader object

You could, with the first row you process, build a simple dictionary that is keyed by column name with ordinals as values (or a HashSet if you don't care about the ordinal values). Then you can just use columnDictionary.ContainsKey("LastName") as your test. You would only build the dictionary once, for the first row encountered, then all the subsequent rows would be fast.

But to be honest, compared with database time, the time consumed by using as-is the solution in that other stackoverflow qeustion would probably be negligible.

Edit: additional possibilities here: Checking to see if a column exists in a data reader

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