'Importando MySql e DataSet
Imports MySql.Data.MySqlClient
Imports System.Data.DataSet
Public Class ConexaoMySql
Private mConexao As New MySqlConnection
Private mComando As New MySqlCommand
Private mDataAdap As New MySqlDataAdapter
Private mDataRead As MySqlDataReader
Private usuarioServidor As String
Private usuarioBancoDeDados As String
Private usuarioLogin As String
Private usuarioSenha As String
Public Property ServidorUs() As String
Get
Return usuarioServidor
End Get
Set(ByVal value As String)
usuarioServidor = value
End Set
End Property
Public Property BancoDeDadosUs() As String
Get
Return usuarioBancoDeDados
End Get
Set(ByVal value As String)
usuarioBancoDeDados = value
End Set
End Property
Public Property LoginUs() As String
Get
Return usuarioLogin
End Get
Set(ByVal value As String)
usuarioLogin = value
End Set
End Property
Public Property SenhaUs() As String
Get
Return usuarioSenha
End Get
Set(ByVal value As String)
usuarioSenha = value
End Set
End Property
Sub New(ByVal servidor As String, ByVal usuario As String, ByVal senha As String, ByVal bancoDeDados As String)
ServidorUs = servidor
LoginUs = usuario
SenhaUs = senha
BancoDeDadosUs = bancoDeDados
End Sub
Public Sub ConectarMySql()
If Not mConexao.State = ConnectionState.Open Then
Dim strConexao As String = "Data Source=" + ServidorUs + ";user id=" + LoginUs + ";password=" + SenhaUs + "; database=" + BancoDeDadosUs
mConexao = New MySqlConnection()
mConexao.ConnectionString = strConexao
mConexao.Open()
End If
End Sub
Public Sub DesconectarMySql()
If mConexao.State = ConnectionState.Open Then
mConexao.Close()
mConexao.Dispose()
mConexao = Nothing
End If
End Sub
Public Function ExecutaDataTable(ByVal sql As String) As DataTable
Dim mDataTable As New DataTable
Try
ConectarMySql()
mComando.CommandType = CommandType.Text
mComando.CommandText = sql
mComando.Connection = mConexao
mDataAdap.SelectCommand = mComando
mDataAdap.Fill(mDataTable)
mDataAdap.Dispose()
Return mDataTable
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return mDataTable
End Function
Public Function ExecutaDataRead(ByVal sql As String) As MySqlDataReader
mDataRead = Nothing
Try
ConectarMySql()
mComando.CommandType = CommandType.Text
mComando.CommandText = sql
mComando.Connection = mConexao
mDataRead = mComando.ExecuteReader()
mComando.Dispose()
Return mDataRead
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return mDataRead
End Function
Public Function ExecutaQuery(ByVal sql As String) As MySqlCommand
Try
ConectarMySql()
mComando.CommandType = CommandType.Text
mComando.CommandText = sql
mComando.Connection = mConexao
Return mComando
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return mComando
End Function
End Class
Obs.: Todos os exemplos com a Classe VB.net Mysql, devem possuir a referência do MySqlConnector, e pode ser baixada aqui. Após baixar é só adicionar a referência da biblioteca, e incluir no começo do código:
Imports MySql.Data.MySqlClient
1 comentários:
Valeu Herbert, boa classe!!
Com certeza é muito útil.
Bom blog!
Postar um comentário