Minggu, 05 Juni 2011

KONEKSI DB


Cara koneksi database visual basic 2010 dengan ms SQL Server 2008
Pertama kita buat modul koneksi, lalu buat tombol untuk test koneksi

Imports System.Data
Imports System.Data.Sql

Module konek
    Public Database As New OleDb.OleDbConnection
    Public Tampil As New OleDb.OleDbCommand
    Public Tampil2 As New OleDb.OleDbCommand

    Public Tampilkan As OleDb.OleDbDataReader
    Public Hasiljur, jur As String

    Public Sub Koneksi()
        Try
            Database.Close()
            Database.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=ptabc; Data Source=MUJAN-COMP\SQLEXPRESS;"
            Database.Open()
            'Integrated Security=True
            MsgBox("konek sukses")
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub

    Public Sub jurusan()
        Call Koneksi()
        Tampil2.Connection = Database
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from table_1 where kdjur='" + Trim(jur) & "'"
        Tampilkan = Tampil2.ExecuteReader
        If Tampilkan.HasRows = True Then
            While Tampilkan.Read
                If IsDBNull(Tampilkan("kdjur")) Then
                    Hasiljur = "_"
                Else
                    Hasiljur = Tampilkan("namajur")
                End If
            End While
        End If
    End Sub
End Module


'kode program di form

Public Class Form1

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call Koneksi()
        jur = TextBox2.Text
        jurusan()
        TextBox1.Text = Hasiljur
    End Sub
End Class

Tidak ada komentar:

Posting Komentar