0% found this document useful (0 votes)
26 views

Module 1

This code defines functions to connect to a MySQL database, execute queries, and handle the results. It establishes a connection string and command object for querying the "final_payroll" database on localhost. Functions are defined to read from the database, add/update/delete records, and reload a data table on form load by executing queries and handling the results.

Uploaded by

Bianca Bacurin
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Module 1

This code defines functions to connect to a MySQL database, execute queries, and handle the results. It establishes a connection string and command object for querying the "final_payroll" database on localhost. Functions are defined to read from the database, add/update/delete records, and reload a data table on form load by executing queries and handling the results.

Uploaded by

Bianca Bacurin
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Imports MySql.Data.

MySqlClient
Module Module1

Public Function strconnection() As MySqlConnection


Return New MySqlConnection("server = localhost; username = root; password =
; database = final_payroll; convert zero datetime = true")
End Function
Public strcon As MySqlConnection = strconnection()

Public result As String


Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public dt As New DataTable

Public conn As New MySqlConnection


Public read As MySqlDataReader
Public server = "'localhost'"
Public username = "'root'"
Public password = "''"
Dim database = "'final_payroll'"

Public strconn As String = "server = " & server & ";uid = " & username & ";
password = " & password & "; database = " & database & ""

Public Sub readqueary(ByVal sql As String)

Try

With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strconn
.Open()
End With
With cmd
.Connection = conn
.CommandText = sql
read = .ExecuteReader
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Public Sub create(ByVal sql As String)


Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = sql
result = cmd.ExecuteNonQuery
If result = 0 Then
MessageBox.Show("Unsuccessful. Data not saved!", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Successful. Data has been saved!", "GREAT",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
End Try
End Sub

Public Sub reload(ByVal sql As String, ByVal DIG As Object)


Try
dt = New DataTable
strcon.Open()
With cmd
.Connection = strcon
.CommandText = sql
End With
da.SelectCommand = cmd
da.Fill(dt)
DIG.datasource = dt
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
da.Dispose()
End Try
End Sub

Public Sub updates(ByVal sql As String)


Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = sql

result = cmd.ExecuteNonQuery
If result = 0 Then
MessageBox.Show("Unsuccessful. Data failed to update!",
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Successful. Data has been updated!",
"SUCCESSFULLY", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
End Try
End Sub

Public Sub delete(ByVal sql As String)


Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = sql

result = cmd.ExecuteNonQuery
If result = 0 Then
MessageBox.Show("Unsuccessful. Data failed to delete!",
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Successful. Data has been deleted!",
"SUCCESSFULLY", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
End Try
End Sub
End Module

You might also like