2.1 Pertama-tama buat dulu Class untuk koneksi ke database. Dalam program gw, nama class-nya adalah : ClsKoneksi
Coding :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| Imports Microsoft.VisualBasic Imports System.Data.Sql Imports System.Data Imports System.Data.SqlClient Public Class ClsKoneksi Protected tblPengguna = New DataTable Protected SQL As String Protected Cn As SqlClient.SqlConnection Protected Cmd As SqlClient.SqlCommand Protected Da As SqlClient.SqlDataAdapter Protected Ds As DataSet Protected Dt As DataTable Public Function OpenConn() As Boolean Cn = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=concept_db;Integrated Security=True") Cn.Open() If Cn.State <> ConnectionState.Open Then Return False Else Return True End If End Function Public Sub CloseConn() If Not IsNothing(Cn) Then Cn.Close() Cn = Nothing End If End Sub Public Function ExecuteQuery(ByVal Query As String) As DataTable If Not OpenConn() Then MsgBox("Koneksi Gagal..!!", MsgBoxStyle.Critical, "Access Failed") Return Nothing Exit Function End If Cmd = New SqlClient.SqlCommand(Query, Cn) Da = New SqlClient.SqlDataAdapter Da.SelectCommand = Cmd Ds = New Data.DataSet Da.Fill(Ds) Dt = Ds.Tables(0) Return Dt Dt = Nothing Ds = Nothing Da = Nothing Cmd = Nothing CloseConn() End Function Public Sub ExecuteNonQuery(ByVal Query As String) If Not OpenConn() Then MsgBox("Koneksi Gagal..!!", MsgBoxStyle.Critical, "Access Failed..!!") Exit Sub End If Cmd = New SqlClient.SqlCommand Cmd.Connection = Cn Cmd.CommandType = CommandType.Text Cmd.CommandText = Query Cmd.ExecuteNonQuery() Cmd = Nothing CloseConn() End Sub End Class |
sumber referensi : http://herosetyanofario.wordpress.com/2011/06/17/membuat-autonumber-menggunakan-kombinasi-bulan-tahun-di-vb-net/
No comments:
Post a Comment