-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNuDB.vb
More file actions
162 lines (152 loc) · 4.7 KB
/
NuDB.vb
File metadata and controls
162 lines (152 loc) · 4.7 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports MySql.Data.MySqlClient
Imports System.Windows.Forms
Imports System.Data.OracleClient
Public Class NuDB
Public Shared MYSQL As Integer = 0
Public Shared SQL As Integer = 1
Public Shared ODBC As Integer = 2
Public Shared OLEDB As Integer = 3
Public Shared ADODB As Integer = 4
Public Shared ORACLE As Integer = 5
Private idbConn As IDbConnection = Nothing
Private idbAdapter As IDbDataAdapter = Nothing
Private dbAdapter As DbDataAdapter = Nothing
Private iReader As IDataReader = Nothing
Private cmd As IDbCommand = Nothing
Private dbDriver As Integer
Private dbConfig As String
Private q As String
Public Sub New(dbd As Integer, dbc As String)
dbDriver = dbd
dbConfig = dbc
End Sub
Public Sub close()
idbConn.Close()
End Sub
Public Function connect()
Select Case dbDriver
Case OLEDB
idbConn = New OleDbConnection(dbConfig)
Case SQL
idbConn = New SqlConnection(dbConfig)
Case ODBC
idbConn = New OdbcConnection(dbConfig)
Case MYSQL
idbConn = New MySqlConnection(dbConfig)
Case ADODB
idbConn = New ADODB.Connection
idbConn.ConnectionString = dbConfig
Case ORACLE
idbConn = New OracleConnection(dbConfig)
Case Else
End Select
If IsNothing(idbConn) Then
Return False
Else
Try
idbConn.Open()
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd = idbConn.CreateCommand()
End Try
Return isConnected()
End If
End Function
Private Function isConnected()
If idbConn.State = ConnectionState.Open Then
Return True
Else
Return False
End If
End Function
Public Function getDataAdapter() As IDbDataAdapter
Select Case dbDriver
Case OLEDB
idbAdapter = New OleDbDataAdapter(q, dbConfig)
Case SQL
idbAdapter = New SqlDataAdapter(q, dbConfig)
Case ODBC
idbAdapter = New OdbcDataAdapter(q, dbConfig)
Case MYSQL
idbAdapter = New MySqlDataAdapter(q, dbConfig)
Case ADODB
idbAdapter = New OleDbDataAdapter(q, dbConfig)
Case ORACLE
idbAdapter = New OracleDataAdapter(q, dbConfig)
Case Else
End Select
Return idbAdapter
End Function
Public Sub setQuery(query As String)
If isConnected() Then
cmd.CommandText = query
Else
MsgBox("Could Not Make Connection")
End If
End Sub
Public Function query()
Dim stat = cmd.ExecuteNonQuery()
'cmd.Dispose()
Return stat
End Function
Public Function loadObject()
Dim table As New DataTable
dbAdapter = getDataAdapter()
dbAdapter.SelectCommand = cmd
dbAdapter.Fill(table)
If table.Rows.Count >= 1 Then
Return table.Rows(0)
Else
Return Nothing
End If
End Function
Public Function loadObjectList()
Dim table As New DataTable
dbAdapter = getDataAdapter()
dbAdapter.SelectCommand = cmd
dbAdapter.Fill(table)
If table.Rows.Count >= 1 Then
Return table.Rows
Else
Return Nothing
End If
End Function
Public Function loadResult()
Dim res As Object = cmd.ExecuteScalar
If res Is Nothing Then
Return ""
Else
Return res.ToString()
End If
End Function
Public Function loadDataSet(table As String, Optional ByVal q As String = Nothing) As DataSet
Dim ds As New DataSet()
If q IsNot Nothing Then
setQuery(q)
Else
setQuery("select * from " & table)
End If
dbAdapter = getDataAdapter()
dbAdapter.SelectCommand = cmd
dbAdapter.Fill(ds)
Return ds
End Function
Public Sub setDataGrid(datagrid As DataGridView, table As String, Optional ByVal q As String = Nothing)
Dim ds As DataSet
If q IsNot Nothing Then
ds = loadDataSet(table, q)
Else
ds = loadDataSet(table)
End If
datagrid.DataSource = ds.Tables(0)
datagrid.AutoResizeColumns()
datagrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End Sub
End Class