ADO.NET DataRow - 检查列是否存在 [英] ADO.NET DataRow - check for column existence

查看:21
本文介绍了ADO.NET DataRow - 检查列是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查数据行中是否存在列?

How do I check for the existence of a column in a datarow?

我正在构建数据表来组织一些我已经从数据库中提取的数据.根据每一行中的数据类型,我需要创建一个具有不同列的数据表.然后,稍后,我想检查一下我正在查看的数据表是否有某个列.

I'm building datatables to organize some data that I've already pulled back from the database. Depending on the type of data in each row, I need to create a datatable with different columns. Then, later on, I want to check and see if the datatable I am looking at has a certain column.

我知道我可以捕获异常并以这种方式处理它,但我很好奇数据行对象上是否有一个属性或方法可以为我执行此操作?

I know I can catch the exception and handle it that way, but I'm curious if there is a property or method on the datarow object that will do this for me?

这是我通过捕获异常来实现的方法:

Here's how I can do it by catching the exception:

public static String CheckEmptyDataRowItem(DataRow row, String rowName, String nullValue)
{
    try
    {
        return row[rowName].ToString();
    }
    catch (System.ArgumentException)
    {
        return nullValue;
    }
}

推荐答案

你可以简单地这样检查:

You can simply check like this:

return row.Table.Columns.Contains(columnName);

这篇关于ADO.NET DataRow - 检查列是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆