Introduction to SQL, OleDB interface to Access from VB.NET.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Advertisements

CSC271 Database Systems Lecture # 11.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Structured Query Language Part I Chapter Three CIS 218.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Introduction to Database Systems Microsoft Access, OleDB.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Analyzing Data For Effective Decision Making Chapter 3.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Concepts of Database Management Seventh Edition
Database Systems Microsoft Access Practical #3 Queries Nos 215.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
BY SATHISH SQL Basic. Introduction The language Structured English Query Language (SEQUEL) was developed by IBM Corporation, Inc., to use Codd's model.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
HNDIT Rapid Application Development
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
1 Working with MS SQL Server Beginning ASP.NET in C# and VB Chapter 12.
Using Database: A very, very short introduction..
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL Introduction SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel – (Structured English.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Accessing Databases using Ado.net
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Structured Query Language
Chapter 5 Introduction to SQL.
MySQL Subquery Source: Dev.MySql.com
ADO.NET Framework.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
JDBC.
Structured Query Language (SQL) William Klingelsmith
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Tonga Institute of Higher Education
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL LANGUAGE and Relational Data Model TUTORIAL
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Access: SQL Participation Project
M1G Introduction to Database Development
Introduction To Structured Query Language (SQL)
Presentation transcript:

Introduction to SQL, OleDB interface to Access from VB.NET

SQL Structured Query Language, abbreviated SQL – Usually pronounced “sequel” but also “ess-cue- ell”) – The common language of client/server database management systems. – Standardized – you can use a common set of SQL statements with all SQL-compliant systems. – Defined by E.F. Codd at IBM research in – Based on relational algebra and predicate logic

SQL Data Retrieval Given an existing database, the SELECT statement is the basic statement for data retrieval. – Both simple and complex, and it may be combined with other functions for greater flexibility. SELECT data_element1 [, {data_element2 | function(..)} ]Or * FROMtable_1, [, table_2, …] [ WHEREcondition_1 [, {not, or, and} condition_2] ] [ GROUP BY data_1, … ] [ HAVINGaggregate function(…)… ] [ORDER BYdata1, … ]

SELECT statement Some sample aggregate functions: – COUNT(*)SUM(item) – AVG(item)MAX(item) – MIN(item) Conditional Operators – =Equal – <Less than – >Greater than – <>,!=Not equal to – <=Less than or equal to – >=Greater than or equal to

SELECT Examples Select every row, column from the table: – SELECT * FROM Orders; – SELECT Orders.cust_id, Orders.prod_id, Orders.cost, Orders.salesperson FROM Orders; Returns a set of all rows that match the query

SELECT If a table has spaces or certain punctuation in it, then Access needs to have the items enclosed in square brackets []. The previous query is identical to the following: – SELECT [orders].[cust_id], orders.prod_id, orders.cost, orders.[salesperson] FROM Orders;

SELECT Query in Access Can flip back and forth between SQL View, Run, and Design Mode SQL Run Design

More SELECT Statements Note that we can have duplicates as a result of the selection. If we want to remove duplicates, we can use the DISTINCT clause: SELECT DISTINCT Orders.cust_id FROM Orders; We can combine a selection and a projection by using the WHERE clause: SELECT Orders.cust_id FROM Orders WHERE Salesperson = “Jones”; This could be used if we wanted to get all the customers that Jones has sold to, in this case, CUST_ID=101 and CUST_ID=100. By default, Access is not case-sensitive, so “jones” would also result in the same table.

More SELECT We can further refine the query by adding AND, OR, or NOT conditions. If we want orders from Jones or from Smith then the query becomes: SELECT Orders.cust_id FROM Orders WHERE Salesperson = “Jones” or Salesperson = “Smith”; Another refinement is to use the BETWEEN operator. If we want only those orders between 10 and 100 then we could define this as: SELECT Orders.cust_id, Orders.cost FROM Orders WHERE Orders.cost >10 and Orders.cost <100; Or use the between operator: SELECT Orders.cust_id, Orders.cost FROM Orders WHERE Orders.cost BETWEEN 10 and 100;

Finally, we might want to sort the data on some field. We can use the ORDER BY clause: SELECT Orders.cust_id, Orders.cost FROM Orders WHERE Orders.cost >10 and Orders.cost <100 ORDER BY Orders.cost; This sorts the data in ascending order of cost. An example is shown in the table: CUST_IDCOST If we wanted to sort them in descending order, use the DESC keyword: SELECT Orders.cust_id, Orders.cost FROM Orders WHERE Orders.cost >10 and Orders.cost <100 ORDER BY Orders.cost DESC; More SELECT

Joining Data from Multiple Tables If our data is in multiple tables we can join them together in one query. – Use a JOIN operator (Access default w/Design view) – Add tables to the FROM, WHERE section (what we will use here) Say we have the following table in addition to Orders:

Multiple Tables SELECT Orders.cust_id, Customer.Cust_Name FROM Orders, Customer WHERE Orders.cost >10 and Orders.cost <100; What do you expect from this query? Result: 100Thomas Jefferson 101Thomas Jefferson 102Thomas Jefferson 100Bill Clinton 101Bill Clinton 102Bill Clinton 100George Bush 101George Bush 102George Bush PRODUCT of two tables!

Multiple Tables Need to link the tables by their common field, the customer ID: SELECT Orders.cust_id, Customer.Cust_Name FROM Orders, Customer WHERE Orders.cust_id = Customer.Cust_Id and Orders.cost >10 and Orders.cost <100; Result: 100Thomas Jefferson 101Bill Clinton 102George Bush

INSERT command Allows you to insert single or multiple rows of data into a table INSERT INTO table [(column-list)] [VALUES (value-list) | sql-query]

INSERT examples Given mytable(field1 as currency, field2 as text, field3 as integer): INSERT INTO mytable (field1, field2, field3) VALUES (12.10, “bah”,20); Adds a new row to the table mytable If you don’t specify every field then fields left out get the default: INSERT INTO mytable (field1, field2) VALUES(24.2, “zot”); Adds only for field1 and field2.

INSERT Examples INSERT INTO ORDERS (CUST_ID, PROD_ID, COST, SALESPESON) VALUES (103, ‘Y338’, 55, ‘Smith’); INSERT INTO ORDERS (PROD_ID, COST, SALESPESON) VALUES (‘Y638’, 155, ‘Smith’); Second might be useful if the CUST_ID is an autonumber field

DELETE Delete will remove a row from the table. DELETE FROM table_name [WHERE search- condition] Examples: DELETE FROM mytable1; Removes all rows! DELETE FROM mytable1 WHERE field1 > 100; Removes only rows with field1>100

UPDATE Update lets you modify the contents of the data. UPDATE table_name SET field_name = expression [, field-name=expression …] [WHERE search-condition] UPDATE mytable SET field1 = 0.0; Changes all field1’s to zero for every row! UPDATE mytable SET field1 = 0.0, field2 = “woof”; Sets field1 to 0 and field2 to woof for all rows! If this is a violation, access will prevent it from happening UPDATE mytable SET field1 = 25.0 WHERE field2=“foo”; Only updates the field where field2 is “foo”

SQL Queries There are a lot more queries, but that should give you an idea of what is possible and how it is done Next we’ll go over an example that uses SQL on an Access Database from VB.NET – Uses OleDB which is different from the book – Database access technology changes rapidly

OleDB in VB.NET Add to the top: Imports System.Data.OleDb Set the connection string: – This tells VB.NET where the database is and how to connect to it: Public Class Form1 Dim connectionString As String Private Sub Form1_Load(...) Handles MyBase.Load connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Path\To\database.accdb" End Sub For Office 2007

Example Reading from the DB Dim cn As New OleDbConnection(connectionString) cn.Open() Dim cmd As New OleDbCommand("SELECT * From Students WHERE Lastname >= 'M'", cn) cmd.ExecuteNonQuery() Dim reader As OleDbDataReader = cmd.ExecuteReader() While (reader.Read()) Dim ID As Integer = Convert.ToInt32(reader("ID")) Dim Name As String = Convert.ToString(reader("LastName")) Dim DOB As Date = Convert.ToDateTime(reader("DOB")) Console.WriteLine(ID.ToString() + " " + Name + " " + DOB.ToString()) End While cn.Close()

Example Writing to the DB Dim cn As New OleDbConnection(connectionString) cn.Open() Dim newLastName As String = "Washington“ ' ID is auto-update field so its left out of the insert ' Put single quotes around String fields, # around dates Dim sql As String = "INSERT INTO Students (LastName, FirstName, DOB, Address) " + _ "VALUES ('" + newLastName + "', 'George', #04/19/2005#, '999 C St.')“ Dim cmd As New OleDbCommand(sql, cn) cmd.ExecuteNonQuery() Console.WriteLine("Executed command " + sql) cn.Close()