Thursday, June 9, 2011

VB.net 2008 and 2010 with MySQL Database

Imports MySql.Data.MySqlClient
Public Class Form1
Dim ServerString As String = "Server=localhost;User Id=root;Password=;Database=grapescorp"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SQLConnection.ConnectionString = ServerString

Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
MsgBox("Successfully Connected To my SQL Database")
Else
SQLConnection.Close()
MsgBox("Connection Close")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SaveNames(ByRef SQLStatement As String)
Dim cmd As MySqlCommand = New MySqlCommand

With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
';.ExecuteReader()


End With
SQLConnection.Close()
MsgBox("Successfully Added!")
SQLConnection.Dispose()


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SQLStatement As String = "Insert Into data(name) Values('" & TextBox1.Text & "')"

SaveNames(SQLStatement)
End Sub
End Class

No comments:

Post a Comment