Advertisement
AndreJaya

Untitled

Nov 16th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.49 KB | None | 0 0
  1. Imports System.Data.SqlClient
  2. Public Class Form2
  3.     Dim conns As SqlConnection
  4.     Dim da As SqlDataAdapter
  5.     Dim ds As DataSet
  6.     Dim dr As SqlDataReader
  7.     Dim cmd As SqlCommand
  8.     Dim str As String
  9.  
  10.     Sub koneksisql()
  11.         Try
  12.             str = "Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\Users\INDRA PC\Documents\Visual Studio 2013\Projects\testproject_autoincrement\testproject_autoincrement\Database.mdf"";Integrated Security=True"
  13.             conns = New SqlConnection(str)
  14.             conns.Open()
  15.             MsgBox("connection succesfully")
  16.         Catch ex As Exception
  17.             MsgBox("Koneksi database gagal", MsgBoxStyle.Critical, "error")
  18.         End Try
  19.     End Sub
  20.     Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  21.         Call koneksisql()
  22.         TextBox1.Enabled = False
  23.         TextBox2.Focus()
  24.         da = New SqlDataAdapter("select * from user", conns)
  25.         ds = New DataSet
  26.         da.Fill(ds)
  27.         DataGridView1.DataSource = ds.Tables(0)
  28.         DataGridView1.ReadOnly = True
  29.     End Sub
  30.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  31.         Call koneksisql()
  32.         Dim simpan As String
  33.         simpan = "insert into user (nama,alamat,phone) values ('" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
  34.         cmd = New SqlCommand(simpan, conns)
  35.         cmd.ExecuteNonQuery()
  36.         MsgBox("data berhasil disimpan")
  37.  
  38.     End Sub
  39.  
  40. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement