List:MySQL and .NET« Previous MessageNext Message »
From:Jorge Bastos Date:November 20 2004 3:29am
Subject:Re: Images and BLOB fields
View as plain text  
Barry, i'll show you my code, for once i'll help someone :D

(It's a vb example, that's my way of coding, of course each person has it's
own way to, nobory is equal or perfect)
To insert the image or other type file:

        Dim OpenFileDialog1 as new OpenFileDialog
        OpenFileDialog1.ShowDialog()
        Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open,
FileAccess.Read)
        Dim mbr As BinaryReader = New BinaryReader(st)
        Dim buffer(st.Length) As Byte
        mbr.Read(buffer, 0, CInt(st.Length))
        st.Close()
        MyCon = New MySqlConnection("your connection string here")
        MyCon.Open()
        sql = "insert into imagens values (1,'some foto
description',@buffer)"
        MyCmd = New MySqlCommand(sql, MyCon)
        MyCmd.Parameters.Add(New MySqlParameter("@buffer", buffer))
        MyCmd.ExecuteNonQuery()
        MyCon.Close()
        MyCmd.Dispose()

To retrieve the file from the db and put it in a picturebox:

            MyCon = New MySqlConnection("your connection string here")
            MyCon.Open()
            Dim str As String = "SELECT image_field FROM table_name WHERE
id=1"
            MyCmd = New MySqlCommand(str, MyCon)
            Dim b() As Byte
            b = MyCmd.ExecuteScalar()
            If (b.Length > 0) Then
                Dim stream As New MemoryStream(b, True)
                stream.Write(b, 0, b.Length)
                picturebox1.image= New Bitmap(stream)
                stream.Close()
            End If
            MyCon.Close()
            MyCmd.Dispose()


Maybe i wrote something wrong, it's 3.30 AM and i'm... sleepy
Tell me if you put it to work.
Cheers

Jorge Bastos



----- Original Message ----- 
From: "Barry Zubel" <barry@stripped>
To: <dotnet@stripped>
Sent: Friday, November 19, 2004 5:13 PM
Subject: Images and BLOB fields


Anyone actually have any working code for the .Net connector to pump an
image file into a BLOB field, and then return it again?

The VS.Net docs focus on SQL server (bah, obviously) and I'm wondering if
anyone else has tackled this, before I leap headlong into the fray and start
myself.

Ta!

B.


-- 
MySQL on .NET Mailing List
For list archives: http://lists.mysql.com/dotnet
To unsubscribe:
http://lists.mysql.com/dotnet?unsub=1


Thread
Images and BLOB fieldsBarry Zubel19 Nov
  • RE: Images and BLOB fieldsJordan Sparks19 Nov
  • Re: Images and BLOB fieldsJorge Bastos20 Nov
  • Re: Images and BLOB fieldsPuiu Hrenciuc22 Nov
    • RE: Images and BLOB fieldsJordan Sparks22 Nov
    • Re: Images and BLOB fieldsBen Reichelt22 Nov
    • Re: Images and BLOB fieldsPuiu Hrenciuc23 Nov
  • Re: Images and BLOB fieldsJorge Bastos22 Nov
  • Re: Images and BLOB fieldsPuiu Hrenciuc23 Nov
  • Re: Images and BLOB fieldsPuiu Hrenciuc23 Nov
RE: Images and BLOB fieldsBob Dankert22 Nov