Home All Groups Group Topic Archive Search About

How to convert a Byte array to a String

Author
6 Apr 2005 5:29 AM
Jaime Stuardo
Hi all...

I'm trying to retrieve a SQLXML query using VB.NET. When I programmed in VB
6.0, I used Stream object to accomplish this which was trivial.

I cannot do the same thing in VB.NET. Here the most similar object is the
OleDbDataReader. I have the following method, ¿is it correct what I am
doing? By doing oRdr.GetBytes(0, 0, bBytes, 0, 1000) I'm limiting only to
1000 bytes. How can I know the length of the stream?

Public Function get_Tree(Optional ByVal sEstado As String = "") As String
        Dim oConn As New OleDbConnection(m_sConn)
        oConn.Open()
        Dim oCmd As New OleDbCommand("SP_GET_CATEGORY_TREE", oConn)
        Dim oParamEst As OleDbParameter = New OleDbParameter("@CAT_EST",
OleDbType.VarChar, Len(sEstado))
        oParamEst.Value = sEstado
        oCmd.Parameters.Add(oParamEst)
        oCmd.CommandType = CommandType.StoredProcedure
        Dim oRdr As OleDbDataReader = oCmd.ExecuteReader
        If oRdr.Read Then
            Dim bBytes(1000) As Byte
            oRdr.GetBytes(0, 0, bBytes, 0, 1000)
            Return ""     ' I NEED TO RETURN THE XML THAT IS RETURNED FROM
THE SQL SP (AS FOR XML AUTO)
        End If
    End Function

Any help will be greately appreciated,
Jaime

Author
6 Apr 2005 7:24 AM
Cor Ligthert
Jaime,

See this snippet.

\\\
   "DataBase=Northwind; Integrated Security=SSPI")
        Dim sqlstr As String = _
        String.Format("SELECT Photo FROM Employees WHERE (EmployeeID =
{0})", _
        CInt(Session.Item("img")))
        Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
        conn.Open()
        Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
        rdr.Read()
        Dim arrImage() As Byte = DirectCast(rdr.Item("Photo"), Byte())
   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Dim ms1 As New System.IO.MemoryStream(arrImage)
        'The one above is for normal purpose, however Northwind has a
strange format
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim ms1 As New System.IO.MemoryStream(arrImage, 78,
arrImage.Length - 78)
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim origimage As System.drawing.Image =
System.Drawing.Image.FromStream(ms1)
///
I hope this helps,

Cor
Author
6 Apr 2005 10:37 AM
Herfried K. Wagner [MVP]
"Jaime Stuardo" <jstua***@manquehue.net> schrieb:
> I'm trying to retrieve a SQLXML query using VB.NET. When I programmed in
> VB 6.0, I used Stream object to accomplish this which was trivial.
>
> I cannot do the same thing in VB.NET. Here the most similar object is the
> OleDbDataReader. I have the following method, ¿is it correct what I am
> doing? By doing oRdr.GetBytes(0, 0, bBytes, 0, 1000) I'm limiting only to
> 1000 bytes. How can I know the length of the stream?

I cannot give you an answer on the database stuff.  However, you can use
'System.Text.Encoding.<encoding name>.GetString' to convert a byte array to
a string.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>