我正在创建一个从postgres DB中检索数据的Web服务。
这是我的代码:
Dim sql As String
Dim recordSet As Odbc.OdbcDataReader
Dim command As Odbc.OdbcCommand
Dim ret As New DataOutput
Dim dataLst As New List(Of Data)
Dim element As Dati
sql = "SELECT * from demo.table"
command = New Odbc.OdbcCommand(sql, odbcConn)
Try
recordSet = command.ExecuteReader
While recordSet.Read()
element = New Data
element.id = recordSet(0)
element.name = recordSet(1)
element.description = recordSet(2)
element.address = recordSet(3)
dataLst.Add(element)
End While
ret.arrDati = dataLst
这只显示行值,但我也需要列名。 我怎么能这样做?
答案 0 :(得分:2)
您可以使用GetName
方法访问列名:
Dim sql As String
Dim recordSet As Odbc.OdbcDataReader
Dim command As Odbc.OdbcCommand
Dim ret As New DataOutput
Dim dataLst As New List(Of Data)
Dim element As Dati
sql = "SELECT * from demo.table"
command = New Odbc.OdbcCommand(sql, odbcConn)
Try
recordSet = command.ExecuteReader
While recordSet.Read()
element = New Data
element.id = recordSet(0) 'column name: recordSet.GetName(0)
element.name = recordSet(1) 'column name: recordSet.GetName(1)
element.description = recordSet(2) 'column name: recordSet.GetName(2)
element.address = recordSet(3) 'column name: recordSet.GetName(3)
dataLst.Add(element)
End While
ret.arrDati = dataLst