3 Replies - 2079 Views - Last Post: 28 October 2008 - 10:55 AM Rate Topic: -----

#1 amartires  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-October 08

BinaryReader to Decimal

Posted 28 October 2008 - 10:44 AM

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:


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?

Is This A Good Question/Topic? 0
  • +

Replies To: BinaryReader to Decimal

#2 amartires  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-October 08

Re: BinaryReader to Decimal

Posted 28 October 2008 - 10:46 AM

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:


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?
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: BinaryReader to Decimal

Posted 28 October 2008 - 10:51 AM

Please don create duplicate topic, not only is it not going to get you help any faster, it will often confuse people as they don't know which thread to reply to. Topics merged :)
Was This Post Helpful? 0
  • +
  • -

#4 amartires  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-October 08

Re: BinaryReader to Decimal

Posted 28 October 2008 - 10:55 AM

Sorry about that.

Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1