Untuk menghubungkan VB ke SQL Server diperlukan variabel
Public Database As New OleDb.OleDbConnection
Public Tampil As New OleDb.OleDbCommand
Public Tampil2 As New OleDb.OleDbCommand
Public Tampilkan As OleDb.OleDbDataReader
Untuk konek database sever:
Database.connectionstring = "Provider = SQLOLEDB.1; Password=sandi; persist security
Info = True; User ID = SS; Initial Catalog = nmdatabase; Data Source = 192.168.10.44" ( Data source bisa nama komputer sever atau no IP nya)
Untuk koneksi ke database lokal:
Database.ConnectionString = "Provider = SQLOLEDB.1; Integrated Security = SSPI; Persist Security Info= False; Initial Catalog = jurusan; Data Source = BATURADE-D8AD88\SQLEXPRESS;"
Contoh:
Buat dulu class module
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 nmjur, kdjur, hasilcek As String
Public Sub koneksi()
Try
Database.Close()
Database.ConnectionString = "Provider = SQLOLEDB.1; Integrated Security = SSPI; Persist Security Info= False; Initial Catalog = jurusan; Data Source = BATURADE-D8AD88\SQLEXPRESS;"
Database.Open()
MsgBox("koneksi berhasil")
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
End Module
Coding di form:
Public Class Form1
Sub jurusan()
Call koneksi()
Tampil2.Connection = Database
Tampil2.CommandType = CommandType.Text
Tampil2.CommandText = " select * from jurusan where kdjur ='" & Trim(TextBox1.Text) & "'"
Tampilkan = Tampil2.ExecuteReader
If Tampilkan.HasRows = True Then
While Tampilkan.Read()
If IsDBNull(Tampilkan("kdjur")) Then
TextBox2.Text = "_"
Else
TextBox2.Text = Tampilkan("nmjur")
End If
End While
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call koneksi()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call jurusan()
End Sub
End Class
Hasilnya :
Kamis, 19 Mei 2011
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar