Salut,
Je me connecte à la une base MySQL sans problèmes avec la librairie suivante :

J'ai créé une classe qui gère la relation avec la BDD. Je te mets le constructeur et deux exemple de requête :
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
|
Imports MySql.Data.MySqlClient
''' <summary>
''' Gère la relation avec la BDD de Synoptic et regroupe l'ensmeble des méthodes de lecture et d'écriture
''' </summary>
Public Class BDDMySQL
#Region " Champs "
Public Connection As New MySqlConnection
Public Chaine_connection As String = "server=srv;user id=root;persistsecurityinfo=True;database=synoptictest;password=1proges"
Public TestConnection As Boolean = False
#End Region
#Region " Constructeur "
''' <summary>
''' Gère la création d'un nouvel objet
''' </summary>
Sub New()
Try
Connection.ConnectionString = Chaine_connection
Connection.Open()
TestConnection = True
Connection.Close()
Catch Erreur As Exception
MsgBox("Une erreur empêche la connection SQL :" & Chr(10) & Erreur.Message & Chr(10) & Chr(10) & "L'ensembles des fonctionalités qui requierts la connection SQL seront inaccessibles.", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly, "Erreur SQL")
TestConnection = False
End Try
End Sub
#End Region
#Region " Methodes "
''' <summary>
''' Recherche la famille achat d'un article à partir de son Code_secondaire
''' </summary>
''' <param name="Code_secondaire"></param>
''' <returns>Identifiant de la famille d'achat as String</returns>
Public Function Recherche_Famille_achat(ByRef Code_secondaire As String) As String
Using Connection
Dim Commande_SQL_Etat_article As New MySqlCommand("select artachat.FAM_ACHAT from artachat left outer join article on artachat.ARTICLE = article.ARTICLE where (article.CODE_2=@Code_secondaire);", Connection)
Dim Adapter_SQL As New MySqlDataAdapter
Dim Data_SQL As MySqlDataReader
Dim Resultat As String
Try
Connection.Open()
Commande_SQL_Etat_article.Parameters.AddWithValue("@Code_secondaire", Code_secondaire)
Adapter_SQL.SelectCommand = Commande_SQL_Etat_article
Data_SQL = Commande_SQL_Etat_article.ExecuteReader()
Data_SQL.Read()
If Data_SQL.HasRows = True Then
If Data_SQL.IsDBNull(0) = False Then
Resultat = Data_SQL.GetString("FAM_ACHAT")
Commande_SQL_Etat_article.Parameters.Clear()
Data_SQL.Close()
Connection.Close()
Return Resultat
End If
End If
Commande_SQL_Etat_article.Parameters.Clear()
Data_SQL.Close()
Connection.Close()
Return ""
Catch ex As Exception
MsgBox("Erreur de la fonction Recherche_Famille_achat : " & ex.Message, MsgBoxStyle.Critical, "Erreur BDD Synoptic")
Connection.Close()
Return ""
End Try
End Using
End Function
''' <summary>
''' Recherche la révision d'un article à partir de son Code_secondaire
''' </summary>
''' <param name="Code_article"></param>
''' <returns></returns>
Public Function Recherche_Revision(ByRef Code_article As String) As Integer
Using Connection
Dim Commande_SQL_Recherche_Revision As New MySqlCommand("SELECT MAX(INDICE) FROM fichetech WHERE (ARTICLE=@Code_article);", Connection)
Dim Adapter_SQL As New MySqlDataAdapter
Dim Data_SQL As MySqlDataReader
Dim Resultat As Integer
Try
Connection.Open()
Commande_SQL_Recherche_Revision.Parameters.AddWithValue("@Code_article", Code_article)
Adapter_SQL.SelectCommand = Commande_SQL_Recherche_Revision
Data_SQL = Commande_SQL_Recherche_Revision.ExecuteReader()
Data_SQL.Read()
If Data_SQL.HasRows = True Then
Resultat = Data_SQL.GetInt32("MAX(INDICE)")
Commande_SQL_Recherche_Revision.Parameters.Clear()
Data_SQL.Close()
Connection.Close()
Return Resultat
Else
Commande_SQL_Recherche_Revision.Parameters.Clear()
Data_SQL.Close()
Connection.Close()
Return Nothing
End If
Catch ex As Exception
MsgBox("Erreur de la fonction Recherche_Revision : " & ex.Message, MsgBoxStyle.Critical, "Erreur BDD Synoptic")
Connection.Close()
Return Nothing
End Try
End Using
End Function
[...] |
Il suffit ensuite de créer un nouvel objet au lancement de l'application :
Dim MyBDDMySQL As BDDMySQL
Ensuite tu pourras accèder à l'ensemble des méthodes de la classe :
MyBDDMySQL.NomDeMaMethode
J'espère t'avoir aider. 
Belle journée.
Partager