1/36 Database Programming with Visual Basic.Net and MS Access 2009.4.29 IKE Lab. Yunho Song Database Management and Analysis.

Slides:



Advertisements
Similar presentations
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Advertisements

Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
1.NET Web Forms DataGrid © 2002 by Jerry Post. 2 Data Grid Has Many Uses  The grid uses HTML tables to display multiple rows of data. It is flexible.
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Access Lesson 2 Creating a Database
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 10 Accessing Database Files Programming In Visual Basic.NET.
Using ADO.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
EGR 141 Computer Problem Solving in Engineering and Computer Science Interfacing with a Database in Visual Basic.NET 2005.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Microsoft Visual Basic 2012 CHAPTER TEN Incorporating Databases with ADO.NET.
Modules Standard Modules. Standard Modules The Subs and Functions worked perfectly well where they were – inside the two lines “Public Class Form1” and.
Stored Procedures Dr. Ralph D. Westfall May, 2009.
Access Tutorial 10 Automating Tasks with Macros
Microsoft Visual Basic 2005 CHAPTER 12 Cell Phone Applications and Web Services.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Some Basic Database Terminology
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
BIM211 – Visual Programming Database Operations II 1.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Introduction to the Visual Studio.NET IDE (LAB 1 )
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
ASP.NET The Clock Project. The ASP.NET Clock Project The ASP.NET Clock Project is the topic of Chapter 23. By completing the clock project, you will learn.
Windows Forms Navigating database with windows forms.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
® Microsoft Access 2010 Tutorial 10 Automating Tasks with Macros.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
CSC 230 (Blum)1 Visual Basic 2005 Hello World Fall 2005 T. Blum.
1 Advanced Computer Programming Lab Calculator Project.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Ch. 101 Database Management An Introduction to Databases.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 8 – Integrating a Database with a FrontPage.
CS 281 – Fall 2015 Lab 4 Parametric Query and Forms in MS Access.
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases.
1 Database Programming with ADO.NET Kashef Mughal.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
VB.NET and Databases. ADO.NET VB.Net allows you many ways to connect to a database. The technology used to interact with a database or data source is.
CIS 338: VB.NET Printing Dr. Ralph D. Westfall May, 2011.
CS 281 – Fall 2010 Lab 4 Parametric Query and Forms in MS Access.
Programming with Visual Basic.NET. Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons.
Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY.
Creating SQL Database file And Displaying a Database Table in a DataGridView.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Lecture 6 VB.Net SQL Server.
Incorporating Databases with ADO.NET
Incorporating Databases with ADO.NET
CIS16 Application Programming with Visual Basic
Chapter 10 Accessing Database Files
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

1/36 Database Programming with Visual Basic.Net and MS Access IKE Lab. Yunho Song Database Management and Analysis

2/36 Database Programming with Visual Basic.Net and MS Access Contents 1.Installation 2.VB.NET and Database 1.The Database Wizard (Visual Studio users) 2.Write your own VB.NET database code 3.Learn about DataSets and Data Adaptors 4.Display the Data in the DataSet 5.Navigate a Database with VB.NET 6.How to Move through the Database 7.Add, Update and Delete Records 8.Add a New Record using VB.NET 9.Delete a Record using VB.NET 10. A VB.NET Database Project

1. Installation

4/36 Installation 1. Installation Database Programming with Visual Basic.Net and MS Access

5/36 1. Installation Database Programming with Visual Basic.Net and MS Access Installation

6/36 1. Installation Database Programming with Visual Basic.Net and MS Access Installation

7/36 1. Installation Database Programming with Visual Basic.Net and MS Access First run - choose the default environment settings Installation

2. VB.NET and Database

9/36 VB.NET and Database Let's make a start on our Database project  Click File > New Project from the menu bar  Select Windows Application, and then give it the Name AddressBook. Click OK  Locate the Solution Explorer on the right hand side Database Programming with Visual Basic.Net and MS Access 2.1. Database Wizard  Select Data Sources

10/36 VB.NET and Database The Wizard display all your tables, fields and objects Database Programming with Visual Basic.Net and MS Access 2.1. Database Wizard

11/36 VB.NET and Database The Data Sources area displays information about your database Database Programming with Visual Basic.Net and MS Access 2.1. Database Wizard  Click the plus symbol next to tblContacts

12/36 VB.NET and Database To add a Field to your Form Database Programming with Visual Basic.Net and MS Access 2.1. Database Wizard  A textbox and a label will be added

13/36 VB.NET and Database Run your program by hitting the F5 key Database Programming with Visual Basic.Net and MS Access 2.1. Database Wizard  Drag and Drop more Fields to your form

14/36 The Connection Object  What you need if you want to connect to a database  OLE(Object Linking and Embedding) allow you to connect to data sources in general, and not just databases. You can use it, for example, to connect to text files, SQL Server, , and a whole lot more  Place a button on your form. Change the Name property to btnLoad Double click your button to open up the code window Add the following line: Database Programming with Visual Basic.Net and MS Access 2.2. Write your own Database code VB.NET and Database Dim con As New OleDb.OleDbConnection  At the top of your code window, before Public Class Form 1, type the following: Imports System.Data

15/36 Database Programming with Visual Basic.Net and MS Access 2.2. Write your own Database code VB.NET and Database Coding window will look like this  Get a pop up box from where you can select OleDbConnection  Use to connect to an Access database

16/36 Database Programming with Visual Basic.Net and MS Access 2.2. Write your own Database code VB.NET and Database Setting a Connection String  The technology is called the Provider; and you use "Data Source" to specify where your database is con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\AddressBook.mdb"  The first part specifies which provider technology we want to use to do the connecting (JET)  The second part, typed after a semi-colon, points to where the database is  In the above code, the database is on the C drive, in the root folder. The name of the Access file we want to connect to is called AddressBook.mdb

17/36 Database Programming with Visual Basic.Net and MS Access 2.2. Write your own Database code VB.NET and Database Opening the Connection  Open method of the Connection Object:  Close method of the Connection Object: con.Open( ) MsgBox("A Connection to the Database is now open“) con.Close() MsgBox("The Connection to the Database is now Closed“)

18/36 Database Programming with Visual Basic.Net and MS Access 2.2. Write your own Database code VB.NET and Database Coding window Test out our code

19/36 Database Programming with Visual Basic.Net and MS Access 2.3. Data Sets and Data Adapters VB.NET and Database Data Sets  Hidden from you, and just stored in memory  Imagine a grid with rows and columns. Each imaginary row of the DataSet represents a Row of information in your Access database. And each imaginary column represents a Column of information in your Access database (called a Field in Access) Data Adapter  The Connection Object and the DataSet can't see each other  They need a go-between so that they can communicate  This go-between is called a Data Adapter  The Data Adapter contacts your Connection Object, and then executes a query that you set up. The results of that query are then stored in the DataSet.

20/36 Database Programming with Visual Basic.Net and MS Access 2.3. Data Sets and Data Adapters VB.NET and Database Data Adapter and DataSet are objects  OleDb.OleDbDataAdapter Called da Hold a reference to the Data Adapter  da = New OleDb.OleDbDataAdapter(sql, con) creates a new Data Adapter object Need to put two things in the round brackets of the Object declaration SQL string (which we'll get to shortly), and connection object Connection Object is stored in the variable called con  Data Adaptor acting as a go-between for the Connection Object and the Data Set Dim ds As New DataSet Dim da As OleDb.OleDbDataAdapter da = New OleDb.OleDbDataAdapter(sql, con)

21/36 Database Programming with Visual Basic.Net and MS Access 2.3. Data Sets and Data Adapters VB.NET and Database Structured Query Language(SQL)  A way to query and write to databases Hold a reference to the Data Adapter  To select just the first name and surname columns from our database  To SELECT all (*) the records from the table called tblContacts Select * from Table_Name SELECT tblContacts.FirstName, tblContacts.Surname FROM tblContacts sql = "SELECT * FROM tblContacts”

22/36 Database Programming with Visual Basic.Net and MS Access 2.3. Data Sets and Data Adapters VB.NET and Database Structured Query Language(SQL) Dim ds As New DataSet Dim da As OleDb.OleDbDataAdapter Dim sql As String sql = "SELECT * FROM tblContacts" da = New OleDb.OleDbDataAdapter(sql, con)

23/36 Database Programming with Visual Basic.Net and MS Access 2.3. Data Sets and Data Adapters VB.NET and Database Filling the DataSet  Data Adapter can Fill a DataSet with records from a Table  The DataSet (ds) will now be filled with the records we selected from the table called tblContact  One slight problem - nobody can see the data yet! We'll tackle that in the next part da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "AddressBook“)

24/36 Database Programming with Visual Basic.Net and MS Access 2.4. Displaying the Data in the DataSet VB.NET and Database To display the records on a Form  Add two textboxes to your form  Change the Name properties of your textboxes to txtFirstName and txtSurname  Go back to your code window  Add the following two lines: txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1) txtSurname.Text = ds.Tables("AddressBook").Rows(0).Item(2)

25/36 Database Programming with Visual Basic.Net and MS Access 2.5. Navigate a Database VB.NET and Database To see a more practical example  Add two Textboxes. Change the Name properties to txtFirstName and txtSurname  Add four Buttons. Change the Name and Text properties to these: Button NameButton Text btnNext btnPrevious btnFirst btnLast Next Record Previous Record First Record Last Record

26/36 Database Programming with Visual Basic.Net and MS Access 2.5. Navigate a Database VB.NET and Database To see a more practical example  Add the following code to the Form1 Declarations area:  When the Form Loads, we can connect to our database, use the data Adaptor to grab some records from the database and then put these records into the DataSet So in the Form1 Load Event, add the following code:

27/36 Database Programming with Visual Basic.Net and MS Access 2.5. Navigate a Database VB.NET and Database To see a more practical example  You've met all the code before, except for these two lines:  In the MaxRows variable, we can store how many rows are in the DataSet  Get how many rows are in DataSet with Rows.Count: To navigate through the records  Use inc variable. We'll either add 1 to it, or take 1 away  Use the variable for the Rows in the DataSet  It's better to do this in a Subroutine of own  So add this Sub to code: MaxRows = ds.Tables("AddressBook").Rows.Count inc = -1 Private Sub NavigateRecords() txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1) txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2) End Sub

28/36 Database Programming with Visual Basic.Net and MS Access 2.5. Navigate a Database VB.NET and Database The important part is Rows(inc). This moves us through the Rows in the DataSet. Then placing the values into the two Textboxes

29/36 Database Programming with Visual Basic.Net and MS Access VB.NET and Database How to Move Forward One Record at a Time  Double click your Next Record button to access the code 2.6. How to Move through the Database Move Back One Record at a Time  to add to your btnPrevious: 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 If inc <> MaxRows - 1 Then inc = inc + 1 NavigateRecords() Else MsgBox("No More Rows") End If

30/36 Database Programming with Visual Basic.Net and MS Access VB.NET and Database Moving to the Last Record in the DataSet  to add to your btnLast: 2.6. How to Move through the Database If inc <> MaxRows - 1 Then inc = MaxRows - 1 NavigateRecords() End If Moving to the First Record in the DataSet  to add to your btnFirst: If inc <> 0 Then inc = 0 NavigateRecords() End If

31/36 Database Programming with Visual Basic.Net and MS Access VB.NET and Database DataSet is disconnected from the database  Not adding the record to the database: adding it to the DataSet How to add, delete, update new records  Add five more buttons to your form 2.7. Add, Update and Delete Records  Change the Name properties btnAddNew btnCommit btnUpdate btnDelete btnClear

32/36 Database Programming with Visual Basic.Net and MS Access VB.NET and Database Updating a Record  The changes will just get made to the DataSet  Add the following code to btnUpdate: 2.7. Add, Update and Delete Records ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text MsgBox("Data updated“) "Changes are made to the DataSet, and NOT to the Database“  To update the database, Add following code Dim cb As New OleDb.OleDbCommandBuilder(da) ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text da.Update(ds, "AddressBook") MsgBox("Data updated“)

33/36 Database Programming with Visual Basic.Net and MS Access VB.NET and Database Add a New Record  Add New Record button: 2.8. How to Add a New Record btnCommit.Enabled = True btnAddNew.Enabled = False btnUpdate.Enabled = False btnDelete.Enabled = False txtFirstName.Clear() txtSurname.Clear() The Clear/Cancel button can be used to switch it back on again  Add this code to btnClear: btnCommit.Enabled = False btnAddNew.Enabled = True btnUpdate.Enabled = True btnDelete.Enabled = True inc = 0 NavigateRecords()

34/36 Database Programming with Visual Basic.Net and MS Access VB.NET and Database To add a new record to the database  Add this code to btnCommit 2.8. How to Add a New Record 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") = txtSurname.Text ds.Tables("AddressBook").Rows.Add(dsNewRow) da.Update(ds, "AddressBook“) MsgBox("New Record added to the Database“) btnCommit.Enabled = False btnAddNew.Enabled = True btnUpdate.Enabled = True btnDelete.Enabled = True End If

35/36 Database Programming with Visual Basic.Net and MS Access VB.NET and Database Deleting Records from a Database  Add this code to btnDelete 2.8. Delete a Record from a Database Dim cb As New OleDb.OleDbCommandBuilder(da) ds.Tables("AddressBook").Rows(inc).Delete() MaxRows = MaxRows - 1 inc = 0 NavigateRecords() da.Update(ds, "AddressBook“)  To display a message box asking users if they really want to delete this record If MessageBox.Show("Do you really want to Delete this Record?", _ "Delete", MessageBoxButtons.YesNo, _ MessageBoxIcon.Warning) = DialogResult.No Then MsgBox("Operation Cancelled") Exit Sub End If

36/36 Thank You!!