'
' /////////////////////////////////////////////////////////////
' // Autor: BlackZeroX ( Ortega Avila Miguel Angel ) //
' // //
' // Web: http://InfrAngeluX.Sytes.Net/ //
' // //
' // |-> Pueden Distribuir Este Codigo siempre y cuando //
' // no se eliminen los creditos originales de este codigo //
' // No importando que sea modificado/editado o engrandesido //
' // o achicado, si es en base a este codigo es requerido //
' // el agradacimiento al autor. //
' /////////////////////////////////////////////////////////////
Option Explicit On
Imports MySql.Data.MySqlClient
Public Class Cls_Ado
' // Constantes de conexión a MySQL por ADO .NET!¡.
Private Const PreStrConexion As String = "Server=(ip);Database=(db);user id=(u);password=(p);Connect Timeout=30;"
Private Const PREDBIpServer As String = "127.0.0.1" ' // Predeterminado Default
Private Const PREDBUser As String = "root" ' // Predeterminado Default
Private Const PREDBPass As String = "root" ' // Predeterminado Default
Private Const PREDBIni As String = "Integra" ' // Predeterminado
' // Otros.
Private StrConexion As String
Private MySQLConnectionString As String
' // .||.
Private MyDataAdapter As MySqlDataAdapter
Private MyCommandBuilder As MySqlCommandBuilder
' // .||.
Private DBIpServer As String
Private DBUser As String
Private DBPass As String
Private DBIni As String
Private PrivDataSet As DataSet
Private PrivSqlQuery As String
Private PrivTable As String
Private PrivThisTheardTag As String
Event ErrorEvent(ByVal Ex As Exception, ByVal Cancel As Boolean)
Event Finish()
Public Property StringConection() As String
Get
Return StrConexion
End Get
Set(ByVal value As String)
StrConexion = value
End Set
End Property
Public Property This_Theard_Tag() As String
Get
Return PrivThisTheardTag
End Get
Set(ByVal value As String)
PrivThisTheardTag = value
End Set
End Property
Public Property ServerIP() As String
Get
Return DBIpServer
End Get
Set(ByVal value As String)
DBIpServer = value
End Set
End Property
Public Property ServerDataBase() As String
Get
Return DBIni
End Get
Set(ByVal value As String)
DBIni = value
End Set
End Property
Public Property ServerPassword() As String
Get
Return DBPass
End Get
Set(ByVal value As String)
DBPass = value
End Set
End Property
Public Property ServerUserName() As String
Get
Return DBUser
End Get
Set(ByVal value As String)
DBUser = value
End Set
End Property
Public Function OpenDBMySql(ByVal pServidorIPDNS As String, ByVal pDBuser As String, ByVal pDBPass As String, _
ByVal pBDD As String, ByVal SqlQuery As String, ByVal MySQLConnectionString As String) _
As MySql.Data.MySqlClient.MySqlConnection
Dim MyADOConnection As MySql.Data.MySqlClient.MySqlConnection
If StrConexion = Nothing And MySQLConnectionString = Nothing Then
MySQLConnectionString = PreStrConexion
Else
If MySQLConnectionString = Nothing Then
MySQLConnectionString = StrConexion
End If
End If
If pServidorIPDNS = Nothing And DBIpServer = Nothing Then
pServidorIPDNS = PREDBIpServer
Else
If pServidorIPDNS = Nothing Then
pServidorIPDNS = DBIpServer
End If
End If
If pDBuser = Nothing And DBUser = Nothing Then
pDBuser = PREDBUser
Else
If pDBuser = Nothing Then
pDBuser = DBUser
End If
End If
If pDBPass = Nothing And DBPass = Nothing Then
pDBPass = PREDBPass
Else
If pDBPass = Nothing Then
pDBPass = DBPass
End If
End If
If pBDD = Nothing And DBIni = Nothing Then
pBDD = PREDBIni
Else
If pBDD = Nothing Then
pBDD = DBIni
End If
End If
MySQLConnectionString = MySQLConnectionString.Replace("(ip)", pServidorIPDNS). _
Replace("(db)", pBDD).Replace("(u)", pDBuser).Replace("(p)", pDBPass)
If Not MyDataAdapter Is Nothing Then
MyDataAdapter = Nothing
End If
If Not MyCommandBuilder Is Nothing Then
MyCommandBuilder = Nothing
End If
Try
MyADOConnection = New MySql.Data.MySqlClient.MySqlConnection(MySQLConnectionString)
MyDataAdapter = New MySqlDataAdapter(SqlQuery, MyADOConnection)
MyCommandBuilder = New MySqlCommandBuilder(MyDataAdapter)
Return MyADOConnection
Catch ex As Exception
Dim Cancel As Boolean
RaiseEvent ErrorEvent(ex, Cancel)
Return Nothing
End Try
End Function
Public Function ExecuteQuery(ByVal SqlQuery As String, ByVal Tabla As String) As Boolean
Dim MyComandoMysql As New MySqlCommand
Dim MyADOConnection As MySql.Data.MySqlClient.MySqlConnection
If Not SqlQuery = "" Then
PrivSqlQuery = SqlQuery
End If
If Tabla = "" Then
PrivTable = Tabla
End If
MyADOConnection = OpenDBMySql(DBIpServer, DBUser, DBPass, DBIni, PrivSqlQuery, StrConexion)
If Not MyADOConnection Is Nothing Then
Try
MyADOConnection.Open()
MyComandoMysql.Connection = MyADOConnection
MyComandoMysql.CommandType = CommandType.Text
MyComandoMysql.CommandText = SqlQuery
MyComandoMysql.ExecuteNonQuery()
MyADOConnection.Close()
RaiseEvent Finish()
Return True
Catch ex As Exception
Dim Cancel As Boolean
RaiseEvent ErrorEvent(ex, Cancel)
Return False
End Try
Else
RaiseEvent Finish()
Return False
End If
End Function
Public Function GetDataSet(Optional ByVal SqlQuery As String = "", Optional ByVal Tabla As String = "") As DataSet
Dim tmpDataSet As New DataSet
Dim MyComandoMysql As New MySqlCommand
Dim MyADOConnection As MySql.Data.MySqlClient.MySqlConnection
If Not SqlQuery = "" Then
PrivSqlQuery = SqlQuery
End If
If Not Tabla = "" Then
PrivTable = Tabla
End If
MyADOConnection = OpenDBMySql(DBIpServer, DBUser, DBPass, DBIni, PrivSqlQuery, StrConexion)
If Not MyADOConnection Is Nothing Then
Try
MyADOConnection.Open()
MyComandoMysql.Connection = MyADOConnection
MyComandoMysql.CommandType = CommandType.Text
MyComandoMysql.CommandText = PrivSqlQuery
MyComandoMysql.ExecuteNonQuery()
MyDataAdapter.Fill(tmpDataSet, PrivTable)
MyADOConnection.Close()
Catch ex As Exception
Dim Cancel As Boolean
RaiseEvent ErrorEvent(ex, Cancel)
If Cancel Then
RaiseEvent Finish()
Return Nothing
End If
tmpDataSet = Nothing
End Try
PrivDataSet = tmpDataSet
RaiseEvent Finish()
Return tmpDataSet
Else
RaiseEvent Finish()
Return Nothing
End If
End Function
Public Property This_DataBase() As DataSet
Get
Return PrivDataSet
End Get
Set(ByVal value As DataSet)
PrivDataSet = value
End Set
End Property
Public Property This_SqlQuery() As String
Get
Return PrivSqlQuery
End Get
Set(ByVal value As String)
PrivSqlQuery = value
End Set
End Property
Public Property This_Table() As String
Get
Return PrivTable
End Get
Set(ByVal value As String)
PrivTable = value
End Set
End Property
Public Function UpdateFromDataSet(Optional ByVal pDataSet As DataSet = Nothing, _
Optional ByVal SQLQuery As String = "", _
Optional ByVal Tabla As String = "") _
As Boolean
If Not pDataSet Is Nothing Then
PrivDataSet = pDataSet
End If
If Not SQLQuery = "" Then
PrivSqlQuery = SQLQuery
End If
If Not Tabla = "" Then
PrivTable = Tabla
End If
Dim MyADOConnection As MySql.Data.MySqlClient.MySqlConnection
MyADOConnection = OpenDBMySql(DBIpServer, DBUser, DBPass, DBIni, PrivSqlQuery, StrConexion)
If Not MyADOConnection Is Nothing Then
Try
MyADOConnection.Open()
MyDataAdapter.Update(PrivDataSet, PrivTable)
MyADOConnection.Close()
RaiseEvent Finish()
Return True
Catch ex As Exception
Dim Cancel As Boolean
RaiseEvent ErrorEvent(ex, Cancel)
RaiseEvent Finish()
Return False
End Try
Else
RaiseEvent Finish()
Return False
End If
End Function
End Class