简体   繁体   中英

Check if a column exists in vb.net, ADO.net

I have a windows service which fetches data from various datasources and build a XML file and send it to a webservice. For example first I will get customer details from Oracle Database and build the XML file and then the SQL Server database and build the customer details XML. I am planning to use the same function below to build the customer object irrespective of what the datasource is. But dr("age") column is not available in SQLserver datbase How can I check if a column exists or not. I want to do something like

if dr("age") exists then
.age=dr("age")
else
.age=0
end if

I have a function as shown below which populates the customer Object. As I am

Public Shared Function Retrieve() As List(Of Customer)
Dim dt As DataTable = Dac.ExecuteDataTable( _ 
  "CustomerRetrieveAll", nothing)
Dim customerList As New List(Of Customer)
For Each dr As DataRow In dt.Rows   
 customerList.Add(New Customer With _
                 {.CustomerId = CType(dr("CustomerID"), Integer), _                  .LastName = dr("LastName").ToString, _   
              .age = dr("age").ToString, _                  
.FirstName = dr("FirstName").ToString})  
Next
Return customerList
End Function

您可以检查DataTable.Columns集合以确定是否存在列。

you can use the method contains of the table that ows the datarow. like:

If row.Table.Columns.Contains("your table name") Then ....

JDVA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM