Results 1 to 3 of 3

Thread: [RESOLVED] Improve reading binary from SQL server 2008

  1. #1

    Thread Starter
    Hyperactive Member Ivenesco's Avatar
    Join Date
    Sep 2007
    Location
    Poland, Lublin
    Posts
    325

    Resolved [RESOLVED] Improve reading binary from SQL server 2008

    Hello,
    I have this code, to read image (saved as varbinary(max) in database):
    Code:
        
    Private Sub ReadImage()
            Dim connection As New SqlConnection(strConnectionString)
            Dim command As New SqlCommand("SELECT [Image] FROM z1 WHERE x=1 AND y=1", connection)
            connection.Open()
            Dim byteTmp() As Byte = command.ExecuteScalar
            connection.Close()
            Dim strfn As String = Convert.ToString(DateTime.Now.ToFileTime())
            Dim fs As New IO.FileStream(strfn, IO.FileMode.CreateNew, IO.FileAccess.Write)
            fs.Write(byteTmp, 0, byteTmp.Length)
            fs.Flush()
            fs.Close()
            PictureBox1.Image = Image.FromFile(strfn)   
    End Sub
    But it isn't very convenient code. It reads bytes from table, save them to file, and finally open file as image.
    How can I read it directly to image, without saving as file?
    "Only two things are infinite; the universe and human stupidity, and I'm not sure about the former."
    Albert Einstein

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Improve reading binary from SQL server 2008

    Code:
            Dim s As New IO.MemoryStream(byteTemp)
            PictureBox1.Image = Image.FromStream(s)
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3

    Thread Starter
    Hyperactive Member Ivenesco's Avatar
    Join Date
    Sep 2007
    Location
    Poland, Lublin
    Posts
    325

    Re: Improve reading binary from SQL server 2008

    Thanks, it's what I was looking for ;-)
    "Only two things are infinite; the universe and human stupidity, and I'm not sure about the former."
    Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width