Chapter 10 Accessing Database Files

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

Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 14.3 LINQ to SQL Programming in Visual Basic 2010: The Very Beginner’s Guide by Jim McKeown Databases – Part 3.
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.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
1 Chapter 12 Working With Access 2000 on the Internet.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Chapter 10 Accessing Database Files Programming In Visual Basic.NET.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
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.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Some Basic Database Terminology
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Databases and LINQ Visual Basic 2010 How to Program 1.
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.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Chapter 10 Accessing Database Files Programming In Visual Basic.NET.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 71 Building Data-Driven ASP.NET Applications Introduction to ASP.NET By Kathleen Kalata.
Creating Web Applications Using ASP.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 11 Accessing Database.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
CHAPTER EIGHT Accessing Data Processing Databases.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 30 – Bookstore Application: Client Tier Examining.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ.
CHAPTER EIGHT Accessing Data Processing Databases.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
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.
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.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases.
HNDIT Rapid Application Development
1 Database Programming with ADO.NET Kashef Mughal.
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.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Binding to ListBox and ComboBox Controls List and combo boxes are frequently.
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
Chapter 2: The Visual Studio .NET Development Environment
VB 2010 Pertemuan 10.
© 2013, Mike Murach & Associates, Inc.
ADO.NET Accessing Databases in VS.NET
VB.NET Using Database.
Brief description on how to navigate within this presentation (ppt)
VISUAL BASIC INTRODUCTION TO DATA CONNECTIVITY.
CIS16 Application Programming with Visual Basic
Using List Controls with SQL Server
Chapter 10 ADO.
Database Applications
Unit J: Creating a Database
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Chapter 10 Accessing Database Files Programming In Visual Basic .NET

Visual Basic and Database Files VB projects can display and update the data from database files VB .NET uses ADO.NET for database access ADO.NET is the next generation of database technology, based on Microsoft's previous version, ActiveX Data Objects (ADO) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

ADO.NET Data is stored and transferred in Extensible Markup Language (XML) Allows access to database data in many formats Basic types of providers OleDb, SQLClient for SQL Server, Odbc and Oracle © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Database Terminology Databases Tables Relational database Records (rows) Fields (columns) Key field (unique to each record) Relational database Multiple tables Relationships between the tables © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

XML Data Industry-standard format for storing and transferring data Specifications at http://www.w3.org/XML, World Wide Web Consortium (W3C) The XML needed for accessing databases will be automatically generated for you in Visual Basic © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

XML Data (continued) Data stored in XML are all text, identified by tags XML files can be edited by any text editor, such as Notepad Tags Used for identification, similar to HTML Not predefined like HTML tags Can identify database fields by name © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

XML Data (continued) XML Data File XML Schema File (.xsd file) Contains actual data XML Schema File (.xsd file) Contains descriptions for Fields Data Types Constraints, such as required fields View .xsd file in Solution Explorer © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Using ADO.NET and Visual Basic Can display data from a database on Windows Form Web Form Add controls to form and bind data to them Label, TextBox Special controls designed just for data such as DataGrid, DataList © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Using ADO.NET and Visual Basic (continued) Connection Establishes a link to a data source, a specific file and/or server Data Adapter Handles retrieving and updating the data Dataset Contains actual data, may come from multiple connections and/or multiple data adapters © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Steps to Display Data in Bound Controls on a Form Source Connection Adapter Dataset Web Form Windows Specific file Connection Object Handles data transfer and provides data for dataset; uses SQL Actual data, can contain multiple tables and relationships Data display on the form in bound controls © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Connections Establishes a link from specific file or database to program ADO.NET provides four types OleDBConnection SqlConnection OdbcConnection OracleConnection © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Connections (continued) Creating a new connection Data controls in the Toolbox Use Server Explorer to manage connections No matter which method above is used to begin the connection, the Data Link Properties dialog appears for you to complete the task © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Creating a Connection from Server Explorer Click Connect to Database button OR Right-click Data Connections, select Add Connection © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Data Link Properties Dialog Used to define Connection's OLE DB Provider Database Filename Can test if connection succeeded © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Connections in Server Explorer Shows connections for all projects Same connection can be used for multiple projects Expand connection node to view tables and fields © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Data Adapters Does all the work of passing data back and forth between a data source and a program Data does not have to be from a database Data can be text file, object, or even an array Add data adapter Drag table name from Server Explorer to form Ctrl + click field names and drag as a group to the form © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Datasets Temporary set of data stored in memory In ADO.NET datasets are disconnected; the copy of data kept in memory does not keep an active connection to the data source Dataset may contain multiple tables and relationships © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Datasets (continued) To define a dataset Select data adapter component Right-click on the adapter and select Generate Dataset Name the dataset in the Generate Dataset dialog box Write code to fill the dataset (opens the connection to the data source and retrieves data into the dataset) Any controls bound to the dataset will automatically fill with data © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Data Passes from Data Source to Dataset to User Interface Data Object (Class) Windows Forms Web Data Adapter Presentation Tier Business Tier Data Tier Dataset Source © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The Fill Method Execute Fill method of Data Adapter at run time to fill the dataset Usually coded in Form_Load (or Page_Load for Web projects) General Form Example DataAdapterName.Fill(DataSetName) booksDataAdapter.Fill(BooksDataset1) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Binding Data to Controls Set properties of the controls To bind data to a grid, set DataSource property Data Member property To bind individual fields to controls (such as labels and text boxes) Set DataBindings property © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Creating a Bound DataGrid Control (pages 391-394) Steps Set up the Connection Add the Connection and Data Adapter to the Form Rename Connection and Data Adapter Generate a Dataset Add a Data Grid to the Form and set DataSource and DataMember properties © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Creating a Bound DataGrid Control (continued) Write the Code Windows Form - Form_Load booksDataAdapter.Fill(BooksDataset1) Web Form - Page_Load booksDataAdapter.Fill(BooksDataset1) booksDataGrid.DataBind( ) Run the Project Modify the Grid Format © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Displaying a Data Preview Allows you to see what your data looks like while you are designing the user interface Right-click DataAdapter control, select Preview Data In the dialog box verify that the correct DataAdapter displays in the list Click Fill Dataset, resize columns and scroll to see all data © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Displaying a Data Preview (continued) Click to Display the data © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Binding Data Complex Data Binding Simple Data Binding Binds more than one data element to a single control Used for DataGrids, and List or Combo Boxes Set DataSource and DataMember properties (may need additional) Simple Data Binding Connects one control to one data element through the Data Bindings property Use for TextBoxes and Labels (or any other control that has a Data Bindings property) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Selecting Records from a Combo Box Allows user to select records to view or update Windows Forms Use ListBox or ComboBox Web Forms Use ListBox or DropDownList © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Filling a List - Windows Forms Set ListBox or ComboBox Properties DataSource to DataSet name DisplayMember to table name.field name Use Fill method to populate list with data ' Fill the dataset for the list box. BooksDataAdapter.Fill(BooksDataset1) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Binding a Single Data Field To bind data to a single control set the DataBindings property Expand DataBindings property and choose the Text property Expand the Dataset to reveal a list of fields Set the DataBindings Text property to the correct field © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Binding a Single Data Field (continued) Bind a Label control’s Text property to a single field in a dataset © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Sorting the List Data Sort records using the data adapter’s SQL SELECT statement Select the data adapter, choose Data/Configure Data Adapter Navigate through the Wizard to the SQL statement page Choose “Ascending” in the Sort Type cell of the Query Builder Right-click the data adapter, select Generate Dataset and select the existing dataset name © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Sorting the List Data (continued) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Selecting Records Using Web Forms Set up connection, data adapter and dataset same as for Windows applications Need to modify program logic for the Web due to security and because web pages are Stateless Stateless means that each time a page displays it is a new fresh page Page_Load event occurs for every round trip to the server © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Filling a List – Web Forms Postback, round trip to the server, occurs each time user makes a selection After Postback, web page redisplays, Page_Load event occurs Modify logic in Page_Load to use selection made by user Use Me.DataBind( ) statement to bind all controls on page at once © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Setting the List Properties Properties to set for the Web version of DropDownList control DataSource property to the Dataset DataMember to the table name DataTextfield to name of field to display in list AutoPostBack property set to True to respond when a user selects from the list © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Retrieving the Selected Record In a Web application, the dataset is created on the server, only one pageful of data is sent to the client When a user makes a selection from a list, it makes sense to retrieve only the selected record, not the entire table Use a parameterized query to create a dataset with only the selected record © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Retrieving the Selected Record (continued) A Web selection application will have two data adapters One selects data to fill a drop-down list One selects a single record to display An SQL SELECT statement to select a particular record requires a WHERE clause Use a question mark in place of an actual value and supply the value as a parameter in code This type of query is called a parameterized query © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Query Builder - Parameterized Query Where (Title = ?) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Displaying the Data for a Selected Item Supply value to be used for a Parameterized Query at run time TextBox Check for an empty dataset after the Fill method List box selection Write code in the SelectedIndexChanged Event Specify the parameter value using the Parameters collection of the data adapter’s SelectCommand property © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Displaying the Data for a Selected Item – Code Example Private Sub titleDropDownList_SelectedIndexChanged(ByVal _ sender As System.Object, ByVal e As System.EventArgs) _ Handles titleDropDownList.SelectedIndexChanged ' Retrieve the record matching the selection. If titleDropDownList.SelectedIndex <> -1 Then BooksDataset1.Clear( ) booksDataAdapter.SelectCommand.Parameters("Title").Value _ = titleDropDownList.SelectedItem.Text booksDataAdapter.Fill(BooksDataset1) ISBNLabel.DataBind( ) authorLabel.DataBind( ) End If End Sub © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Making a Database Project Portable You must move database AND Create Connection on new computer Configure DataAdapter For Web projects, also create virtual directory Less problems if the database is stored in the bin folder of the project Do not include path in the data adapter’s ConnectionString property © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.