Dibawah ini merupakan coding untuk menghubungkan antara vb.net dengan
sql server.
Silahkan copi pastekan ke class yang telah anda buat.
Ingat!!!
Silahkan ganti “Data Source=.;Initial Catalog=DBME48;Integrated
Security=True" menggunakan server komputer anda
masing-masing. Karna itu merupakan server yang akan digunakan untuk
menghubungkan antara vb.net dengan sql server.
Imports System.Data.SqlClient
Imports System.Data
Public Class CLSKoneksi
Protected sql As String
'
Protected conn as SqlClient.SqlConnection
Protected conn As SqlConnection
Protected cmd As SqlCommand
Protected da As SqlDataAdapter
Protected ds As DataSet
Protected dt As DataTable
Public Function openConn() As Boolean
sql = "Data
Source=.;Initial Catalog=DBME48;Integrated Security=True"
conn = New SqlConnection(sql)
conn.Open()
If conn.State <> ConnectionState.Open Then
Return False
Else
Return True
End If
Return False
End Function
Public Sub closeConn()
If Not IsNothing(conn) Then
conn.Close()
conn = Nothing
End If
End Sub
Public Sub ExecuteNonQuery(ByVal query As String)
If Not openConn() Then
MsgBox("Koneksi Gagal", MsgBoxStyle.Critical, "Informasi")
Exit Sub ' break
End If
cmd = New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = query
cmd.ExecuteNonQuery()
cmd = Nothing
closeConn()
End Sub
Public Function reader(ByVal query As String) As DataTable
If Not openConn() Then
MsgBox("Koneksi Gagal", MsgBoxStyle.Critical, "Informasi")
Return Nothing
Exit
Function ' break
End If
cmd = New SqlCommand(query, conn)
da = New SqlDataAdapter
da.SelectCommand = cmd
ds = New DataSet
da.Fill(ds)
dt = ds.Tables(0)
Return dt
dt = Nothing
ds = Nothing
da = Nothing
cmd = Nothing
closeConn()
End Function
End Class
Comments
Post a Comment