0% found this document useful (0 votes)
26 views2 pages

D

This document contains code for a Windows Forms application written in Visual Basic .NET. The code allows a user to select a file using an OpenFileDialog, reads the file contents into a byte array, and inserts the file data into a database table. It defines event handlers for the OpenFileDialog and a button click. The button click handler calls a SaveDocumentToDatabase function that opens a MySQL connection, inserts the file data into a table using a parameterized query, and handles exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

D

This document contains code for a Windows Forms application written in Visual Basic .NET. The code allows a user to select a file using an OpenFileDialog, reads the file contents into a byte array, and inserts the file data into a database table. It defines event handlers for the OpenFileDialog and a button click. The button click handler calls a SaveDocumentToDatabase function that opens a MySQL connection, inserts the file data into a table using a parameterized query, and handles exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Imports System.Windows.Forms.VisualStyles.

VisualStyleElement
Imports MySql.Data.MySqlClient
Imports System.IO

Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub OpenFileDialog1_FileOk(sender As Object, e As


System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles


Button1.Click
Dim openFileDialog As New OpenFileDialog()

' Set the title and initial directory (optional).


openFileDialog.Title = "Select a Document"
openFileDialog.InitialDirectory = "C:\Users\YourUsername\Documents"

' Set the file filter to specify the types of files you want to allow.
openFileDialog.Filter = "All Files|*.*|Text Files|*.txt|PDF Files|*.pdf"

' Show the OpenFileDialog.


If openFileDialog.ShowDialog() = DialogResult.OK Then
' Get the selected file's path.
Dim selectedFilePath As String = openFileDialog.FileName

' Process the selected file, for example, display the file path in a
TextBox.
SaveDocumentToDatabase(selectedFilePath)
End If
End Sub
' Function to save the selected document to the database
Private Sub SaveDocumentToDatabase(filePath As String)
' Read the document content into a byte array
Dim documentContent As Byte() = File.ReadAllBytes(filePath)

' Database connection string


Dim connectionString As String =
"Server=localhost;Database=VRM;User=root;Password=Damdam03;"

' SQL INSERT statement with parameter


Dim insertQuery As String = "INSERT INTO univ_vehicle (mvir) VALUES
(@content)"

Try
Using connection As MySqlConnection = New
MySqlConnection(connectionString)
connection.Open()

Using command As MySqlCommand = New MySqlCommand(insertQuery,


connection)
' Add the document content as a parameter
command.Parameters.AddWithValue("@content", documentContent)

' Execute the INSERT statement


command.ExecuteNonQuery()
End Using

MessageBox.Show("Document saved to the database successfully.",


"Success")
End Using
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Error")
End Try
End Sub

End Class

You might also like