varbinary to real

amartires

New member
Joined
Oct 28, 2008
Messages
1
Programming Experience
5-10
Hi, I have this situation:

I must store a Real in a Varbinary field in SQL, then I must Read that varbinary real into a real field into another DB.

As SQL 2005 does not suport convertion from varbinary to real or float (Although I can store them into a varbinary...) What is the best aproach for this problem?
I've tried to manipulate ir in TSQL but no success, I'm trying VB.NET, to do this also, here is my sample code:

VB.NET:
Dim con As SqlConnection = New SqlConnection("Server=MyServer;Database=master;Trusted_Connection=True;")
        Dim cmd As SqlCommand = New SqlCommand()
        Dim da As SqlDataAdapter = New SqlDataAdapter()
        Dim dt As DataTable = New DataTable()
        Dim ba As Byte()
        Dim val As Decimal
        Dim memStream As MemoryStream = New MemoryStream()
        Dim bReader As BinaryReader

        cmd.CommandText = "select convert(varbinary(max), cast(500 as real)) val"
        cmd.Connection = con

        da.SelectCommand = cmd

        Try
            con.Open()
            da.Fill(dt)
            ba = dt.Rows(0).Item(0)
            memStream.Write(ba, 0, ba.Length)
            Dim bfr(100) As Char

            bReader = New BinaryReader(memStream)
            memStream.Seek(0, SeekOrigin.Begin)
            Dim testArray As Byte() = {0, 0, 0, 0}
            Dim count As Integer = bReader.Read(testArray, 0, 3)
            val = bReader.ReadDecimal()

            MessageBox.Show(byteArrayToString(ba))

        Catch ex As Exception
            MessageBox.Show("Erro: " & vbCrLf & ex.Message)
        Finally
            con.Close()
        End Try

When I run this I get an "Unable to read beyond the end of the stream". Any clues on how to solve this?
 
Top Bottom