我正在尝试将 web api 添加到现有的 asp.net ASPX 站点。
我想要的只是它返回(以 JSON 或 XML 格式)我的查询结果 - 但是我收到错误:无法将“System.Data.DataTable”类型的对象转换为“System.Collections.Generic.IEnumerable”类型1 [系统.Web.UI.WebControls.Table]'。
...在代码的最后部分。
我在 API 控制器中的代码是:
Imports System.Web.Http
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Public Class ValuesController
Inherits ApiController
' GET /api/<controller>
Public Function GetValues() As IEnumerable(Of Table)
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=inv;Integrated Security=True")
Dim cmd As SqlCommand = New SqlCommand("select * from customers", con)
cmd.CommandType = CommandType.Text
Dim adapter As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As New DataTable
Using con
Try
con.Open()
adapter.Fill(dt)
'Check if any rows were returned
If dt.Rows.Count = 0 Then
'If no rows, return nothing
Return Nothing
Else
'Return the filled datatable
Return dt
End If
Catch ex As Exception
con.Close()
Throw ex
Finally
con.Close()
End Try
End Using
Return New String() {"value1", "value2"}
End Function