≡ Menu

Nambah Record Ke MySql di vb.Net

Dim InsertQuery As String
Dim Kelamin As String
Dim hasil As Integer
Dim cmd As MySqlCommand

If rbPria.Checked = True Then
Kelamin = rbPria.Text
Else
Kelamin = rbWanita.Text
End If

InsertQuery = “INSERT INTO Anggota ” & _
“(id_anggota, nama, alamat, jenis_kelamin)” & _
” Values(‘” & txtId.Text & “‘, ” & _
” ‘” & txtNama.Text & “‘, ” & _
” ‘” & txtAlamat.Text & “‘, ‘” & Kelamin & “‘)”

Try
cmd = New MySqlCommand(InsertQuery, Conn)
cmd.Connection.Open()
‘ eksekusi pernyataan SQL
hasil = cmd.ExecuteNonQuery()
‘ periksa nilai yang dikembalikan
If (hasil > 0) Then
MessageBox.Show(“Record Berhasil Dimasukkan”, _
“Sukses”, MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Clear()
End If
Catch ex As MySqlException
MessageBox.Show(“Failed : ” & ex.Message, “Gagal”, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
cmd.Connection.Close()
cmd.Dispose()

{ 0 comments… add one }

Leave a Comment