German SQL-Server abfragen mit JSON

mcGeorge

Member
Licensed User
Longtime User
Falls Interesse besteht, im folgenden das originale C# Skript in vb.net übersetzt.

B4X:
<%@ Page Language="vb"%>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    Protected Sub Page_Load(sender As Object, ec As EventArgs)
        Dim cn As New SqlConnection("Data Source=**server/instanz**;Initial Catalog=**datenbank**;User=**user**;password=**pwd**")
        Dim sr As New StreamReader(Request.InputStream, Encoding.UTF8)
        
        Response.ContentType = "text/plain"
        Dim c As String = Request.QueryString("query")
        If (c.Length = 0) Then
            c = sr.ReadToEnd()
        End If
        

        Try
            Dim cmd As New SqlCommand(c, cn)
            cn.Open()

            Dim rdr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            Dim list As New List(Of Dictionary(Of String, Object))
            
            While (rdr.Read())
                Dim d As New Dictionary(Of String, Object)(rdr.FieldCount())
                Dim i As Integer
                
                For i = 0 To (rdr.FieldCount() - 1)
                    d(rdr.GetName(i)) = rdr.GetValue(i)
                Next
                list.Add(d)
            End While
            
            Dim j As New JavaScriptSerializer()
            Response.Write(j.Serialize(list.ToArray()))
            
        Catch ex As Exception
            Response.TrySkipIisCustomErrors = True
            Response.StatusCode = 500
            Response.Write( "Error occurred. Query=" & c & vbCrLf & vbCrLf )
            Response.Write( ex.ToString() )
        End Try
        
        Response.End()         
    End Sub
</script>

Damit kommt ein Basic-Entwickler sicherlich besser zurecht.

mfg
Georg
 
Last edited:
Top