Log in

View Full Version : Editor SQL sederhana problem(VB),help me please


Darkc0der
20th November 2011, 11:20 PM
Gan,ane mencoba buat editor sql sederhana pake VB,koneksinya udah bagus,fungsi SELECT,DESC dan SHOW udah bisa dijalankan,tpi gmana cara menintegrasikan fungsi USE(maksudnya biar bisa ganti database yg mau diedit)



berikut sintaksnya gan :

Imports MySql.Data.MySqlClient



Public Class Form1



Dim querystatus As Integer = 1

Dim ServerString As String = "Server=Localhost; User Id=root; Database=datacd"

Dim SQLConnection As MySqlConnection = New MySqlConnection



1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.Label3.Text = "Query dijalankan...."

End Sub



2. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Form2.Show()

End Sub



3. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



SQLConnection.ConnectionString = ServerString



Try

If SQLConnection.State = ConnectionState.Closed Then

SQLConnection.Open()

Me.stat.Text = "MySQL tersambung..."

Me.stat.ForeColor = Color.Black

Else

SQLConnection.Close()

End If

Catch ex As Exception

Me.stat.Text = "MySQL tidak tersambung..."

Me.stat.ForeColor = Color.Red

MsgBox(ex.ToString)

End Try

End Sub



4. Private Sub Button1_Click() Handles Button1.Click

Dim SQLStatement As String = Me.TextBox1.Text

Dim cmd As MySqlCommand = New MySqlCommand

Dim querystatus As Integer = 1

Dim adapter As MySqlDataAdapter

Dim table As DataTable

MsgBox(SQLStatement)

If Mid(TextBox1.Text, 1, 6) = "SELECT" Or Mid(TextBox1.Text, 1, 4) = "DESC" Or Mid(TextBox1.Text, 1, 4) = "SHOW" Then

Try

adapter = New MySqlDataAdapter(SQLStatement, ServerString)

table = New DataTable

adapter.Fill(table)

DataGridView1.DataSource = table

Catch ex As Exception

Me.Label3.Text = "Ada kesalahan! Tolong diperbaiki!"

My.Computer.Audio.PlaySystemSound(Media.SystemSoun ds.Asterisk)

Me.Label3.ForeColor = Color.Red

Me.TextBox1.Focus()

querystatus = 0

End Try

Else

EksekusiQuery(SQLStatement)

End If



End Sub



5. Public Sub EksekusiQuery(ByRef SQLStatement As String)

'Dim SQLStatement As String = Me.TextBox1.Text

Dim cmd As MySqlCommand = New MySqlCommand

Dim querystatus As Integer = 1

Dim adapter As MySqlDataAdapter

Dim table As DataTable



Try

If Mid(SQLStatement, 1, 6).ToUpper = "SELECT" Or Mid(SQLStatement, 1, 4).ToUpper = "DESC" Or Mid(SQLStatement, 1, 4).ToUpper = "SHOW" Then



adapter = New MySqlDataAdapter(SQLStatement, ServerString)

table = New DataTable

adapter.Fill(table)

DataGridView1.DataSource = table



Else

With cmd

.CommandText = SQLStatement

.CommandType = CommandType.Text

.Connection = SQLConnection

.ExecuteNonQuery()

End With

End If

Me.Label4.Text = "Query berhasil dijalankan!"

My.Computer.Audio.PlaySystemSound(Media.SystemSoun ds.Exclamation)

Me.Label4.ForeColor = Color.Green

Me.TextBox1.Focus()

querystatus = 1



Catch ex As Exception

Me.Label4.Text = "Terjadi kesalahan pada query anda! Harap periksa kembali!"

My.Computer.Audio.PlaySystemSound(Media.SystemSoun ds.Asterisk)

Me.Label4.ForeColor = Color.Red

Me.TextBox1.Focus()

querystatus = 0

End Try

End Sub



6. Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

querystatus = 1

End Sub

End Class





Itu die gan,sengaja ane ngasi nomor untuk nandai per bagian,kira2 gmana fungsinya gan dan disisipkan di bagian mana(googling gak nemu)?sebelumnya ane mo ngucapin hontou ni arigato gozaimasu :D

</div>