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

Source Code

The document contains source code for connecting to a SQL Server database and performing basic CRUD (create, read, update, delete) operations in VB.NET. It includes functions for opening and closing a database connection, and code examples for inserting, updating, deleting, searching for, and displaying data in a datagrid by executing SQL commands on the connection. The form code handles button clicks to call these functions and perform the CRUD operations, updating the UI in response.

Uploaded by

adenz_mobho
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Source Code

The document contains source code for connecting to a SQL Server database and performing basic CRUD (create, read, update, delete) operations in VB.NET. It includes functions for opening and closing a database connection, and code examples for inserting, updating, deleting, searching for, and displaying data in a datagrid by executing SQL commands on the connection. The form code handles button clicks to call these functions and perform the CRUD operations, updating the UI in response.

Uploaded by

adenz_mobho
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Source Code untuk di module 1

Imports System.Data.SqlClient Imports System.Data Module Module1 Public constrg As String = "Data source=.\sqlexpress;Initial Catalog=latihan;integrated security=true" Public koneksi As SqlConnection Public perintah As SqlCommand Public da As SqlDataAdapter Public dr As SqlDataReader Public Function buka() As SqlConnection Try koneksi = New SqlConnection(constrg) If koneksi.State <> ConnectionState.Closed Then koneksi.Close() koneksi.Open() Catch ex As Exception MsgBox(Err.Description.ToString()) End Try Return koneksi End Function Public Function tutup() As SqlConnection Try koneksi = New SqlConnection(constrg) If koneksi.State <> ConnectionState.Open Then koneksi.Close() koneksi.Close() Catch ex As Exception MsgBox(Err.Description.ToString()) End Try Return koneksi End Function End Module

Source code untuk yang di form


Imports System.Data.SqlClient Public Class Form1 Private kata As String Private ds As New DataSet Private dttable As New DataTable Private dtview As New DataView 'Berikut ini contoh beberapa perintah dasar dalam pemrograman vb.net dan database sqlserver 'Semoga Contoh dibawah ini bermanfaat 'simpan Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click buka() kata = "insert into person(name,addr)values('" & TextBox2.Text & "','" & TextBox1.Text & "')" perintah = New SqlCommand(kata, koneksi) Dim i As Integer = perintah.ExecuteNonQuery Try If i = 1 Then MsgBox("data disimpan") isi() Else MsgBox("gagal") End If Catch ex As Exception MsgBox(Err.Description.ToString()) End Try End Sub 'edit data Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click buka() kata = "update person set addr='" & TextBox1.Text & "' where name='" & TextBox2.Text & "'" perintah = New SqlCommand(kata, koneksi) Dim i As Integer = perintah.ExecuteNonQuery Try If i = 1 Then MsgBox("data di ubah") isi() Else MsgBox("gagal") End If Catch ex As Exception MsgBox(Err.Description.ToString()) End Try End Sub

'delete data Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click buka() kata = "delete from person where name='" & TextBox2.Text & "'" perintah = New SqlCommand(kata, koneksi) Dim i As Integer = perintah.ExecuteNonQuery Try If i = 1 Then MsgBox("data dihapus") isi() Else MsgBox("gagal") End If Catch ex As Exception MsgBox(Err.Description.ToString()) End Try End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load isi() End Sub 'Isi data grid Sub isi() buka() ds.Clear() DataGridView1.Refresh() da = New SqlDataAdapter("select * from person", koneksi) da.Fill(ds, "person") dttable = ds.Tables("person") dtview.Table = dttable DataGridView1.DataSource = dtview settingkolom() End Sub 'settingan nama kolom Sub settingkolom() With DataGridView1 .Columns("Name").HeaderText = "Nama Orang" .Columns("addr").HeaderText = "Alamat" End With End Sub 'menampilkan data/pencarian dengan keypress Private Sub TextBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress If Asc(e.KeyChar) = 13 Then buka() kata = "select * from person where name='" & TextBox2.Text & "'" perintah = New SqlCommand(kata, koneksi) dr = perintah.ExecuteReader While dr.Read = True TextBox1.Text = dr(1) End While Else Exit Sub End If End Sub 'pencarian dengan cara seleksi data grid Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged buka() ds.Clear() DataGridView1.Refresh() da = New SqlDataAdapter("select * from person where name like '" & TextBox2.Text & "%'", koneksi) da.Fill(ds, "person") dttable = ds.Tables("person") dtview.Table = dttable DataGridView1.DataSource = dtview settingkolom() End Sub 'kosongkan textbox Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click TextBox1.Clear() TextBox2.Clear() End Sub End Class

You might also like