I use Visual studio 2008
I use MySQL 5.1
I have created een database (test) table (table1 ) with field (Images) Data Type mediumblob size

The maximum length of mediumblob is 16777215 (2^24 - 1) characters


I want to Check file size before save in MySQL table1.
I want to avoid getting an error message when the image is too large for the mediumblobb field

I now use the following code

Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand

Dim SQL As String

Dim FileSize As UInt32
Dim rawData() As Byte
Dim fs As FileStream

conn.ConnectionString = "server=127.0.0.1;" _
    & "uid=root;" _
    & "pwd=12345;" _
    & "database=test"

Try
    fs = New FileStream("c:\image.png", FileMode.Open, FileAccess.Read)
    FileSize = fs.Length
    
    rawData = New Byte(FileSize) {}
    fs.Read(rawData, 0, FileSize)
    fs.Close()
    
    conn.Open()
    
    SQL = "INSERT INTO file VALUES(NULL, ?FileName, ?FileSize, ?File)"
    
    cmd.Connection = conn
    cmd.CommandText = SQL
    cmd.Parameters.Add("?FileName", strFileName)
    cmd.Parameters.Add("?FileSize", FileSize)
    cmd.Parameters.Add("?File", rawData)
    
    cmd.ExecuteNonQuery()
    
    MessageBox.Show("File Inserted into database successfully!", _
    "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    
    conn.Close()
Catch ex As Exception
    MessageBox.Show("There was an error: " & ex.Message, "Error", _
        MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

Can anyone tell me how I can check file size before save in MySQL table1!

Debug.WriteLine(My.Computer.FileSystem.GetFileInfo("C:\image.png").Length)
'OR
Debug.WriteLine((New IO.FileInfo("C:\image.png")).Length)

Thanks, But now I have a very strange problem!!!

Sorry i use field (Images) Data Type BLOB

'Blob 65,535 bytes, 65KB

I have a image "children shoe.jpg", see appendix , its 45.056 bytesbytes great (smaller than BLOB field, I think)
.
First program check if image not to big and if oke ,than i want to save in a BLOB field, see code.

Now I get the following error: Data too long for column 'Foto1' at row 1

Dim file1 As String
        file1 = OpenFileDialog1.FileName
        Dim information As System.IO.FileInfo
        information = My.Computer.FileSystem.GetFileInfo(file1)
        MsgBox("The file's full name is " & information.FullName & ".")
        MsgBox("Last access time is " & information.LastAccessTime & ".")
        MsgBox("The length is " & information.Length & ".")
        If information.Length > 65535 Then
            MsgBox("The image to big " & information.Length & ".")
        Else
            MsgBox("The image to oke " & information.Length & ".")
       
            Dim Myms1 As New MemoryStream

            PictureBox1.Image.Save(Myms1, PictureBox1.Image.RawFormat)
          
            Dim arrImage1() As Byte = Myms1.GetBuffer

            Myms1.Close()


            myCommand1.Connection = myConn
            myCommand1.CommandText = "update artikelen set Fotonaam1='" & FotoName1 & "',Foto1=@foto1 "

            myCommand1.Parameters.Clear()
            myCommand1.Parameters.AddWithValue("@foto1", arrImage1)
            myCommand1.ExecuteNonQuery()


       End If

I debug the code is see by arrimage1 => length = 89280

I have three questions:

1.How is that possible?
2. Should I save the image differently?
3. What should I do so that it works properly?

Can someone please answer these questions!

Previsously thanks,

Andre

ok, im a bit confused which file you wanna save anyway. the one from the openfiledialog or the one from the picturebox?

but at all ... why not just doing

Dim file1 As String
		OpenFileDialog1.ShowDialog()
		file1 = OpenFileDialog1.FileName

		Dim buffer() As Byte = IO.File.ReadAllBytes(file1)
		If buffer.Length > 65535 Then
			MsgBox("The image to big " & Information.Length & ".")
			Return
		Else
			MsgBox("The image to oke " & Information.Length & ".")
		End If
		myCommand1.Connection = myConn
		myCommand1.CommandText = "update artikelen set Fotonaam1='" & FotoName1 & "',Foto1=@foto1 "

		myCommand1.Parameters.Clear()
		myCommand1.Parameters.AddWithValue("@foto1", buffer)
		myCommand1.ExecuteNonQuery()

I want to save image "children shoe.jpg" from the picturebox into BLOB field "foto1"

tried with your attached image. returns 44989 bytes. which is correct

Dim mem As New IO.MemoryStream With {.Position = 0}
	PictureBox1.Image.Save(mem, PictureBox1.Image.RawFormat)
	mem.Flush()
	If mem.Length > 65535 Then
		MsgBox("The image to big " & mem.Length & ".")
		Return
	Else
	        MsgBox("The image to oke " & mem.Length & ".")
	End If
	mem.Close()

Sorry I'm a type mistake!!!!.
Everything works now perfectly!

Right from the scratch, I guess there wasn't any problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.