ADO.NET的DataRow - 检查列的存在 [英] ADO.NET DataRow - check for column existence
问题描述
我如何检查一个数据行一列的存在?
我要建数据表来组织一些数据,我已经拉从数据库返回的。根据各行中的数据的类型,我需要创建具有不同列的数据表。然后,后来,我想检查,看看我在看表具有一定的列。
我知道我可以捕获该异常,并处理它的方式,但我很好奇,如果有一个属性或方法DataRow对象上,这将帮我这个忙?
下面就是我能做到这一点通过捕捉异常:
公共静态字符串CheckEmptyDataRowItem(DataRow中的行,字符串rowName,字符串NullValue属性)
{
尝试
{
回行[rowName]的ToString();
}
赶上(System.ArgumentException)
{
返回NullValue属性;
}
}
您可以简单地检查是这样的:
返回row.Table.Columns.Contains(COLUMNNAME);
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屋!