Sunteți pe pagina 1din 4

Connecting VB.

NET to MySQL

1. Install first the drivers and software we need.


mysql-connector-net-6.6.2
mysql-connector-odbc-5.1.8-win32
2. Create database.
Database Name: bsit2f
Table Structures:
Table 1 Name: uaccount
Fields: userID [ INT(11) ] Auto_Inc PRIMARY KEY
userName [ VARCHAR(25) ] PRIMARY KEY
userPass [ VARCHAR(25) ]
Table 2 Name: student
Fields: studNo [ VARCHAR(20) ] - PRIMARY
studName [ VARCHAR(30) ]
studLocation [ VARCHAR(30) ]
studAge [ INT(3) ]
3. Open VB.NET and follow the given form design and codes.

Add module and type the ff. code.


Module1.vb
Imports MySql.Data.MySqlClient
Module Module1
Public
Public
Public
Public
Public
Public

objConn As New MySqlConnection


objCmd As New MySqlCommand
strSQL, strReportName As String
acsDS As New DataSet
objDA As New MySqlDataAdapter
objDR As MySqlDataReader

End Module
DATABASE SERVER LOGIN:
Form1.vb (Design)

TextBox1
TextBox2
TextBox3

Form1.vb
Code for Connect button:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
objConn.ConnectionString = "Server=" & TextBox1.Text & "; User ID=" & TextBox2.Text & ";Password=" &
TextBox3.Text & ";Database=bsit2f"
Try
objConn.Open()
MsgBox("Connected")
Me.Hide()
Form2.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

USER ACCOUNT LOGIN:


Form2.vb (Design)

TextBox1
TextBox2

Form2.vb
Code for Login button:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
strSQL = "SELECT * FROM uaccount WHERE userName='" & TextBox1.Text & "' AND userPass='" &
TextBox2.Text & "'"
objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
objDR = objCmd.ExecuteReader
If (objDR.Read()) Then
MsgBox("Welcome")
objCmd.Dispose()
objDR.Close()
TextBox1.Text = ""
TextBox2.Text = ""
Me.Hide()
Form3.Show()
Else
MsgBox("Invalid Username or Password")
End If
objCmd.Dispose()
objDR.Close()
End Sub
Form3.vb (Design)

TextBox5
TextBox1
TextBox2
TextBox3
TextBox4

ListView1

Set the properties of Listview


Click [columns] under properties window
Add 4 columns and change the text of each column based on your field names. Ex.(Student
No.,Student Name,Location, Age)
Change the ff. properties:
FullRowSelect = True
GridLines = True
View = Details

Form3.vb
Create a function for populating records in Listview:

Type the ff. code:


Sub fillListview()
Try
ListView1.Items.Clear()
strSQL = "SELECT * FROM student"
objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL, objConn)
objDR = objCmd.ExecuteReader
While (objDR.Read())
With ListView1.Items.Add(objDR("studNo"))
.subitems.add(objDR("studName"))
.subitems.add(objDR("studLocation"))
.subitems.add(objDR("studAge"))
End With
End While
objCmd.Dispose()
objDR.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Code for Listview1: (change the event in DoubleClick)
Private Sub ListView1_DoubleClick(sender As Object, e As EventArgs) Handles ListView1.DoubleClick
With ListView1.SelectedItems(0)
TextBox1.Text = .SubItems(0).Text
TextBox2.Text = .SubItems(1).Text
TextBox3.Text = .SubItems(2).Text
TextBox4.Text = .SubItems(3).Text
End With
End Sub

Code for Form3:


Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.fillListview()
End Sub
Code for Insert button:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
strSQL = "INSERT INTO student VALUES" _
& "('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
objCmd.Dispose()
MsgBox("RECORD SUCCESSFULLY SAVED", MsgBoxStyle.Information, "ADD RECORD")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Me.fillListview()
End Sub
Code for Update button:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
strSQL = "UPDATE student SET studNo='" & TextBox1.Text & "',studName='" & TextBox2.Text &
"',studLocation='" & TextBox3.Text & "',studAge='" & TextBox4.Text & "' WHERE studNo='" & TextBox1.Text
& "'"
objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
objCmd.Dispose()
MsgBox("RECORD SUCCESSFULLY UPDATED", MsgBoxStyle.Information, "UPDATE RECORD")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""

TextBox4.Text = ""
Me.fillListview()
End Sub
Code for Delete button:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
strSQL = "DELETE FROM student WHERE studNo='" & TextBox1.Text & "'"
objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
objCmd.Dispose()
MsgBox("RECORD SUCCESSFULLY DELETED", MsgBoxStyle.Information, "DELETE RECORD")
TextBox1.Text
TextBox2.Text
TextBox3.Text
TextBox4.Text

=
=
=
=

""
""
""
""

Me.fillListview()
End Sub
Code for Search:
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
Try
ListView1.Items.Clear()
strSQL = "SELECT * FROM student WHERE studName LIKE '" & TextBox5.Text & "%'"
objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL, objConn)
objDR = objCmd.ExecuteReader
While (objDR.Read())
With ListView1.Items.Add(objDR("studNo"))
.subitems.add(objDR("studName"))
.subitems.add(objDR("studLocation"))
.subitems.add(objDR("studAge"))
End With
End While
objCmd.Dispose()
objDR.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Code for Clear button:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End Sub
Code for Exit button:
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
End
End Sub
Code for Logout button:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Me.Hide()
Form2.Show()
End Sub

S-ar putea să vă placă și