Sie sind auf Seite 1von 2

Imports System.Data.

SqlClient Public Class Form1 Dim Dim Dim Dim conn As SqlConnection cmd As New SqlCommand adapter As New SqlDataAdapter ds As DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.Even tArgs) Handles MyBase.Load Label4.Text = "Tambah" Try conn = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename= C:\Users\Public\Documents\coba.mdf;Integrated Security=True;Connect Timeout=30;U ser Instance=True") conn.Open() cmd = New SqlCommand cmd.Connection = conn showList() Catch ex As Exception MessageBox.Show(ex.Message) End Try TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" End Sub Private Sub showList() Try cmd.CommandText = "select id as ID, nama as Nama, alamat as Alamat f rom Customer" cmd.CommandType = CommandType.Text ds = New DataSet adapter.SelectCommand = cmd adapter.Fill(ds, "Customer") DataGridView1.DataSource = ds DataGridView1.DataMember = "Customer" Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles Button1.Click Try If Label4.Text = "Tambah" Then cmd = New SqlCommand("insert into Customer values (" + TextBox1. Text + ", '" + TextBox2.Text + "', '" + TextBox3.Text + "')", conn) Else cmd = New SqlCommand("update Customer set nama='" + TextBox2.Tex t + "', alamat='" + TextBox3.Text + "' where id=" + TextBox1.Text, conn) End If cmd.ExecuteNonQuery() showList() Catch ex As Exception MessageBox.Show(ex.Message) End Try

Label4.Text = TextBox1.Text TextBox2.Text TextBox3.Text End Sub

"Tambah" = "" = "" = ""

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As Syst em.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing conn.Close() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles Button2.Click Try cmd = New SqlCommand("delete from Customer where id=" + TextBox1.Tex t, conn) cmd.ExecuteNonQuery() showList() Catch ex As Exception MessageBox.Show(ex.Message) End Try TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" End Sub Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As System.Obje ct, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridV iew1.CellContentDoubleClick Label4.Text = "Edit" TextBox1.Text = ds.Tables(0).Rows(Me.DataGridView1.CurrentRow.Index).Ite m("id") TextBox2.Text = ds.Tables(0).Rows(Me.DataGridView1.CurrentRow.Index).Ite m("nama") TextBox3.Text = ds.Tables(0).Rows(Me.DataGridView1.CurrentRow.Index).Ite m("alamat") End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles Button3.Click Try cmd = New SqlCommand("update Customer set nama='" + TextBox2.Text + "', alamat='" + TextBox3.Text + "' where id=" + TextBox1.Text, conn) cmd.ExecuteNonQuery() showList() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub End Class

Das könnte Ihnen auch gefallen