我创建了一个函数,它基本上使我能够以与 Facebook 的 FQL 查询方法类似的方法从我的数据库中输出我需要的任何内容作为 JSON 文本结果。
我还可以使用相同的函数将输出作为字典或 List of(Dictionary) 与 VB 函数一起用于其他目的。
Public Shared Function getDataList(sql As String, output As String, useDb As String, labelName As String, fileName As String)
Dim dataDetail
'' This function will return the content of a query as either a key/value dictionary, or a key(key/value) result for formating
'' as JSON for JS handling or a Dictionary to use with server side codes
If labelName <> "" Then
dataDetail = New List(Of Generic.Dictionary(Of String, String))
Else
dataDetail = New Generic.Dictionary(Of String, Generic.Dictionary(Of String, String))
End If
Dim connection As OdbcConnection = Common.getConnection(useDb)
Try
connection.Open()
Dim connect As OdbcCommand = Common.createCommand(sql, connection, useDb)
Dim getData As New DataTable()
getData.Load(connect.ExecuteReader(CommandBehavior.CloseConnection))
For Each row In getData.Rows
Dim rowDetail As New Generic.Dictionary(Of String, String)
For Each column In getData.Columns
Dim theData = row(column)
If IsDBNull(theData) Then
theData = ""
End If
If LCase(output) = "json" Then
theData = HttpContext.Current.Server.HtmlDecode(theData)
End If
rowDetail.Add(column.ToString, theData)
Next
Next
connection.Close()
Catch theError As Exception
HttpContext.Current.Response.Write(theError)
Finally
connection.Dispose()
End Try
If output = "json" Then
Dim serializer As New JavaScriptSerializer
HttpContext.Current.Response.Write(serializer.Serialize(dataDetail))
ElseIf output = "dictionary" Then
Return dataDetail
End If
End Function
这很好用,我发现它对于快速从我的数据库中获取数据非常有用。
我唯一的问题是,当我将输出作为 JSON 文件获取时,我希望将存储在数据库中的任何内容作为编码 HTML 进行转换,以便在浏览器中很好地显示。
线
theData = HttpContext.Current.Server.HtmlDecode(theData)
打算这样做,并且确实......有点......但今天这个角色:
'
不是解码。
有任何想法吗?!