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
|