cara koneksi vb.net dengan database sql server 2005

 

 

Imports System.Data.SqlClient

 

Public Class form1

Dim conn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet

 

Sub koneksidb()
'tidak menggunakan password
conn = New SqlConnection("data source=.\sqlexpress;initial catalog=dbnashir;integrated security=true")
'menggunakan password
'conn = New SqlConnection("data source=.\sqlexpress;initial catalog=dbnashir;UID=sa;password=123")
conn.Open()
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call koneksidb()
da = New SqlDataAdapter("select * from tblbarang", conn)
ds = New DataSet
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.ReadOnly = True
End Sub
End Class