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

Coding Cari Siswa

This document contains code for connecting to a MySQL database and retrieving student data from a table to display in a datagridview. It imports MySQL and SQL libraries, declares connection and data adapter objects, and has methods to open the connection, fill a dataset from the table, and display it. It also includes code to search the table by name and display matching records or a message if none are found.

Uploaded by

iwanhc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

Coding Cari Siswa

This document contains code for connecting to a MySQL database and retrieving student data from a table to display in a datagridview. It imports MySQL and SQL libraries, declares connection and data adapter objects, and has methods to open the connection, fill a dataset from the table, and display it. It also includes code to search the table by name and display matching records or a message if none are found.

Uploaded by

iwanhc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Imports MySql.Data.

MySqlClient
Imports System.Data.Sql

Public Class Form4

Dim str As String


Dim str2 As String 'untuk spp
Dim koneksi As MySqlConnection
Dim da As MySqlDataAdapter
Dim ds As DataSet
Dim cmd As MySqlCommand

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


str = "server=localhost;user id=root;password=;database=11rpla"
koneksi = New MySqlConnection(str)
If koneksi.State = ConnectionState.Closed Then
Try
koneksi.Open()
MsgBox("Database koneksi berhasil", vbInformation, "Test Koneksi")
Catch ex As Exception
MsgBox("Database koneksi TIDAK berhasil", vbCritical, "Test Koneksi")
End Try
End If
End Sub

Sub tampil()
da = New MySqlDataAdapter(str, koneksi)
ds = New DataSet
ds.Clear()
da.Fill(ds, "t_siswa")
dgv1.DataSource = ds.Tables(0)
'Mengatur lebar kolom di datagridview
dgv1.Columns(0).Width = 40
dgv1.Columns(1).Width = 100
dgv1.Columns(2).Width = 50
dgv1.Columns(3).Width = 150
'Mengatur header di datagridview
dgv1.Columns(0).HeaderText = "NIS"
dgv1.Columns(1).HeaderText = "NAMA"
dgv1.Columns(2).HeaderText = "KELAS"
dgv1.Columns(3).HeaderText = "KOMPETENSI KEAHLIAN"
End Sub

Private Sub BtTampil_Click(sender As Object, e As EventArgs) Handles BtTampil.Click


str = "select * from t_siswa"
tampil()

End Sub

-----
Private Sub BtnCari_Click(sender As Object, e As EventArgs) Handles BtnCari.Click
Dim nama As String
koneksi.Close()
koneksi.Open()
nama = InputBox("masukkan nama siswa ", "Pencarian")
str = "select * from t_siswa where nama like '%" & nama & "%'"
cmd = New MySqlCommand(str, koneksi)
cmd.ExecuteNonQuery()
tampil()

If cmd.ExecuteReader.Read Then 'cek apakah ada data siswa yg dicari


MsgBox("ada")

TextBox1.Text = dgv1.Item(0, dgv1.CurrentRow.Index).Value


TextBox2.Text = dgv1.Item(1, dgv1.CurrentRow.Index).Value
TextBox3.Text = dgv1.Item(2, dgv1.CurrentRow.Index).Value

Else
MsgBox("kosong")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End If
koneksi.Close()

End Sub

You might also like