0% found this document useful (0 votes)
49 views

Database On: By: Engr. Michael B. Tomas

This document provides code samples for connecting a VB.NET application to a Microsoft Access database and performing basic CRUD (create, read, update, delete) operations. It includes code for connecting to the database, searching for records, adding new records, deleting records, and loading records from the database into a list view. The code samples show how to define connection strings, commands, and data adapters to execute queries and manipulate data in the database.

Uploaded by

Hayeon Kim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Database On: By: Engr. Michael B. Tomas

This document provides code samples for connecting a VB.NET application to a Microsoft Access database and performing basic CRUD (create, read, update, delete) operations. It includes code for connecting to the database, searching for records, adding new records, deleting records, and loading records from the database into a list view. The code samples show how to define connection strings, commands, and data adapters to execute queries and manipulate data in the database.

Uploaded by

Hayeon Kim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

DATABASE ON

VB.NET
By: Engr. Michael B. Tomas
WHAT WE NEED?
 Visual Basic .Net (Visual Studio)
 Database (Microsoft Access, SQL etc…)
INSIDE THE CODE.
 Connection Code
 Search Code
 Add Code
 Delete Code
 Update or Edit
CREATING
DATABASE
MICROSOFT ACCESS
 Create an Access Database
START CODING
IMPORTS AND VARIABLE
NAMES
 Before the Main Class
Imports System.Data.OleDb
Imports System.Data
 After the Main Class
Public cn As New OleDbConnection()
Public dr As OleDbDataReader
Public cm As OleDbCommand
Public Found As Boolean
Public ID, Username, Password, FirstName, LastName As String
Dim sql As String
Public i As Integer
Dim adapter As OleDb.OleDbDataAdapter
ReadOnly dt As DataTable = New DataTable
CONNECTION CODE
Public Sub modConnect()
cn = New OleDb.OleDbConnection
With cn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Application.StartupPath & "\dbUsers.mdb"
.Open()
End With
End Sub
SEARCH CODE
Public Sub modSearch()
Try
OpnCon()
cm = New OleDb.OleDbCommand
With cm
.Connection = cn
.CommandType = CommandType.Text
.CommandText = "SELECT * FROM tblUsers WHERE (Username = ‘” & _
Username & "')"
dr = .ExecuteReader
End With
SEARCH CODE
While dr.Read()
ID = dr("ID").ToString
Username = dr("Username").ToString
Password = dr("Password").ToString
FirstName = dr("FirstName").ToString
LastName = dr("LastName").ToString
Found = True
End While
SEARCH CODE
cn.Close()
dr.Close()
Catch ex As Exception
MsgBox(ex.Message, vbCritical, "Error")
End Try
End Sub
ADD CODE
Public Sub modAdd()

OpnCon()
sql = "INSERT INTO tblUsers ([Username],[Password],[FirstName],[LastName]) _
VALUES ('" & Username & "', '" & Password & "','" & FirstName & "','" & _
LastName & "')"
cm.Connection = cn
cm.CommandText = sql

i = cm.ExecuteNonQuery

End Sub
DELETE CODE
Public Sub modDelete()
Try
OpnCon()
sql = "Delete * FROM tblUsers WHERE ID= " & ID & ""
cm.Connection = cn
cm.CommandText = sql

i = cm.ExecuteNonQuery
DELETE CODE
Catch ex As Exception
MsgBox(ex.Message, vbCritical, "Error")
Finally
cn.Close()
'modLoadRecord()
End Try
End Sub
SOME NEEDED CODES
 Connection Checker

Public Sub OpnCon()


If cn.State = ConnectionState.Closed Then
cn.Open()
End If
End Sub
SOME NEEDED CODES
 Record Loader to ListView

Public Sub modLoadRecord()


Dim sql As String = "SELECT * From tblUsers"
cm = New OleDb.OleDbCommand(sql, cn)

Try
adapter = New OleDb.OleDbDataAdapter(cm)
adapter.Fill(dt)
SOME NEEDED CODES
 Record Loader to ListView (Cont..)

For Each row In dt.Rows


Populate(row(1), row(3), row(4))
Next

dt.Rows.Clear()
cn.Close()
SOME NEEDED CODES
 Record Loader to ListView (Cont..)

Catch ex As Exception
MsgBox(ex.Message, vbCritical, "Error")
End Try
End Sub
POPULATE CODE
Private Sub Populate(Username As String, FirstName As String, LastName As String)
'Row array
Dim row As String() = New String() {Username, LastName, FirstName}
Dim items As ListViewItem = New ListViewItem(row)

'Add to row collection


frmUserList.ListView1.Items.Add(items)
End Sub
END OF SLIDE…

You might also like