Senin, 15 Oktober 2018

Membuat koneksi dinamis pada vb 2010 dengan mysql

1.Buat modul dengan source code seperti di bawah ini :
Imports MySql.Data.MySqlClient
Module Module1
    Dim server As String = "Server=" & Formkoneksi.TextBox1.Text & ";"
    Dim db As String = "Database=" & Formkoneksi.TextBox2.Text & ";"
    Dim user As String = "User=" & Formkoneksi.TextBox3.Text & ";"
    Dim password As String = "Password=" & Formkoneksi.TextBox4.Text & ";"
    Dim Constring = server & db & user & password & "default command timeout=3600; Allow User Variables=true"


    Public Database As New MySqlConnection
    Public Tampil As New MySql.Data.MySqlClient.MySqlCommand
    Public Tampilkan As MySql.Data.MySqlClient.MySqlDataReader
    Public Grid As New MySql.Data.MySqlClient.MySqlDataAdapter
    Public Ds As New DataSet
    Public Tabel As String
    Public record As New BindingSource
    Public Sub konek()
        Try
            Database.Close()
            Database.Open()
        Catch ex As Exception
            MsgBox("Koneksi Gagal")
            Database.Close()
            Formkoneksi.Show()
            Formkoneksi.TextBox1.Clear()
            Formkoneksi.TextBox2.Clear()
            Formkoneksi.TextBox3.Clear()
            Formkoneksi.TextBox4.Clear()

        End Try
    End Sub

    Public Sub Koneksi()
        Try
            Database.Close()
            Database.Open()
        Catch ex As Exception
            MsgBox("Koneksi Gagal")

        End Try
    End Sub
End Module

2.Kemudian buat tampilan form seperti berikut :

Source code untuk button Ok :

Dim server As String = "Server=" & TextBox1.Text & ";"
        Dim db As String = "Database=" & TextBox2.Text & ";"
        Dim user As String = "User=" & TextBox3.Text & ";"
        Dim password As String = "Password=" & TextBox4.Text & ";"
        Dim Constring = server & db & user & password & "default command timeout=3600; Allow User Variables=true"
        Database.ConnectionString = Constring

        Try
            If Database.State <> ConnectionState.Open Then
                Database.Open()
                Call Module1.konek()
                Tampil.Connection = Module1.Database
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "select * from ms_pegawai"
                Tampilkan = Tampil.ExecuteReader

                If Tampilkan.HasRows Then
                    Me.Hide()
                    Home.Show()

                ElseIf Not Tampilkan.HasRows Then
                    Me.Hide()
                    Pegawai.Show()
                End If

            End If

        Catch ex As Exception

            MsgBox("Koneksi Gagal")

            Me.Show()
            TextBox1.Clear()
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox1.Focus()


        End Try
Bagaimana reaksi Anda tentang artikel ini?

1 komentar:

https://www.youtube.com/watch?v=ldzUWdct2bQ&t=54s

app gratis