Imports MySql

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Imports MySql.Data.

MySqlClient

Public Class DatabaseForm

    Dim myConn As New


MySqlConnection("Server=localhost; User Id=root;
Password=; Database=sample")

    Private Sub DatabaseForm_Load(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load

        
        fill()

    End Sub

    Public Sub fill()


        Dim myAdapter As New MySqlDataAdapter("Select *
from students", myConn)

        Dim myDT As New DataTable


        myDT.Clear()
        myAdapter.Fill(myDT)

        myGridView.DataSource = myDT
        ' Database --> Adapter (Select) -->  DataTable
--> Control/Object (Gridview)
    End Sub

    Private Sub btnView_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs)

    End Sub
    Private Sub txtSearch_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
txtSearch.TextChanged

        Dim searchAdapter As New


MySqlDataAdapter("Select * from students where lname
like '%" & txtSearch.Text & "%' or fname like '%" &
txtSearch.Text & "%'", myConn)

        Dim myDT As New DataTable


        myDT.Clear()
        searchAdapter.Fill(myDT)

        myGridView.DataSource = myDT

    End Sub

    Private Sub btnAdd_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
btnAdd.Click

        Dim strSQL As String


        strSQL = "Insert into students values('" &
txtID.Text & "', '" & txtLName.Text & "', '" &
txtFname.Text & "', '" & txtCourse.Text & "', '" &
txtEmail.Text & "')"

        Dim checkAdapter As New


MySqlDataAdapter("Select * from students where idnum =
'" & txtID.Text & "' ", myConn)
        Dim mydt As New DataTable
        mydt.Clear()
        checkAdapter.Fill(mydt)

        If (mydt.Rows.Count = 0) Then


            Dim myCMD As New MySqlCommand

            If (myConn.State =
Data.ConnectionState.Closed) Then
                myConn.Open()
            End If
            myCMD.Connection = myConn
            myCMD.CommandText = strSQL
            myCMD.ExecuteNonQuery()

            MessageBox.Show("Record Saved!")
            fill()
        Else
            MessageBox.Show("Record already exists!")
        End If

    End Sub

    Private Sub myGridView_CellClick(ByVal sender As


Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
myGridView.CellClick
        Dim row As Integer

        row = myGridView.CurrentRow.Index

        txtID.Text = myGridView.Item(0, row).Value


        txtLName.Text = myGridView.Item(1, row).Value
        txtFname.Text = myGridView.Item(2, row).Value
        txtCourse.Text = myGridView.Item(3, row).Value
        txtEmail.Text = myGridView.Item(4, row).Value

    End Sub

    Private Sub btnEdit_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
btnEdit.Click

        Dim strSQL As String

        strSQL = "Update students set lname = '" &


txtLName.Text & "',  fname = '" & txtFname.Text & "',
course = '" & txtCourse.Text & "', email = '" &
txtEmail.Text & "' where idnum = '" & txtID.Text & "'"

        Dim myCMD As New MySqlCommand


        If (myConn.State = Data.ConnectionState.Closed)
Then
            myConn.Open()
        End If

        myCMD.Connection = myConn
        myCMD.CommandText = strSQL
        myCMD.ExecuteNonQuery()

        MessageBox.Show("Record Updated!")

        fill()

    End Sub

    Private Sub btnDelete_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
btnDelete.Click
        Dim strSQL As String
        Dim response As Integer

        response = MessageBox.Show("Are you sure you


want to delete record of student : " +
myGridView.Item(0, myGridView.CurrentRow.Index).Value,
"Delete Record", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)

        If response = 6 Then


            strSQL = "Delete from students where idnum
= '" & myGridView.Item(0,
myGridView.CurrentRow.Index).Value & "'"

            Dim myCMD As New MySqlCommand

            If (myConn.State =
Data.ConnectionState.Closed) Then
                myConn.Open()
            End If

            myCMD.Connection = myConn
            myCMD.CommandText = strSQL
            myCMD.ExecuteNonQuery()

            MessageBox.Show("Record Deleted!")
            fill()
        End If

        

    End Sub

    
End Class

You might also like