CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.

Slides:



Advertisements
Similar presentations
Microsoft SQL Server 2008 From the Program menu choose: Microsoft SQL Server 2008 R2  SQL Server Management Studio. You may see a window indicating the.
Advertisements

Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
 Open the Paradise.exe file  Application displays records in the Books database  Allows the store manager to enter an author’s name (or part of a name)
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.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Visual Basic 2010 How to Program Reference: Instructor: Maysoon Bin Duwais slides Visual Basic 2010 how to program by Deitel © by Pearson Education,
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
3-1 Chapter 3 Data and Knowledge Management
Chapter Extension 6 Using Microsoft Access © 2008 Pearson Prentice Hall, Experiencing MIS, David Kroenke.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Access Tutorial 10 Automating Tasks with Macros
Some Basic Database Terminology
ACCESS CHAPTER 1. OBJECTIVES Tables Queries Forms Reports Primary and Foreign Keys Relationship.
With Microsoft Office 2007 Intermediate© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Intermediate.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
With Microsoft Access 2007 Volume 1© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access 2007 Volume 1 Chapter.
1 Chapter 1: Finding Your Way Through a Database Exploring Microsoft Office Access 2010.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 15: Using LINQ to Access Data in C# Programs.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
CHAPTER EIGHT Accessing Data Processing Databases.
© 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.
Connecting to Data Sources Using ADO.NET Dr. Awad Khalil Computer Science & Engineering Department AUC.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Introduction to Databases Trisha Cummings. What is a database? A database is a tool for collecting and organizing information. Databases can store information.
CIS 338: Using Queries in Access as a RecordSource Dr. Ralph D. Westfall May, 2011.
MS Access 2007 Management Information Systems 1. Overview 2  What is MS Access?  Access Terminology  Access Window  Database Window  Create New Database.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
1 Database Design and Development: A Visual Approach © 2006 Prentice Hall Chapter 8 DATABASE DESIGN AND DEVELOPMENT: A VISUAL APPROACH Chapter 8 Creating.
1 Chapter 1: Finding Your Way Through a Database Exploring Microsoft Office Access 2007.
Introduction to Database using Microsoft Access 2013 Part 7 November 19, 2014.
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 3 BACKNEXTEND 3-1 LINKS TO OBJECTIVES Modify a Table – Add, Delete, Move Fields Modify a Table.
INFORMATION TECHNOLOGY DATABASE MANAGEMENT. Adding a new field 1Right click the table name and select design view 2Type the field information at the end.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Databases with LINQ. LINQ to SQL LINQ to SQL uses LINQ syntax to query databases. LINQ to SQL classes are automatically generated by the IDE’s LINQ to.
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.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
INTRODUCTION TO ACCESS. OBJECTIVES  Define the terms field, record, table, relational database, primary key, and foreign key  Create a blank database.
1 Database Design and Development: A Visual Approach © 2006 Prentice Hall Chapter 12 DATABASE DESIGN AND DEVELOPMENT: A VISUAL APPROACH Chapter 12 Accessing.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 25 I’m Suffering from Information Overload.
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases.
Chapter 24 I’m Suffering from Information Overload (Access Databases) Clearly Visual Basic: Programming with Visual Basic nd Edition.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 1 ® Database & Table.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
 A database is an organized collection of data  A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying.
Creating SQL Database file And Displaying a Database Table in a DataGridView.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
Basic Database Concepts
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
Database Applications
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011

Objectives In this chapter, you will –Learn the relational database model –Get familiar with using LINQ to retrieve and manipulate data from a database –Know how to add data sources to project, create LINQ to SQL classes, and use data binding to move data between GUI controls and databases –Learn how to create Master/Detail views to select a record and display its details 2

Introduction A database is an organized collection of data A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data Existing DBMSs –Microsoft SQL Server –Oracle –Sybase –IBM DB2 3

Introduction (cont'd) PostgreSQL and MySQL are popular open- source DBMSs that can be downloaded and used freely by anyone In this class, we use Microsoft’s free SQL Server Express, which is installed with Visual Basic Express and Visual Studio It can also be downloaded separately from Microsoft ( 4

Relational Database A relational database organizes data simply in tables –rows (also called records) –columns (also called fields, attributes) Primary key: a column (or group of columns) requiring a unique value that cannot be duplicated in other rows A primary key composed of two or more columns is known as a composite key Foreign key—a column in this table that matches the primary-key column in another table 5

Example of Table: Employees 6

SQL A program might select data from the table to create a query result –E.g., to retrieve the location of each department, in increasing order by Department number –SQL: Select distinct Department, Location From Employees Order By Department 7

SQL Results 8

Schema A database’s tables, their fields and the relationships among them are collectively known as a database schema In SQL Server, database files have the.mdf (“master data file”) file-name extension –Books.mdf 9

Books Database Books database consists of 3 tables –Authors –Titles –AuthorISBN 10

Authors 11

Titles 12

AuthorISBN 13

Entity-Relationship Model Entity-Relationship (ER) model –Entity Authors Titles –Relationship There is a one-to-many relationship between a primary key and a corresponding foreign key –E.g., one author can write many books and one book can be written by many authors Others: many-to-many or one-to-one relationship E.g., AuthorISBN 14

Example of ER Diagram 15 Books database

Querying a Database with LINQ Connect to the Books database Create the LINQ to SQL classes required to use the database Add the Authors table as a data source Drag the Authors table data source onto the Design view to create a GUI for displaying the table’s data Add a few statements to the program to allow it to interact with the database 16

GUI To Display Authors Table 17

Creating LINQ to SQL Classes Step 1: Creating the Project –Create a new Windows Forms Application named DisplayTable –Change the name of the source file to DisplayAuthorsTable.vb –The IDE updates the Form’s class name to match the source file –Set the Form’s Text property to Display Authors Table 18

Creating LINQ to SQL Classes (cont'd) Step 2: Adding a Database to the Project and Connecting to the Database –To interact with a database, you must create a connection to the database. –In Visual Basic 2010 Express, select View > Other Windows > Database Explorer to display the Database Explorer window For a full version of Visual Studio, select View > Server Explorer to display the Server Explorer –Click the Connect to Database icon at the top of the Database Explorer In Choose Data Source dialog, select Microsoft SQL Server Database File from the Data source: list Always use this selection CheckBox, the IDE will use this type of database file by default when you connect to databases Click Continue to display the Add Connection dialog 19

Choose Data Source Dialog 20

Creating LINQ to SQL Classes (cont'd) In the Add Connection dialog –click the Change… Button to select a database –click Browse… to locate and select the Books.mdf file in the Databases directory –click Test Connection to verify that the IDE can connect to the database through SQL Server Express –click OK to create the connection 21

Add Connection Dialog 22

Creating LINQ to SQL Classes (cont'd) Step 3: Generating the LINQ to SQL classes –Select the database from which the LINQ to SQL classes will be created –LINQ to SQL uses the database’s schema to help define the classes –Right click the project name in the Solution Explorer and select Add > New Item… to display the Add New Item dialog Select the LINQ to SQL Classes template, name the new item Books.dbml and click the Add button –The Object Relational Designer window will appear Double click the Books.dbml file in the Solution Explorer to open the Object Relational Designer 23

Object Relational Designer 24

Creating LINQ to SQL Classes (cont'd) Expand the Books.mdf database node in the Database Explorer Expand the Tables node Drag the Authors, Titles and AuthorISBN tables onto the Object Relational Designer The IDE prompts whether you want to copy the database to the project directory Select Yes –The Object Relational Designer will display the tables that you dragged from the Database Explorer Save the Books.dbml file 25

Data Bindings Between Controls and the LINQ to SQL Classes Step 1: Adding the Author LINQ to SQL Class as a Data Source To use the LINQ to SQL classes for data binding, you must first add them as a data source. –Select Data > Add New Data Source… to display the Data Source Configuration Wizard. –The LINQ to SQL classes are used to create objects representing the tables in the database, so we’ll use an Object data source. In the dialog, select Object and click Next >. Expand the tree view and ensure that Author is checked. An object of this class will be used as the data source –Click Finish 26

27

Data Bindings Between Controls and the LINQ to SQL Classes (cont'd) The Authors table in the database is now a data source that can be used by the bindings Open the Data Sources window by selecting Data > Show Data Sources. 28

Data Bindings Between Controls and the LINQ to SQL Classes (cont'd) Step 2: Creating GUI Elements –Use the Design view to create a GUI control that can display the Authors table’s data Switch to Design view for the DisplayAuthorsTable class Click the Author node in the Data Sources window—it should change to a drop-down list. –Open the drop-down by clicking the down arrow –Select DataGridView option—this is the GUI control that will be used to display and interact with the data. Drag the Author node from the Data Sources window onto the Form in Design view 29

Data Bindings Between Controls and the LINQ to SQL Classes (cont'd) To make the DataGridView occupy the entire window –select the DataGridView –use the Properties window to set the Dock property to Fill 30

Data Bindings Between Controls and the LINQ to SQL Classes (cont'd) Step 3: Connecting the BooksDataContext to the AuthorBindingSource –The final step is to connect the BooksDataContext to the AuthorBindingSource, so that the application can interact with the database –Obtain data from the database and to save any changes that the user makes to the data back into the database (see next slide) 31

32

Data Bindings Between Controls and the LINQ to SQL Classes (cont'd) Step 4: Saving Modifications Back to the Database –If the user modifies the data in the DataGridView, we’d also like to save the modifications in the database –By default, the BindingNavigator’s Save Data Button is disabled –To enable it, right click this Button’s icon and select Enabled –Then, double click the icon to create its Click event handler –Saving the data entered into the DataGridView back to the database is a three-step process (lines 22–24) 33

34

35