-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDbCore.vb
148 lines (140 loc) · 5.29 KB
/
MyDbCore.vb
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
Imports Microsoft.VisualBasic
Imports System.Configuration
Imports MySql.Data
'/**
'*
'* @author: daMgeL
'* @date: 03-03-2014:2:04PM
'*
'/**
Public Class MyDbCore
Implements IDisposable
Private Shared myDataSet As New Data.DataSet
Private Shared myCnSQL As New MySqlClient.MySqlConnection(GetConnectionStringByName("myConnString"))
Private Shared myCommandSQL As New MySqlClient.MySqlCommand
Private Shared Function GetConnectionStringByName(ByVal name As String) As String
' asumo que puede ocurrir un error
Dim returnCnnString As String = Nothing
' busco el nombre de la coneccion en el conectionString
Dim settings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(name)
'Si existe, retorno el valor.
If Not settings Is Nothing Then
returnCnnString = settings.ConnectionString
End If
Return returnCnnString
End Function
Private Shared Sub ABM(ByVal statementSQL As String)
'A=insetar(altas), B=Eliminar(bajas), M=modificar(modificaciones).
Try
myCnSQL.Open()
myCommandSQL = New MySqlClient.MySqlCommand(statementSQL, myCnSQL)
myCommandSQL.ExecuteNonQuery()
Catch Ex As Exception
MsgBox("Error al conectar a la base de datos: " + Ex.Message)
Finally
myCnSQL.Close()
End Try
End Sub
Public Shared Sub Insert(statementSQL As String)
Dim ins As String = "INSERT INTO "
ABM(ins + statementSQL)
End Sub
Public Shared Sub Update(statementSQL As String)
Dim upd As String = "UPDATE "
ABM(upd + statementSQL)
End Sub
Public Shared Sub Delete(statementSQL As String)
Dim del As String = "DELETE "
ABM(del + statementSQL)
End Sub
Public Shared Sub DeleteById(statementSQL As String, Where As String)
Dim del As String = "DELETE "
Where = "WHERE " + Where
ABM(del + statementSQL + Where)
End Sub
Public Shared Function GetDatosAsDataset( _
ByVal FIELDS As String, _
ByVal FROM As String, _
Optional WHERE As String = "") As Data.DataSet
'Funcion para buscar datos, permite llenar un dataset y un combo.
Try
Dim setWhere As String = WHERE
If setWhere = "" Then
'valor asignado en el where
setWhere = ""
Else
setWhere = " WHERE " & WHERE.ToString
End If
Dim setFrom As String = " FROM " & FROM.ToString
myDataSet = New Data.DataSet
myCnSQL.Open()
Dim myDataAdapter As New MySqlClient.MySqlDataAdapter(FIELDS & setFrom & setWhere, myCnSQL)
myDataAdapter.Fill(myDataSet, FROM.ToString)
Catch ex As Exception
MsgBox("Error al conectar a la base de datos" + ex.Message)
Finally
myCnSQL.Close()
myDataSet.Dispose()
End Try
Return myDataSet
End Function
Public Shared Function GetEscalarAsDouble(ByVal statementSQL As String) As Double
'Funcion que busca y retorna una constante(numero).
Try
myCnSQL.Open()
myCommandSQL = New MySqlClient.MySqlCommand(statementSQL, myCnSQL)
Return CDbl(myCommandSQL.ExecuteScalar())
Catch ex As Exception
' si hay alguna excepcion retornar CERO en vez de una excepcion
Return 0
Finally
myCnSQL.Close()
myCommandSQL.Dispose()
End Try
End Function
Public Shared Sub RawQuery(statementSQL As String)
ABM(statementSQL)
End Sub
Public Shared Function VerificarConexion() As Boolean
'Funcion para verificar si existe conexion a la db,
'retorna TRUE si hay conexion y FALSE si no hay conexion.
Dim estado As Boolean = False
If estado = False Then
Try
myCnSQL.Open()
myCnSQL.Close()
estado = True
Catch ex As Exception
' retornar como FALSE si hay algun error en la conexion.
Return False
End Try
End If
Return estado
End Function
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
' Funcion para detectar llamadas redundantes y liberar recursos inutilizados en esta clase.
If disposing Then
' Aqui se deben eliminar los objetos con estado administrado
'( los objetos que se desean limpiar.)
myCommandSQL.Dispose()
myCnSQL.Close()
myDataSet.Dispose()
End If
End Sub
Private Sub Dispose() Implements IDisposable.Dispose
' No cambiar este código. Colocar el código de limpieza en Dispose(disposing As Boolean).
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Private Function FixQueryToProcess(CompleteText As String, WordToFind As String) As Boolean
Dim Encontrado As Boolean = False
Dim i As Integer
i = InStr(1, CompleteText, WordToFind)
If i > 0 Then
Encontrado = True
Else
Encontrado = False
End If
Return Encontrado
End Function
End Class