Visual Basic 2010 How to Program

Slides:



Advertisements
Similar presentations
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

 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 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.
© by Pearson Education, Inc. All Rights Reserved.
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.
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
Unit J: Creating a Database Microsoft Office Illustrated Fundamentals.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Advanced Forms Lesson 10.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 2 Welcome Application Introducing the Visual Basic 2008 Express Edition IDE.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
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.
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.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
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.
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,
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
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.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
1 Access Lesson 1 Understanding Access Fundamentals Microsoft Office 2010 Fundamentals Story / Walls.
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.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
Databases Databases Using Microsoft Access 2003 Microsoft Access 2003 is a powerful, yet easy to learn, relational database application for Microsoft Windows.
Dive Into® Visual Basic 2010 Express
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Database, SQL and ADO.NET
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Practical Office 2007 Chapter 10
Basic Database Concepts
Incorporating Databases with ADO.NET
Access Creating a Database
Exploring Microsoft Office Access
Access Creating a Database
Access Lesson 1 Understanding Access Fundamentals
Incorporating Databases with ADO.NET
The University of Texas – Pan American
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
Databases and Information Management
Introduction to Database Programs
Database Applications
Chapter 10 Accessing Database Files
Introduction to Database Programs
Databases and Information Management
Unit J: Creating a Database
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Visual Basic 2010 How to Program Databases and LINQ Visual Basic 2010 How to Program

1. Introduction A database is an organized collection of data. A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data. Today’s most popular DBMSs manage relational databases, which organize data simply as tables with rows and columns. In this chapter, 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 (www.microsoft.com/express/sql).

1.Introduction Previous chapter introduced LINQ to Objects and used it to manipulate data stored in arrays. LINQ to SQL allows you to manipulate data stored in a SQL Server or SQL Server Express relational database. This chapter introduces general concepts of relational databases, then explores LINQ to SQL and the tools for working with databases.

2. Relational Databases A relational database organizes data simply in tables. Figure 12.1 illustrates a sample Employees table that might be used in a personnel system. The table stores the attributes of employees. Tables are composed of rows (also called records) and columns (also called fields) in which values are stored. This table consists of six rows (one per employee) and five columns (one per attribute).

© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 2.Relational Databases LINQ to SQL requires every table to have a primary key to support updating the data in tables. The rows in Fig. 12.1 are displayed in ascending order by primary key. But they could be listed in decreasing (descending) order or in no particular order at all. You can use LINQ to SQL to define queries that select subsets of the data from a table. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 3.LINQ to SQL LINQ to SQL enables you to access data in SQL Server databases using the same LINQ syntax introduced in Chapter 3. You interact with the database via classes that are automatically generated from the database schema by LINQ to SQL Designer. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

LINQ to SQL object model The most fundamental elements in the LINQ to SQL object model and their relationship to elements in the relational data model are summarized in the following table: Relational data model LINQ to SQL object model Table Entity Class Column Class member Foreign key relationship Association Stored Procedure or Function Method

3.LINQ to SQL Objects are linked to relational data by decorating normal classes with attributes. Two of the most important attributes are Table and Column: The Table attribute is used to decorate the class (the name of the class will be used for the name of the table). The Column attribute is used to decorate fields or properties of an entity class.

3.LINQ to SQL DataContext Class All LINQ to SQL queries occur via a DataContext class, which controls the flow of data between the program and the database. A specific DataContext derived class, which inherits from the class System.Data.Linq.DataContext, is created when the LINQ to SQL classes representing each row of the table are generated by the IDE. This derived class has properties for each table in the database, which can be used as data sources in LINQ queries. Any changes made to the DataContext can be saved back to the database using the DataContext’s SubmitChanges method, so with LINQ to SQL you can modify the database’s contents.

3.LINQ to SQL To simplify the steps of querying a database with LINQ consider a simple example Books database. Books database contains three tables (Authors, AuthorISBN and Tiltles) as in the following figure:

© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 3.LINQ to SQL A database’s tables, their fields and the relationships among them are collectively known as a database schema. LINQ to SQL uses a database’s schema to define classes that enable you to interact with the database. Next, we show how to use LINQ to SQL to retrieve information from the Books database. The database file—Books.mdf—is provided with this chapter’s examples. SQL Server database files have the .mdf (“master data file”) file-name extension. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

4.Querying a Database with LINQ In this section, we demonstrate how to connect to a database, query it and display the results of the query. The IDE provides visual programming tools and wizards that simplify accessing data in applications. These tools establish database connections and create the objects necessary to view and manipulate the data through Windows Forms GUI controls—a technique known as data binding.

4.Querying a Database with LINQ The basic steps are: Connect to the database. Create the LINQ to SQL classes required to use the database. Add the table as a data source. Drag the 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.

4.Querying a Database with LINQ Our first example performs a simple query on the Books database. We retrieve the entire Authors table and use data binding to display its data in a DataGridView—a control from namespace System.Windows.Forms that can display data from a data source in tabular format.

4.1 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.

4.1 Creating LINQ to SQL Classes Step 2: Adding a Database to the Project and Connecting to the Database In Visual Basic 2010 Express, select View > Other Windows > Database Explorer to display the Database Explorer window. Click the Connect to Database icon at the top of the Database Explorer. If the Choose Data Source dialog appears (Fig. 12.11), select Microsoft SQL Server Database File from the Data source: list. Click Continue to display the Add Connection dialog (Fig.12.12). To interact with a database, you must create a connection to the database. By default, it appears on the left side of the IDE. If you’re using a full version of Visual Studio, select View > Server Explorer to display the Server Explorer. From this point forward, we’ll refer to the Database Explorer. If you have a full version of Visual Studio, substitute Server Explorer for Database Explorer in the steps.

Choose database file (.mdf) verify that the IDE can connect to the database through SQL Server Express.

4.1 Creating LINQ to SQL Classes Step 3: Generating the LINQ to SQL classes After the database has been added, you must 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. 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 (Fig. 12.13). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

4.1 Creating LINQ to SQL Classes Expand the Books.mdf database node in the Database Explorer (or Server Explorer), then expand the Tables node. Drag the tables (Authors, Titles and AuthorISBN) onto the Object Relational Designer. The Object Relational Designer will display the tables that you dragged from the Database Explorer. Save the Books.dbml file.

4.1 Creating LINQ to SQL Classes When you save Books.dbml, the IDE generates the LINQ to SQL classes that you can use to interact with the database. These include a class for each table you selected from the database and a derived class of DataContext named BooksDataContext that enables you to programmatically interact with the database.

4.2 Data Bindings Between Controls and the LINQ to SQL Classes The IDE’s automatic data binding capabilities simplify creating applications that can view and modify the data in a database. To display the contents of the table (such as Authors table) in a GUI do the following steps: Step 1: Adding the Author LINQ to SQL Class as a Data Source Step 2: Creating GUI Elements Step 3: Connecting the BooksDataContext to the AuthorBindingSource Step 4: Saving Modifications Back to the Database

4.2 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. In the dialog, select Object and click Next >. Expand the tree view as shown in Fig. 12.15 and ensure that Author is checked. An object of this class will be used as the data source. Click Finish. The Authors table in the database is now a data source that can be used by the bindings.

4.2 Data Bindings Between Controls and the LINQ to SQL Classes 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 and ensure that the DataGridView option is selected—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.

4.2 Data Bindings Between Controls and the LINQ to SQL Classes The IDE creates a DataGridView (Fig. 12.17) with the correct column names and a BindingNavigator (AuthorBindingNavigator) that contains Buttons for moving between entries, adding entries, deleting entries and saving changes to the database. The IDE also generates a BindingSource (AuthorBindingSource), which handles the transfer of data between the data source and the data- bound controls on the Form.

4.2 Data Bindings Between Controls and the LINQ to SQL Classes Step 3: Connecting the BooksDataContext to the AuthorBindingSource The final step is to connect the BooksDataContext (created with the LINQ to SQL classes in Section 4.1) to the AuthorBindingSource (created with in Section 4.2), so that the application can interact with the database. Figure 12.18 shows the small amount of code needed to obtain data from the database and to save any changes that the user makes to the data back into the database.

4.2 Data Bindings Between Controls and the LINQ to SQL Classes As mentioned in before, a DataContext object is used to interact with the database. The BooksDataContext class was automatically generated by the IDE when you created the LINQ to SQL classes to allow access to the Books database. Line 4 creates an object of this class named database. Create the Form’s Load handler by double clicking the Form’s title bar in Design view.

4.2 Data Bindings Between Controls and the LINQ to SQL Classes We allow data to move between the DataContext and the BindingSource by creating a LINQ query that extracts data from the BooksDataContext’s Authors property (lines 12–14), which corresponds to the Authors table in the database. The AuthorBindingSource’s DataSource property (line 11) is set to the results of this query. The AuthorBindingSource uses the DataSource to extract data from the database and to populate the DataGridView. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

4.2 Data Bindings Between Controls and the LINQ to SQL Classes 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). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

4.2 Data Bindings Between Controls and the LINQ to SQL Classes First, all controls on the form are validated (line 22)—if any of the controls have event handlers for the Validating event, those execute. You typically handle this event to determine whether a control’s contents are valid. Second, line 23 calls EndEdit on the AuthorBindingSource, which forces it to save any pending changes in the BooksDataContext. Finally, line 24 calls SubmitChanges on the BooksDataContext to store the changes in the database. For efficiency, LINQ to SQL saves only data that has changed. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.