Sei sulla pagina 1di 17

LAB 8: ADO.

NET B A
Dur ration : ing omes (CL LO4, CLO LD2) O5: Learni Outco
By the en of this lab nd boratory session, you sh hould be able to: e 1. Create and open a connection to a a data e n abase 2. Create read, upda and dele records in a database e, ate ete n e 3. Use th Data Form Wizard to create a sim he m mple data ac ccess applica ation 3. Displa and modif data extracted from a database ay fy Hardwar Software Latest ver re/ e: rsion of Micro osoft Visual Studio

2 Ho ours

Activit 8A ty
Activity O Outcome: Cr reating a dat tabase using Microsoft A g Access This activ will crea a databas AddressBook.mdb w a table Login. vity ate se with Step 1: C Create a dat tabase in Ms Access. s Step 2: O Open Ms Ac ccess New Blank Da w atabase File New Data abase File Name e Step 3: S Save the File Name as AddressBo e ook.mdb in your prefere ence location example: D:\Addre essBook.md db Step 4: T Then click Create. You w get the re will esult as sho own below af you click Create fter k

Step 4: Double Click at Create table in Design View.

Step 5: Fill in the field name and data type as shown

Step 6: Click Close

. Then it will pop a message as shown below. Click Yes.

Step 7: Save as tblContacts and click OK.

Step 8: Then it will pop up another message. Click Yes.

Now you already have a table tblContacts.

Step 9: Double Click tblContacts and you will get something like shown below

Congrats!! You have successfully created your own database as AddressBook.mdb with a table tblContacts. Now we move on to create an interface so that we can integrate the interface with the database.

Activit 8B ty
Activity O Outcome: Cr reating an ap pplication tha will integr at rate the data abase and ADO.NET This activ will crea an interfa with sev vity ate ace veral labels, buttons with the usage of the data form h wizard to create simp data acc o ple cess applicat tion. Create a new project as LAB8 and save it in your preferred location ex w d xample D: Step 1: C Step 2: C Create an int terface as sh hown below in form1. Name it as frm mRegistrati ion:

Step 3: S the name for every o Set e object used i the frmRe in egistration a as:

Object
TextBox1 1 TextBox2 2

Name
txtFirstName txtSurN Name btnAdd d btnCom mmit btnUpd date btnDele ete btnCan ncel btnFirst BtnPrevious

BtnNext btnLast btnClick

Step 4: Write the program code as followed

Imports System.Data.OleDb Public Class frmRegister

Dim Dim Dim Dim Dim Dim Dim Dim

inc As Integer MaxRows As Integer con As New OleDb.OleDbConnection dbProvider As String dbSource As String ds As New DataSet da As OleDb.OleDbDataAdapter sql As String

Private Sub frmRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" dbSource = "Data Source = D:\AddressBook.mdb" con.ConnectionString = dbProvider & dbSource con.Open()

sql = "SELECT * FROM tblContacts" da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "AddressBook") con.Close() MaxRows = ds.Tables("AddressBook").Rows.Count inc = -1 End Sub Private Sub navigateRecords() txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1) txtLastName.Text = ds.Tables("AddressBook").Rows(inc).Item(2) End Sub Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click If inc <> MaxRows - 1 Then

inc = inc + 1 navigateRecords() Else MsgBox("No More Rows") End If End Sub Private Sub BtnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrevious.Click If inc > 0 Then inc = inc - 1 navigateRecords() ElseIf inc = -1 Then MsgBox("No Records Yet") ElseIf inc = 0 Then MsgBox("First Record") End If End Sub Private Sub BtnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLast.Click If inc <> MaxRows - 1 Then inc = MaxRows - 1 navigateRecords() End If End Sub Private Sub BtnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFirst.Click If inc <> 0 Then inc = 0 navigateRecords() End If End Sub Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click Dim cb As New OleDb.OleDbCommandBuilder(da) ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text ds.Tables("AddressBook").Rows(inc).Item(2) = txtLastName.Text

da.Update(ds, "AddressBook") MsgBox("Data updated successfully") End Sub Private Sub BtnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCommit.Click If inc <> -1 Then Dim cb As New OleDb.OleDbCommandBuilder(da) Dim dsNewRow As DataRow

dsNewRow = ds.Tables("AddressBook").NewRow() dsNewRow.Item("FirstName") = txtFirstName.Text dsNewRow.Item("SurName") = txtLastName.Text

ds.Tables("AddressBook").Rows.Add(dsNewRow) da.Update(ds, "AddressBook") MsgBox("New Record added to the database") BtnCommit.Enabled = False BtnAdd.Enabled = True BtnUpdate.Enabled = True BtnDelete.Enabled = True txtFirstName.Clear() txtLastName.Clear()

End If End Sub Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click Dim cb As New OleDb.OleDbCommandBuilder(da)

If MessageBox.Show("Do you really want to Delete this Record?", _ "Delete", MessageBoxButtons.YesNo, _ MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then ds.Tables("AddressBook").Rows(inc).Delete() MaxRows = MaxRows - 1 da.Update(ds, "AddressBook") txtFirstName.Clear() txtLastName.Clear() Else MsgBox("Operation Cancelled") Exit Sub End If End Sub Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click BtnCommit.Enabled = False BtnAdd.Enabled = True BtnUpdate.Enabled = True BtnDelete.Enabled = True

txtFirstName.Clear() txtLastName.Clear()

End Sub Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click BtnCommit.Enabled = True BtnAdd.Enabled = False BtnUpdate.Enabled = False BtnDelete.Enabled = False txtFirstName.Clear() txtLastName.Clear() inc = 0 End Sub

Private Sub BtnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClick.Click frmSearch.Show() End Sub

End Class

Step 5: Create an interface as shown below in form2. Name it as frmSearch.

Step 6: Set the name for every object used in the frmSearch as:

Object
TextBox1

Name
txtSearch btnSearch btnReset DataGrid1

Step 7: Write the program code as in frmSearch:


Imports System.Data.OleDb Public Class frmSearch Dim Dim Dim Dim Dim Dim Dim Dim inc As Integer MaxRows As Integer con As New OleDb.OleDbConnection dbProvider As String dbSource As String ds As New DataSet da As OleDb.OleDbDataAdapter sql As String

Private Sub BtnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearch.Click If TextBox1.Text = "" Then MessageBox.Show("Please Enter First Name") Else dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" dbSource = "Data Source = D:/AddressBook.mdb" con.ConnectionString = dbProvider & dbSource

sql = "select* from tblContacts where FirstName " & _ "like '%" & TextBox1.Text & "%'" da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "AddressBook")

DataGrid1.DataSource = ds.Tables(0) BtnSearch.Enabled = False BtnReset.Enabled = True End If

End Sub Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged Dim stuid As String stuid = DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0) End Sub

Private Sub BtnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReset.Click BtnSearch.Enabled = True BtnReset.Enabled = False TextBox1.Text = "" End Sub End Class

Step 8: Set the connection by click Data Add New Source

Step 9: Click Next

Step 10: Click New Connection

Step 11: Click Browse and Look in your location of AddressBook.mdb.After you select it click Open

Step 12: Then the text box for database file name will be filled up according to the location of your database file. Click Test Connection and when the pop up message Test connection

succeeded is displayed, the connection is properly connected. If not you have to check the right database file or set up the right procedure to have the connection. Click OK.

Step 13: Click Next.

Step 14: Then it will pop up a message. Click Yes.

Step 15: Check

Tables. Click Finish.

You will get something like shown above in your Solution Explorer. Congrats! You have successfully set the connection to the database. Now you may fill in the data into the form to be manipulated. Enjoy your exploration!

Potrebbero piacerti anche