How to convert a Byte array to a String

  • Thread starter Thread starter Jaime Stuardo
  • Start date Start date
J

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
 
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
 
Jaime Stuardo said:
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.
 
Back
Top