Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am working on a project for a refresher course I am doing in VB (It has been a long time since I touched it). The scope is VB.NET based employee information system using MySQL database.

I have managed to complete everything except for one step - File save (PDF) to the MySQL DB and ability to download the file. This I have never done before.


This is the MySQL DB Connect Method I Re-Use

VB
Public Sub DBConnect()
        Dim DatabaseName As String = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\db", "4", Nothing)
        Dim server As String = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\db", "1", Nothing)
        Dim userName As String = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\db", "2", Nothing)
        Dim password As String = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\db", "3", Nothing)

        If Not sConnection Is Nothing Then sConnection.Close()
        sConnection.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
        Try
            sConnection.Open()

        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox("ERROR 1001")

        End Try

    End Sub



Inserting a User Details into the DB
VB
<pre>
Public Sub InsertUser()
        DBConnect()

        Dim sqlQuery As String = "INSERT INTO db_table (firstname, surname, age, dept) VALUES('" & fname_box.Text & "','" & sname_box.Text & "','" & age_box.Text & "','" & dept_box.Text & "')"

        Dim sqlCommand As New MySqlCommand

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = sConnection
            .ExecuteNonQuery()
        End With

        LoadUsers()
       

    End Sub



My Question

I am looking for any ideas/suggestions you might have to upload a PDF document to the MySQL DB either as part of the above process (OR) as a separate process.
The Project has a requirement to attach a signed consent form for the user in PDF format, hence the requirement.

Essentially:
1. Browse for File
2. Select File
3. Upload or Save to MySQL LONGBLOB or VARBINARY

4. Ability to download PDF from button click.

What I have tried:

I have reviewed a few different solutions on youtube and other websites but each time I attempt one of them, I generally run into issues (mainly with the way in which I call the DB connection) which would take another hour to type and explain :) Essentially I am just looking for advice from someone who has achieved this previously.

Thank you in advance.
Posted
Comments
Jochen Arndt 3-Mar-17 8:29am    
Not a solution to your question but a suggestion:

Store the PDF as normal files within a dedicated directory and store the path to the files in the database.
This is a common method for large binary data that are already present as file (images, PDF).

Then handling the file pathes is just like for any other "normal" text records.
BoffinIE 3-Mar-17 8:33am    
Hey Jochen,

Thanks for your response buddy. If only I could do that :)
The project stipulates we must save to the MySQL DB, but yes I fully agree with you, I was considering using WMI to create a DIR using something like (System.IO.Directory.Create("\\server\$user\pdf")) and impersonate using the NTCurrentUser but alas that would be too easy :D

They want me to suffer badly on this project I think - But thank you so much for the suggestion! Appreciated
BoffinIE 4-Mar-17 8:31am    
Hey Richard,

Thank you for the tip - That is absolutely crazy... (yes I am a tech dinosaur) - Makes perfect sense, Ill go correct that and hopefully will get some extra points for it :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900