[Visual Basic]Edit Tables in mySQL

Posts 1–9 of 9 · Page 1 of 1
[Visual Basic]Edit Tables in mySQL
Velcomen to editing tbls in mySQL!

First import the mysql connector (download and extract below)
Then Add the reference: Projekt => Add Reference => Browse => Path of mySQL.Data.dll

Now we insert teh imports:
Code:
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports MySql.Data.MySqlClient
Add some dims(under form1):
Code:
Dim conn As MySqlConnection
    Dim data As DataTable
    Dim da As MySqlDataAdapter
    Dim cb As MySqlCommandBuilder
Now we start!
But really, lets start... (It says "Friday!!!!!!!!! Burn the shrimp, burn it)

Add 5 Textboxes & 1 Buten!

Textbox1 : Server URL; URL to connect to the server
Textbox2 : Database Name: Name of the Database
Textbox3 : Database Table: (Just to be more precise, cause I don't like selectin my tables!)
Textbox4 : Database Username: Username used to connect.
Textbox5 : Database Password: Password used to connect.
Button1 : Button that will handle the connection.

If you want, you can make textbox5 have a password char of "•"

Under Button1:

Code:
'Disable Buttens!
TextBox1.Enabled = False
        TextBox2.Enabled = False
        TextBox3.Enabled = False
        TextBox4.Enabled = False
        TextBox5.Enabled = False
        Button1.Enabled = False
        If Not conn Is Nothing Then conn.Close()
        'Server Info
        Dim connStr As String
        connStr = String.Format("server=" & TextBox1.Text & ";user id=" & TextBox4.Text & "; password=" & TextBox5.Text & "; database=" & TextBox2.Text & "; pooling=false", TextBox1.Text, TextBox4.Text, TextBox5.Text)

        Try
            conn = New MySqlConnection(connStr)
            conn.Open()
            'Connection Success
            ShowTbl()
        Catch ex As MySqlException
            'Connection Failed, redo.
            MessageBox.Show("Error connecting to the server: " + ex.Message)
            TextBox1.Enabled = True
            TextBox2.Enabled = True
            TextBox3.Enabled = True
            TextBox4.Enabled = True
            TextBox5.Enabled = True
            Button1.Enabled = True
        End Try
So lets make a new function for ShowTbl()!

Code:
Public Sub ShowTbl()

End Sub
Add a DataGridView called DataGridView1 -.-

Add a butten that will save the database called button2


Yay! or not...


So teh Code under it (Sub ShowTbl()):
Code:
data = New DataTable
        da = New MySqlDataAdapter("SELECT * FROM " + TextBox3.Text, conn)
        cb = New MySqlCommandBuilder(da)
        da.Fill(data)
        DataGridView1.DataSource = data
        data.AcceptChanges()
        TextBox6.Enabled = True
        Button2.Enabled = True
    End Sub
Now For the Final Code! (Butten2):
Code:
Try
            Dim changes As DataTable = data.GetChanges()
            da.Update(changes)
            data.AcceptChanges()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
You will most likely get an error after trying to edit your database, ignore it and reclick the button untill it goes away.

DO NOT DELETE ROWS IT WILL NOT WORK!

Another tutorial will be added about removing rows sometime 2moro.

Tank Joo!
http://www.virustotal.com/file-scan/...7f4-1295505098
http://vscan.novirusthanks.org/file/...2LTMtNi16aXA=/
mysql-connector-net-6.3.6.zipattachment deleted · 18 downloads before removal
Virus scan.
TL;DR just yet, I'll go through it properly tonight. Looks good from what I did read though.
Good Job Son.
Quote Originally Posted by Hassan View Post
Good Job Son.
Thanks Daddy!


Quote Originally Posted by Jason View Post
TL;DR just yet, I'll go through it properly tonight. Looks good from what I did read though.
uhh, ok.
Okay, most of it seems pretty good! Of course there are ways that it could be improved slightly, but that's the case with 90% of code.

Oh yeah

Code:
Imports System
Imports System.Data
Imports System.Windows.Forms


All of these namespaces are imported by default, so no need to manually type them out.
Quote Originally Posted by Jason View Post
Okay, most of it seems pretty good! Of course there are ways that it could be improved slightly, but that's the case with 90% of code.

Oh yeah

Code:
Imports System
Imports System.Data
Imports System.Windows.Forms


All of these namespaces are imported by default, so no need to manually type them out.
Just felt like adding them to the imports list.
Moved to Tutorials section. Should be here. Visual Basic section is not for tutorials, it is for Closed application releases and questions.

Open Source Applications goto Open source, Snippets to snippets vault, tuts to tutorial section and tut requests to tut request section.

There is a reason for the sub sections, use them please & thank you.
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Talk with us