SHARE
TWEET

VB .NET MYSQL : Input Data, Edit, Hapus

stevennathaniel Apr 5th, 2016 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Data.Odbc
  2.  
  3. Imports CrystalDecisions.CrystalReports.Engine
  4.  
  5. Imports CrystalDecisions.Shared
  6.  
  7. Imports CrystalDecisions.Windows.Forms
  8.  
  9. Imports CrystalDecisions.ReportSource
  10.  
  11.  
  12. Public Class Form1
  13.  
  14.     Dim koneksi As OdbcConnection = Nothing
  15.  
  16.     Dim stringKoneksi As String = "DSN=localhost4"
  17.  
  18.     Dim stringSQL As String
  19.  
  20.  
  21.     Sub tampilIsisDataGrid()
  22.  
  23.         koneksi = New OdbcConnection(stringKoneksi)
  24.  
  25.         stringSQL = "SELECT * FROM pelanggan"
  26.  
  27.         Dim adapter As New OdbcDataAdapter(stringSQL, koneksi)
  28.  
  29.         Dim ds As New DataSet
  30.  
  31.         adapter.Fill(ds, "nama")
  32.  
  33.         Try
  34.  
  35.             koneksi.Open()
  36.  
  37.             DataGridView1.DataSource = ds.Tables(0)
  38.  
  39.  
  40.         Catch ex As Exception
  41.  
  42.             MessageBox.Show(ex.Message)
  43.  
  44.  
  45.         End Try
  46.  
  47.  
  48.  
  49.     End Sub
  50.  
  51.  
  52.  
  53.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  54.  
  55.         koneksi = New OdbcConnection(stringKoneksi)
  56.  
  57.         stringSQL = "INSERT INTO pelanggan(idpelanggan,namaawal,namaakhir,alamat,negara,kodepos,email,telepon,kota,tambahmailinglist,catatanpelanggan) VALUES (?,?,?,?,?,?,?,?,?,?,?)"
  58.  
  59.         Try
  60.  
  61.             koneksi.Open()
  62.  
  63.             Dim perintah = New OdbcCommand(stringSQL, koneksi)
  64.  
  65.             perintah.Prepare()
  66.  
  67.             perintah.Parameters.AddWithValue("@idpelanggan", TextBox1.Text)
  68.  
  69.             perintah.Parameters.AddWithValue("@namaawal", TextBox2.Text)
  70.  
  71.             perintah.Parameters.AddWithValue("@namaakhir", TextBox3.Text)
  72.  
  73.             perintah.Parameters.AddWithValue("@alamat", TextBox4.Text)
  74.  
  75.             perintah.Parameters.AddWithValue("@negara", TextBox5.Text)
  76.  
  77.             perintah.Parameters.AddWithValue("@kodepos", TextBox6.Text)
  78.  
  79.             perintah.Parameters.AddWithValue("@email", TextBox7.Text)
  80.  
  81.             perintah.Parameters.AddWithValue("@telepon", TextBox8.Text)
  82.  
  83.             perintah.Parameters.AddWithValue("@kota", TextBox9.Text)
  84.  
  85.             perintah.Parameters.AddWithValue("@tambahmailinglist", ComboBox1.Text)
  86.  
  87.             perintah.Parameters.AddWithValue("@catatanpelanggan", TextBox10.Text)
  88.  
  89.  
  90.             perintah.ExecuteNonQuery()
  91.  
  92.  
  93.             koneksi.Close()
  94.  
  95.  
  96.  
  97.         Catch ex As Exception
  98.  
  99.             MessageBox.Show(ex.Message)
  100.  
  101.  
  102.         End Try
  103.  
  104.  
  105.         TextBox1.Text = ""
  106.  
  107.         TextBox2.Text = ""
  108.  
  109.         TextBox3.Text = ""
  110.  
  111.         TextBox3.Text = ""
  112.  
  113.         TextBox4.Text = ""
  114.  
  115.         TextBox5.Text = ""
  116.  
  117.         TextBox6.Text = ""
  118.  
  119.         TextBox7.Text = ""
  120.  
  121.         TextBox8.Text = ""
  122.  
  123.         TextBox9.Text = ""
  124.  
  125.         TextBox10.Text = ""
  126.  
  127.         ComboBox1.SelectedIndex = 0
  128.  
  129.         tampilIsisDataGrid()
  130.  
  131.  
  132.  
  133.  
  134.     End Sub
  135.  
  136.  
  137.     Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  138.  
  139.         ComboBox1.Items.Add("Ya")
  140.  
  141.         ComboBox1.Items.Add("Tidak")
  142.  
  143.         koneksi = New Odbc.OdbcConnection
  144.  
  145.         koneksi.ConnectionString = "Dsn=localhost4"
  146.  
  147.         Dim perintah As String = "SELECT * FROM pelanggan"
  148.  
  149.         Dim adapter As New OdbcDataAdapter(perintah, koneksi)
  150.  
  151.         Dim ds As New DataSet()
  152.  
  153.         adapter.Fill(ds, "pelanggan")
  154.  
  155.  
  156.         Try
  157.  
  158.             koneksi.Open()
  159.  
  160.             DataGridView1.DataSource = ds.Tables(0)
  161.  
  162.         Catch ex As Exception
  163.  
  164.             MsgBox("Terjadi Error: " & ex.Message)
  165.  
  166.  
  167.         Finally
  168.  
  169.             koneksi.Close()
  170.  
  171.  
  172.         End Try
  173.  
  174.  
  175.     End Sub
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.     Private Sub DataGridView1_CellMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
  184.  
  185.         TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value
  186.  
  187.         TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value
  188.  
  189.         TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
  190.  
  191.         TextBox4.Text = DataGridView1.Rows(e.RowIndex).Cells(3).Value
  192.  
  193.         TextBox5.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value
  194.  
  195.         TextBox6.Text = DataGridView1.Rows(e.RowIndex).Cells(5).Value
  196.  
  197.         TextBox7.Text = DataGridView1.Rows(e.RowIndex).Cells(6).Value
  198.  
  199.         TextBox8.Text = DataGridView1.Rows(e.RowIndex).Cells(7).Value
  200.  
  201.         TextBox9.Text = DataGridView1.Rows(e.RowIndex).Cells(8).Value
  202.  
  203.         ComboBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(9).Value
  204.  
  205.         TextBox10.Text = DataGridView1.Rows(e.RowIndex).Cells(10).Value
  206.  
  207.  
  208.     End Sub
  209.  
  210.     Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
  211.  
  212.  
  213.         Form4.Show()
  214.  
  215.     End Sub
  216.  
  217.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  218.         ' Hapus data
  219.  
  220.         koneksi = New OdbcConnection(stringKoneksi)
  221.  
  222.         stringSQL = "DELETE FROM pelanggan WHERE idpelanggan=(?)"
  223.  
  224.         Try
  225.  
  226.             koneksi.Open()
  227.  
  228.             Dim perintah = New OdbcCommand(stringSQL, koneksi)
  229.  
  230.             perintah.Prepare()
  231.  
  232.             perintah.Parameters.AddWithValue("@idpelanggan", TextBox1.Text)
  233.  
  234.             perintah.ExecuteNonQuery()
  235.  
  236.             koneksi.Close()
  237.  
  238.         Catch ex As Exception
  239.  
  240.             MessageBox.Show(ex.Message)
  241.  
  242.  
  243.         End Try
  244.  
  245.  
  246.         TextBox1.Text = ""
  247.  
  248.         TextBox2.Text = ""
  249.  
  250.         TextBox3.Text = ""
  251.  
  252.         TextBox4.Text = ""
  253.  
  254.         TextBox5.Text = ""
  255.  
  256.         TextBox6.Text = ""
  257.  
  258.         TextBox7.Text = ""
  259.  
  260.         TextBox8.Text = ""
  261.  
  262.         TextBox9.Text = ""
  263.  
  264.         TextBox10.Text = ""
  265.  
  266.         ComboBox1.SelectedIndex = 0
  267.  
  268.         tampilIsisDataGrid()
  269.  
  270.  
  271.  
  272.     End Sub
  273.  
  274.     Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
  275.  
  276.         'Update Data
  277.  
  278.         koneksi = New OdbcConnection(stringKoneksi)
  279.  
  280.         stringSQL = "UPDATE pelanggan SET namaawal=(?),namaakhir=(?),alamat=(?),negara=(?),kodepos=(?),email=(?),telepon=(?),kota=(?),tambahmailinglist=(?),catatanpelanggan=(?) WHERE idpelanggan=(?)"
  281.  
  282.         Try
  283.  
  284.             koneksi.Open()
  285.  
  286.             Dim perintah = New OdbcCommand(stringSQL, koneksi)
  287.  
  288.             perintah.Prepare()
  289.  
  290.             perintah.Parameters.AddWithValue("@namaawal", TextBox2.Text)
  291.  
  292.             perintah.Parameters.AddWithValue("@namaakhir", TextBox3.Text)
  293.  
  294.             perintah.Parameters.AddWithValue("@alamat", TextBox4.Text)
  295.  
  296.             perintah.Parameters.AddWithValue("@negara", TextBox5.Text)
  297.  
  298.             perintah.Parameters.AddWithValue("@kodepos", TextBox6.Text)
  299.  
  300.             perintah.Parameters.AddWithValue("@email", TextBox7.Text)
  301.  
  302.             perintah.Parameters.AddWithValue("@telepon", TextBox8.Text)
  303.  
  304.             perintah.Parameters.AddWithValue("@kota", TextBox9.Text)
  305.  
  306.             perintah.Parameters.AddWithValue("@tambahmailinglist", ComboBox1.Text)
  307.  
  308.             perintah.Parameters.AddWithValue("@catatanpelanggan", TextBox10.Text)
  309.  
  310.             perintah.Parameters.AddWithValue("@idpelanggan", TextBox1.Text)
  311.  
  312.             perintah.ExecuteNonQuery()
  313.  
  314.             koneksi.Close()
  315.  
  316.  
  317.         Catch ex As Exception
  318.  
  319.             MessageBox.Show(ex.Message)
  320.  
  321.  
  322.         End Try
  323.  
  324.         TextBox1.Text = ""
  325.  
  326.         TextBox2.Text = ""
  327.  
  328.         TextBox3.Text = ""
  329.  
  330.         TextBox4.Text = ""
  331.  
  332.         TextBox5.Text = ""
  333.  
  334.         TextBox6.Text = ""
  335.  
  336.         TextBox7.Text = ""
  337.  
  338.         TextBox8.Text = ""
  339.  
  340.         TextBox9.Text = ""
  341.  
  342.         TextBox10.Text = ""
  343.  
  344.         ComboBox1.SelectedIndex = 0
  345.  
  346.         tampilIsisDataGrid()
  347.  
  348.  
  349.  
  350.     End Sub
  351. End Class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top