Вы находитесь на странице: 1из 24

CICS 314: Advanced Visual Basic

.NET Programming

Coding the Connections and Commands


- Using OLE DB and SQL Data Sources -
Ghana Technology University College
Lecturer – Dr. Forgor Lempogo
2020
Procedure.
 The following shows how to communicate with
databases.

1. we need a connection to the data source, provided by


a Connection object.
 The Connection object requires certain information for it to connect to
the data source.

2. The Connection object is called by the DataAdapter


object, which handles commands to select, update,
insert, and delete data in the data source.

3. The DataSet contains the tables and uses the


DataAdapter to populate itself and to update
information in the data source.
4
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
General Program Structure:
 In form-level declaration section:

 Declare objects for a database connection, dataAdapter and a dataSet

 Assume the DataGridView is defined within the Form Design view

 In the Sub Form_Load event:

 Specify the connection string

 Create the connection

 Create new dataAdapter specifying the SelectCommand

 Fill the dataSet from the dataAdapter

 Bind the DataGridView to the dataSet

5
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Creating the Project

Create a new Windows application in


Visual Studio

Design the user interface

Copy the database into the debug folder

Write the code

7
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
The User Interface

CICS 314: Advanced Visual Basic .NET


8 Programming - GTUC 2020 Delivery
Declaring Public Variables
Imports System.Data.OleDb

Public Class Form1

Dim con As New OleDb.OleDbConnection


Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Dim conString As String
Dim inc As Integer
Dim maxrows As Integer
Dim dsNewRow As DataRow

10
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Declaring Public Variables -SQL
Imports System.Data.SqlClient

Public Class Form1

Dim con As New SqlClient.SqlConnection


Dim ds As New DataSet
Dim da As New SqlClient.DataAdapter
Dim sql As String
Dim conString As String
Dim inc As Integer
Dim maxrows As Integer
Dim dsNewRow As DataRow

11
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Linking Form to Database – Form_Load
Try
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=‘ &
System.Environment.CurrentDirectory.ToString() & "\AddressBook.mdb’"

con = New OleDbConnection(conString)


con.Open()
sql = "select * from tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, “AddressBook")
con.Close()
Catch EXP As Exception
 MessageBox.Show(EXP.ToString)
End Try

NavigateRecords()
btnCommit.Enabled = False

12
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Linking Form to Database – Form_Load -
Try SQL
‘conString = "Provider='SQLOleDB'; Data Source=SQLServerName; Initial Catalog=dbName;
User ID=myusername; Password=mypassword;"

conString = “Provider =‘System.Data.SqlClient’; Data Source=localhost\sqlexpress;


Initial Catalog=myDBname; Integrated Security=True“

con = New SqlClient.SqlConnection(conString)


con.Open()
sql = "select * from tblContacts"
da = New SqlClient.SqlDataAdapter(sql, con)
da.Fill(ds, “AddressBook")
con.Close()
Catch EXP As Exception
 MessageBox.Show(EXP.ToString)
End Try

NavigateRecords()
btnCommit.Enabled = False

13
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Navigating Records Sub Procedure
 Private Sub NavigateRecords()

 txtFName.Text = ds.Tables("AddressBook").Rows(inc).Item(1)
 txtSName.Text = ds.Tables("AddressBook").Rows(inc).Item(2)
 txtAddress1.Text = ds.Tables("AddressBook").Rows(inc).Item(3)
 txtAddress2.Text = ds.Tables("AddressBook").Rows(inc).Item(4)
 txtAddress3.Text = ds.Tables("AddressBook").Rows(inc).Item(5)
 txtPostCode.Text = ds.Tables("AddressBook").Rows(inc).Item(6)
 txtPhone.Text = ds.Tables("AddressBook").Rows(inc).Item(7)
 txtEmail.Text = ds.Tables("AddressBook").Rows(inc).Item(8)
 txtNotes.Text = ds.Tables("AddressBook").Rows(inc).Item(9)
 maxrows = ds.Tables("addressbook").Rows.Count

 End Sub

14
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Moving to First Record

 If inc <> 0 Then


 inc = 0
 NavigateRecords()
 Else
 MessageBox.Show(“This is First Record”)
 End If

15
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Moving to the Next Record

 If inc <> maxrows - 1 Then


 inc = inc + 1
 NavigateRecords()
 Else
 MessageBox.Show("No Rec")
 End If

16
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Moving to the Previous Record

 If inc <> 0 Then


 inc = inc - 1
 NavigateRecords()
 Else
 MessageBox.Show("No Rec")
 End If

17
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Moving To The Last Record
 Try
 If inc <> maxrows - 1 Then
 inc = maxrows - 1
 NavigateRecords()
 Else
 MessageBox.Show(“This is Last Record”)
 End If

 Catch EXP As Exception


 MessageBox.Show(EXP.ToString)
 End Try
18
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Adding Records To Database
Public Sub AddRecords()
dsNewRow.Item("FirstName") = txtFName.Text
dsNewRow.Item("Surname") = txtSName.Text
dsNewRow.Item("Address1") = txtAddress1.Text
dsNewRow.Item("Address2") = txtAddress2.Text
dsNewRow.Item("Address3") = txtAddress3.Text
dsNewRow.Item("PostCode") = txtPostCode.Text
dsNewRow.Item("Phone") = txtPhone.Text
dsNewRow.Item("Email") = txtEmail.Text
dsNewRow.Item("Notes") = txtNotes.Text
End Sub
19
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Clear Records from Textboxes
 Public Sub ClearRecords()
txtFName.Text = “”
txtSName.Text= “”
txtAddress1.Text = “”
txtAddress2.Text = “”
txtAddress3.Text = “”
txtPostCode.Text = “”
txtPhone.Text = “”
txtEmail.Text = “”
txtNotes.Text = “”
End Sub

20
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Adding New Records to Database
 btnCommit.Enabled = True
 btnAddNew.Enabled = False
 btnUpdate.Enabled = False
 btnDelete.Enabled = False
 Call ClearRecords()
 MessageBox.Show("please fill in
information and commit", "Info required")

21
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Commit Records To Database
Dim cb As New OleDb.OleDbCommandBuilder(da)
Try
dsNewRow = ds.Tables("AddressBook").NewRow()
Call AddRecords()
ds.Tables("AddressBook").Rows.Add(dsNewRow)
da.Update(ds, "AddressBook")
MessageBox.Show("New Record added to the Database")
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True
Catch EXP As Exception
MessageBox.Show(EXP.ToString)
End Try

22
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Updating Records In A Database
Dim cb As New OleDb.OleDbCommandBuilder(da)
Try
ds.Tables("AddressBook").Rows(inc).Item(1) = txtFName.Text
ds.Tables("AddressBook").Rows(inc).Item(2) = txtSName.Text
ds.Tables("AddressBook").Rows(inc).Item(3) = txtAddress1.Text
ds.Tables("AddressBook").Rows(inc).Item(4) = txtAddress2.Text
ds.Tables("AddressBook").Rows(inc).Item(5) = txtAddress3.Text
ds.Tables("AddressBook").Rows(inc).Item(6) = txtPostCode.Text
ds.Tables("AddressBook").Rows(inc).Item(7) = txtPhone.Text
ds.Tables("AddressBook").Rows(inc).Item(8) = txtEmail.Text
ds.Tables("AddressBook").Rows(inc).Item(9) = txtNotes.Text
da.Update(ds, "AddressBook")
MsgBox("Records successfuly saved", MsgBoxStyle.OkOnly, )
Catch exp As Exception
MessageBox.Show(exp.ToString)
CICS 314: Advanced Visual Basic .NET
23
End Try - GTUC 2020 Delivery
Programming
Searching For Records
 Dim ID As String
 Static keepID As String
 Dim isFound As Boolean = False
Try
 ID = InputBox("Please Enter ID to search")

 If ID = "" Then
 MessageBox.Show("please enter ID")
 ID = InputBox("Please Enter ID to search")
 Exit Sub
 Else

25
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Searching For Records Cont…
 Do While inc < maxrows - 1
 If ID = ds.Tables(" AddressBook").Rows(inc).Item("ID") Then
 inc = ID - 1
 keepID = inc
 IsFound = True
 Exit Try
 End If
 inc = inc + 1
 Loop
 End Try

 If isFound then
 Call NavigateRecords()
 Else
 MsgBox("Please Record does not exist", MsgBoxStyle.Exclamation, "Non Exisense
of Record")
 Catch EXCP As Exception
 MsgBox(EXCP.ToString)

 End Sub

26
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Deleting Records From Database
 Try
 If MessageBox.Show("Do you really want to Delete this Record?", _
 "Delete", MessageBoxButtons.YesNo, _
 MessageBoxIcon.Warning) = DialogResult.No Then

 MsgBox("Operation Cancelled")
 Exit Sub

 Else
 Dim cb As New OleDb.OleDbCommandBuilder(da)
 ds.Tables(“AddressBook").Rows(inc).Delete()
 maxrows = maxrows - 1
 inc = 0
 NavigateRecords()
 da.Update(ds, “AddressBook")
 clear()
 End If

28
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Any Questions?

29
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery
Any Questions?

42
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2020 Delivery

Вам также может понравиться