[vb.NET] - Pilote ODBC pour connexion à une base MySQL

Avatar de l’utilisateur
LM2I_Laurent
Administrateur - Site Admin
Messages : 246
Inscription : jeu. 27 août 2009 17:00

[vb.NET] - Pilote ODBC pour connexion à une base MySQL

Messagepar LM2I_Laurent » lun. 12 janv. 2009 00:46

Voici un lien d'installation pour le pilote ODBC d'acces a une base MySQL:

Lien

Celui-ci vous permet de vous connecter à une base MySQL sur un serveur distant...


@+
Laurent
Image
Webmaster
http://www.lm2i.fr
http://www.lm2i.eu

Image Image Image Image
.

Avatar de l’utilisateur
LM2I_Laurent
Administrateur - Site Admin
Messages : 246
Inscription : jeu. 27 août 2009 17:00

Re: Module VB pour vous connecter à MySQL

Messagepar LM2I_Laurent » lun. 12 janv. 2009 00:54

Ci joint le module à intégrer à votre projet :

Lien

Code : Tout sélectionner

Option Explicit On 

Module MySQL
    Public Server As String = "Adresse IP"
    Public User As String = "Utilisateur"
    Public Pwd As String = "Mot de Passe"
    Public Connexion As ADODB.Connection
    Public Rs As ADODB.Recordset
    Public DataId As Integer


    Public Function MySQL_Conn(ByVal Db As String) As ADODB.Connection

        'Connect to database
        MySQL_Conn = New ADODB.Connection()
        MySQL_Conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
                                    & "SERVER=" & Server & ";" _
                                    & "DATABASE=" & Db & ";" _
                                    & "UID=" & User & ";" _
                                    & "PWD=" & Pwd & ";" _
                                    & "OPTION=3"


        MySQL_Conn.Open()

    End Function

    Public Sub MySQL_NewDb(ByRef MySQL_Conn As ADODB.Connection, ByVal Db_Name As String)

        'New database
        MySQL_Conn.Execute("DROP DATA BASE IF EXISTS " & Db_Name)
        MySQL_Conn.Execute("CREATE DATABASE " & Db_Name)

    End Sub

    Public Sub MySQL_NewTable(ByRef MySQL_Conn As ADODB.Connection, ByVal TableName As String, ByVal Attrib As String)

        'New table
        MySQL_Conn.Execute("DROP TABLE IF EXISTS " & TableName)
        MySQL_Conn.Execute("CREATE TABLE " & TableName & "(" & Attrib & ")")

    End Sub

    Public Function MySQL_Select(ByRef MySQL_Conn As ADODB.Connection, ByVal Fields As String, ByVal Table As String, Optional ByVal Where As String = "", Optional ByVal Group As String = "", Optional ByVal Order As String = "") As ADODB.Recordset

        Dim strSQL As String

        'Select string
        strSQL = "SELECT " & Fields & " FROM `" & Table & "`"
        If Where <> "" Then strSQL = strSQL & " WHERE " & Where
        If Group <> "" Then strSQL = strSQL & " GROUP BY " & Group
        If Order <> "" Then strSQL = strSQL & " ORDER BY " & Order
        MySQL_Select = New ADODB.Recordset()
        MySQL_Select.CursorLocation = ADODB.CursorLocationEnum.adUseServer
        MySQL_Select.Open(strSQL, MySQL_Conn)

    End Function

    Public Function MySQL_SelectR(ByRef MySQL_Conn As ADODB.Connection, ByVal strSQL As String) As ADODB.Recordset

        'Select string
        MySQL_SelectR = New ADODB.Recordset()
        MySQL_SelectR.CursorLocation = ADODB.CursorLocationEnum.adUseServer
        MySQL_SelectR.Open(strSQL, MySQL_Conn)

    End Function

    Public Sub MySQL_Insert(ByRef MySQL_Conn As ADODB.Connection, ByVal Table As String, ByVal Fields As String, ByVal Values As String)

        'Insert data
        MySQL_Conn.Execute("INSERT INTO `" & Table & "` (" & Fields & ") VALUES (" & Values & ")")

    End Sub

    Public Sub MySQL_Delete(ByRef MySQL_Conn As ADODB.Connection, ByVal Table As String, ByVal WhereField As String, ByVal WhereValue As String)

        'Delete data
        MySQL_Conn.Execute("DELETE FROM `" & Table & "` WHERE " & WhereField & " =" & WhereValue)

    End Sub

    Public Sub MySQL_Update(ByRef MySQL_Conn As ADODB.Connection, ByVal Table As String, ByVal Fields As String, ByVal WhereFields As String)

        'Update data
        MySQL_Conn.Execute("UPDATE `" & Table & "` SET " & Fields & " WHERE " & WhereFields)

    End Sub

    Public Sub Rs_Close()
        'Close recordset
        If Not (Rs Is Nothing) Then
            If Rs.State = 1 Then Rs.Close()
        End If
    End Sub
End Module


:wink:


@+
Laurent
Image
Webmaster
http://www.lm2i.fr
http://www.lm2i.eu

Image Image Image Image
.


Qui est en ligne ?

Utilisateurs parcourant ce forum : Aucun utilisateur inscrit et 1 invité