I'm creating an tool called dbfiddle, and I'm having trouble with displaying a few SQL datatypes as readable text. One in particular is hierarchyid
.
I'm using a JSON API to the database backends, and SQL Server 2014/2016 use System.Web.Script.Serialization.JavaScriptSerializer
to convert the results to JSON.
For the decimal
this works:
result.data(i).Add(If(reader.IsDBNull(i),Nothing,reader.GetSqlDecimal(i).ToString()))
For varbinary
, this is fine:
result.data(i).Add(If(reader.IsDBNull(i),Nothing,"0x" & (New System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary(reader(i))).ToString()))
For most other data types, a simple ToString
is fine:
result.data(i).Add(If(reader.IsDBNull(i),Nothing,reader(i).ToString()))
but I can't figure out what the equivalent code for hierarchyid
should be. How do I convert to a string? I can't alter the query in any way as dbfiddle does not parse the input SQL.
Here is a simplified version of the relevant portion of my code:
Dim sr = New StreamReader(Request.InputStream)
Dim ser = New System.Web.Script.Serialization.JavaScriptSerializer()
ser.MaxJsonLength = Int32.MaxValue
Dim queries = ser.Deserialize(Of List(Of String))(sr.ReadToEnd())
sr.Close()
Dim query = queries(0)
Dim connection As SqlConnection
Dim command As SqlCommand
…
command = connection.CreateCommand
command.CommandText = query
Dim reader = command.ExecuteReader()
Dim result = New List(Of List(Of Object))
For i As Integer = 0 To reader.FieldCount-1
result.Add(New List(Of Object))
Next
While reader.Read()
For i As Integer = 0 To reader.FieldCount-1
result(i).Add(reader(i).ToString())
Next
End While
reader.Close()
connection.Close()
…
Response.Write(ser.Serialize(result))