How to check if a column with a given name exists in a datarow

允我心安 提交于 2020-01-03 13:50:11

问题


I want to insert a value from loop in datarow so before entering value in datarow, I want to check that a perticular column NAME exist in table or not.

Please tell me how can I check it. (vb.net preferred).


回答1:


I got the answer.and its working . its:

  If dr.Table.Columns.Contains("columnname") = True Then
   --your work---
  End If



回答2:


Try this

Dim dt As New DataTable
For Each dc As DataColumn In dt.Columns
    If dc.ColumnName = "" Then

    End If
Next



回答3:


try:

if dr.Table.Columns("nameColumn") == null then

 //....



回答4:


The shortest solution.

 If dr.Table.Columns.Contains("columnname") Then
     'your code here
 End If



回答5:


Here is another way to find out if a column exists:

If dataRow.Table.Columns("ColumnName") IsNot Nothing Then
    -- Your code if a column exists
End If

See this answer for further reference when this approach might be handier than the Contains("ColumnName") one.



来源:https://stackoverflow.com/questions/1984893/how-to-check-if-a-column-with-a-given-name-exists-in-a-datarow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!