如何确定 VB.Net DataRow 中是否存在列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/178712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:15:38  来源:igfitidea点击:

How do I find out if a column exists in a VB.Net DataRow

.netvb.netado.netdatasetdatarow

提问by Bryan Anderson

I am reading an XML file into a DataSet and need to get the data out of the DataSet. Since it is a user-editable config file the fields may or may not be there. To handle missing fields well I'd like to make sure each column in the DataRow exists and is not DBNull.

我正在将 XML 文件读入 DataSet 并需要从 DataSet 中获取数据。由于它是用户可编辑的配置文件,因此这些字段可能存在也可能不存在。为了很好地处理缺失的字段,我想确保 DataRow 中的每一列都存在并且不是 DBNull。

I already check for DBNull but I don't know how to make sure the column exists without having it throw an exception or using a function that loops over all the column names. What is the best method to do this?

我已经检查了 DBNull 但我不知道如何确保该列存在而不让它抛出异常或使用循环所有列名的函数。执行此操作的最佳方法是什么?

回答by John Chuckran

DataRow's are nice in the way that they have their underlying table linked to them. With the underlying table you can verify that a specific row has a specific column in it.

DataRow 的优点在于它们将底层表链接到它们。使用基础表,您可以验证特定行中是否包含特定列。

    If DataRow.Table.Columns.Contains("column") Then
        MsgBox("YAY")
    End If

回答by Phillip Wells

You can use DataSet.Tables(0).Columns.Contains(name)to check whether the DataTablecontains a column with a particular name.

您可以使用DataSet.Tables(0).Columns.Contains(name)来检查 是否DataTable包含具有特定名称的列。

回答by Alexander Abakumov

Another way to find out if a column exists is to check for Nothingthe value returned from the Columnscollection indexer when passing the column name to it:

另一种确定列是否存在的方法是在将列名传递给它时检查NothingColumns集合索引器返回的值:

If dataRow.Table.Columns("ColumnName") IsNot Nothing Then
    MsgBox("YAY")
End If

This approach might be preferred over the one that uses the Contains("ColumnName")method when the following code will subsequently need to get that DataColumnfor further usage. For example, you may want to know which type has a value stored in the column:

Contains("ColumnName")当以下代码随后需要获取该方法DataColumn以供进一步使用时,此方法可能优于使用该方法的方法。例如,您可能想知道列中存储了哪种类型的值:

Dim column = DataRow.Table.Columns("ColumnName")
If column IsNot Nothing Then
    Dim type = column.DataType
End If

In this case this approach saves you a call to the Contains("ColumnName")at the same time making your code a bit cleaner.

在这种情况下,这种方法可以节省您对 的调用,Contains("ColumnName")同时使您的代码更简洁一些。

回答by Anders

You can encapsulate your block of code with a try ... catch statement, and when you run your code, if the column doesn't exist it will throw an exception. You can then figure out what specific exception it throws and have it handle that specific exception in a different way if you so desire, such as returning "Column Not Found".

您可以使用 try ... catch 语句封装您的代码块,并且当您运行您的代码时,如果该列不存在,它将抛出异常。然后,您可以找出它抛出的特定异常,如果您愿意,可以让它以不同的方式处理该特定异常,例如返回“未找到列”。