0% found this document useful (0 votes)
49 views34 pages

Programming in Visual Basic 2010: The Very Beginner's Guide: Linq To SQL

The document provides instructions for connecting a Visual Basic program to a Microsoft Access database using LINQ to SQL. It describes how to create a database connection, select the Access file, test the connection, and save the connection string. The steps include adding a DataGridView control to display database records, selecting the data source property, choosing a database model, browsing to select the Access file, and clicking through dialog boxes to complete the connection. The overall process introduces how to link a VB program to a database using LINQ to SQL and display records in a DataGridView.

Uploaded by

Sovanly Kim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views34 pages

Programming in Visual Basic 2010: The Very Beginner's Guide: Linq To SQL

The document provides instructions for connecting a Visual Basic program to a Microsoft Access database using LINQ to SQL. It describes how to create a database connection, select the Access file, test the connection, and save the connection string. The steps include adding a DataGridView control to display database records, selecting the data source property, choosing a database model, browsing to select the Access file, and clicking through dialog boxes to complete the connection. The overall process introduces how to link a VB program to a database using LINQ to SQL and display records in a DataGridView.

Uploaded by

Sovanly Kim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Programming in Visual Basic 2010:

The Very Beginner’s Guide

Chapter 14.1
LINQ to SQL

Databases – Part 1

by Jim McKeown
You will learn how to…

Describe a Relational Database Structure

Describe LINQ to SQL and how it's used

Create and populate a DataGridView

Write simple SQL commands

Use the QueryBuilder to create database queries

2
LINQ to SQL
The world runs on databases

Businesses depend on them for nearly all of their


business needs

LINQ to SQL is the link between a Visual Basic program


and a database

Significant programming and database instruction


needed to become proficient at it

General principles introduced here


3
Background

Databases are usually Relational databases


(RDBMS)
Part of a database management system (DBMS)
that includes servers, software and data
Data usually organized in a series of tables

Relational database:
a database with multiple tables and relationships
between tables
Database management system:
4 a database and the software to manage it
Background

Tables tied together by key fields


Key fields share a common name and have the
same data
Microsoft Access databases are similar to DBMS
but easier to use and widely available for a PC

Key fields
a field shared by two or more tables in a
database; key fields are the link between tables
in a relational database
5 like, ssan in one table; ssan in a second table.
Background

Database managers handle the data and


equipment; ensure data is available for business
processes.

Database manager:
a person who manages and maintains
databases; not as cool as a programmer, but
almost

6
Background
• In VB, Data Manipulation handled by Language-Integrated Query
(LINQ) and Structured Query Language (SQL); called LINQ to
SQL
• SQL is the language of most databases
• Language-Integrated Query (LINQ) -
• programming implementation that allows SQL
statements to be integrated into a VB program
• Structured Query Language (SQL)
• a database development language that allows for the searching, filtering,
organizing and reports of records from a database
• So, the database stores the data and your VB program provides
the interface to see the data.

• LINQ to SQL: the way to get data from database to screen.



7
Background

• Requests for data called a Query

• LINQ to SQL:
• the implementation of SQL statements into a program
using the tools available in .NET;
• a seamless way to connect to a database
• Query
• Search into a database;
• Queries
• Return specified fields from selected records, and
• Display them in a specific order
8 •
Background

• Queries determine
• what tables to use
• what records are requested
• what fields in the record to retrieve
• the conditions used to search through the data

• Whole books available on SQL


• SQL handles the database side
• LINQ manages the connection and the Visual
Basic side
9
Connecting to a Database Tutorial
• In general a developer “Selects” a database
and determines where to display the data

• This tutorial uses a Microsoft Access database

• Places data in what we call a DataGridView

• Wizard manages most of the decisions and does


most of the work

10
Connecting to a Database Tutorial

Connecting to a Database
11 Converting to the Database Grid is done for us.
Connecting to a Database Tutorial
• Use NEC.mdb database for the tutorial
• Copy it to your computer and put it in a place
that’s easy to find
• Desktop is fine.
• Open and inspect it but close it before starting the
tutorial

• I have placed the NEC.mdb on my webpage for you to


download.
• Open it, look at it, but close it.
• I assume you have Microsoft Access w/Office
• Click icon on your desktop and select NEC is left frame.
12
13
VB Tip

When an Access file is in use, it creates another


file, for NEC it's NEC.ldb.
This file locks the database so others cannot
change it.
Just don't try to have a database open in more
than one application at a time.

14
Connecting to a Database Tutorial

• 1. Create a VB solution and name it DatabaseTutorial


• Resize the form to 800, 600
• Add a DataGridView control (from ToolBox) and
• Name it dgvNEC
• DataGridView displays database records
• Database displays teachers’ database for NorthEast Central High
• DataGridView: a control used to display data from DataSet

• 2. Name form: frmDatabaseTutorial


• Save your solution
• Don’t wait because you won’t be able to establish a connection to
the database if your solution hasn’t been saved
15
Connecting to a Database Tutorial

• 3. Select DataSource property of dgvNEC


control
• so that you can add a DataSource to the DataGridView
• Selecting the DataSource property allows the
developer to select a specific database file

• DataSource property says None but has a down-arrow


in the property box.
• gives the following small window:

16
Connecting to a Database Tutorial

4. Add a Data Source

Click on link to
Add Project Data Source...

This will start a wizard that


guides us (the developer)
through the steps to
connect to a database

The notion of “connecting”


top a database is a critical
concept / term in the
17 database world.
Connecting to a Database Tutorial

Next two slides together: Select a Data Source Type

DataSource Configuration Wizard asks for data source type


Clearly we want a database to connect to.

5. Select Database icon and Click Next to connect to a


database

• Symbol for a database is a canister


• Creates a DataSet to hold database records in memory for quick and easy access
• Canister a symbol that represents a database; it looks something like a soup can
18
Connecting to a Database Tutorial

Data Source
Configuration
Wizard

Select
Database;

Click Next
button

19
Connecting to a Database Tutorial

Next Window: (not shown in book)


Select Database Model

6. Choose a Database Model


Select Dataset cannister, and Next

20
Connecting to the Database Tutorial

Next two slides together


Connect to a Database

7. Click on New Connection... Button


(see next slide)

DataSet:
• A place in memory that holds database records for quick and easy access in a
program;
• A DataSet stores changes, additions and deletions until they are sent back to
update the database
21
Connecting to a Database Tutorial

Data Source
Configuration
Wizard

Click on
New
Connection

22
Connecting to a Database Tutorial
Next two slides together.
Select source of your database
Select Microsoft Access (have other choices…)

When the Add Connection dialog appears,


• 8. Select Change...
• Change Data Source to Microsoft Access Database File
• Click OK

Establishes Microsoft Access as the type of database used in


this program
23 (This happens to be step 7 on page 574 in your book.)
Connecting to a Database Tutorial

Look up OLE DB!!


24
Connecting to a Database Tutorial
Next two slides together

9. Select Browse...locate the NEC.mdb file on desktop


Click on desktop icon to select it
Click on the Open button to add file and path to the
Database file name:
Click on Test Connection to check the connection
Click OK (completed Step 8 in book)

Add Connection dialog set us up to work with Access files


You get dialog box: Test Connection Succeeded.
User name and Password settings aren’t a concern
25
Connecting to a Database Tutorial

Add
Connection
Dialog

26
Connecting to a Database Tutorial

Next two slides together

10. Click Yes in the dialog box


Asks if you want to copy this file into your project folder
Happens every time local files are used
Files are then automatically copied every time the application runs

27
Connecting to a Database Tutorial

Local Connection
Warning Dialog

28
Connecting to a Database Tutorial

Next two slides together

11. Click Next to save the Connection String

29
Connecting to a Database Tutorial

Save
Connection
String
Dialog

30
Connecting to a Database Tutorial
Next two slides together
12. Select the Database Objects for the DataSet
Last step (Step 11 in your book)
Determines tables and fields available when your
program runs

Expand the Tables CheckBox


Select NEC (Selects the NEC table)
(We only have one table at this time.  )
Expand the NEC table to see the fields (attributes)
(Fields are already selected; Leave selected)
Click Finish
Now have connection between pgm and database!
31
Connecting to a Database Tutorial

Choose
Your
Database
Objects
Screen

See
expansions

32
Connecting to a Database Tutorial

Summary:

•DataGrid view adds and displays field names

•NEC.mdb files added to Solution Explorer

•Controls added to the component tray

•Run your application to see the data from the NEC


database
33
VB Tip

The connection between your program and a


database is temporary.

It lasts only long enough to transfer the data to


and from the DataSet.

In that sense, it's very similar to file I/O.

34

You might also like