我<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
用来自动将我Return
格式化为 json 格式的输出。
但是,我认为我做的比必要的多,因为我首先将 a 的内容转储DataSet
到 aDictionary
中,然后Return
将Dictionary
.
如果我在列上使用别名并想输出所有列,有没有办法像我一样简单Return
地使用? 如果没有,我怎样才能用尽可能少的行来做到这一点?DataSet
Dictionary
Dim conn As New SqlConnection
conn.ConnectionString = Module1.DBConn2
Dim sqlCommand = New SqlCommand("Select id, column1, column2... From table1", conn)
conn.Open()
Dim sqlDataset As DataSet = New DataSet()
Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
sqlDataAdapter.Fill(sqlDataset)
conn.Close()
Dim jsonDict(sqlDataset.Tables(0).Rows.Count - 1) As Dictionary(Of Object, Object)
Dim i As Integer = 0
For Each rs As DataRow In sqlDataset.Tables(0).Rows
jsonDict(i) = New Dictionary(Of Object, Object)
jsonDict(i).Add("id", rs.Field(Of Object)("id"))
jsonDict(i).Add("column1", rs.Field(Of Object)("column1"))
jsonDict(i).Add("column2", rs.Field(Of Object)("column2"))
...
i = i + 1
Next
Return jsonDict