Question : Store file in mysql using connector.net and blob field

Hi,
I am working on a project where I have to store pictures in a BLOB field in my MySql database.
I have been trying lots of different ways of storing the byte array I create as a blob in the database, but I always end up getting this error or similar:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'é³ä\" ól6¿ü_¿?¿Ï)\0 S 7~ RÌÁ ír¹D(óù\\¿mx®òMÓ´¼L§S¿ v/­ ¢hnfP7³¸£rÁÁ¿ËÞPì¿ lw' at line 1"

I've spent several hours trying out every example and code snippet I've found searching around but I always end up with the same problem.

My environment is:
VS 2010
MySql Server 5.0.67
mysql-connector-net-6.2.3

Database table I use:
CREATE TABLE  file (
  file_id smallint(5) unsigned NOT NULL auto_increment,
  file_name varchar(64) default NULL,
  file_size mediumint(8) unsigned default NULL,
  file mediumblob,
  PRIMARY KEY  (file_id)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

I have tried storing a file in the same table as I am trying to use from VB.net using the MySql QueryBrowser and that works, so we know that there is nothing wrong with the MySql Server.

I have attached the picture used when getting the error message above and the function I use exactly as they are.
Picture used:
 
Picture used when getting error above
310618
 


Code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
Private Sub WriteFileToDb(ByVal strFileName As String, ByVal strFilePath As String)
        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
        Dim kFileName As MySqlParameter = Nothing
        Dim kFileSize As MySqlParameter = Nothing
        Dim kFile As MySqlParameter = Nothing

        conn.ConnectionString = sConnString

        Try
            fs = New FileStream(strFilePath, 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

            kFileName = New MySqlParameter("@FileName", MySqlDbType.String)
            kFileName.Direction = ParameterDirection.Input
            kFileName.Value = strFileName

            kFileSize = New MySqlParameter("@FileSize", MySqlDbType.Int32)
            kFileSize.Direction = ParameterDirection.Input
            kFileSize.Value = FileSize

            kFile = New MySqlParameter("@File", MySqlDbType.MediumBlob)
            kFile.Direction = ParameterDirection.Input
            kFile.Value = rawData

            cmd.Parameters.Add(kFileName)
            cmd.Parameters.Add(kFileSize)
            cmd.Parameters.Add(kFile)

            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

    End Sub

Has anyone got some bright ideas on why I have these problems?

Very greatful for some help... :-)

Answer : Store file in mysql using connector.net and blob field

I made a test where I changed the BLOB field to LONGTEXT and instead of trying to store the byte array, I convert the array to a Base64 string. This worked for some strange reason and with that I can both store and read-out files from the database.

However, I dont know if encoding it to base64 is the best way of doing this and it feels more like a work-around than a good solution. I've attached my new code here and hopefully someone can give me a hint on why the BLOB doesnt work for me.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
Private Sub StoreFileInDb(ByVal strFileName As String, ByVal strFilePath As String)
        Dim FileSize As UInt32
        Dim rawData() As Byte
        Dim fs As FileStream
        Dim connection As New MySqlConnection
        Dim strSql As String = "SELECT * FROM File"
        Dim ds As New DataSet("FileStore")
        Dim tempAP As New MySqlDataAdapter(strSql, connection)
        Dim objCommand As New MySqlCommandBuilder(tempAP)

        connection.ConnectionString = sConnString

        fs = New FileStream(strFilePath, FileMode.Open, FileAccess.Read)
        FileSize = fs.Length - 1

        rawData = New Byte(FileSize) {}
        fs.Read(rawData, 0, FileSize)
        fs.Close()

        tempAP.Fill(ds, "File")
        Try
            connection.Open()
            Dim objNewRow As DataRow = ds.Tables("File").NewRow()
            objNewRow("file_name") = strFileName
            objNewRow("file_size") = rawData.Length
            objNewRow("file") = Convert.ToBase64String(rawData)
            ds.Tables("File").Rows.Add(objNewRow)
            tempAP.Update(ds, "File")
        Catch ex As Exception
            Dim trace As New System.Diagnostics.StackTrace(ex, True) : WriteLogFile(System.Reflection.MethodBase.GetCurrentMethod.Name & " - Undermetod: " & trace.GetFrame(1).GetMethod().Name, "Error, rad: " & trace.GetFrame(1).GetFileLineNumber() & " Kolumn: " & trace.GetFrame(1).GetFileColumnNumber(), ex.Message)
        Finally
            connection.Close()
        End Try
    End Sub
Random Solutions  
 
programming4us programming4us