Database, SQL, and ADO .NET- Part 1 Session 11
description
Transcript of Database, SQL, and ADO .NET- Part 1 Session 11
Database, SQL, and ADO .NET- Part 1Session 11
Mata kuliah : M0874 – Programming IITahun : 2010
Bina Nusantara University
3
Outline Materi
•Introduction•Relational Database Model•Structured Query Language•ADO .NET Object Model
Bina Nusantara University
4
Introduction
•A database is an integrated collection of data.•A database management system (DBMS) provides mechanisms for storing and organizing data in a manner that is consistent with the database’s format.•Database management systems enable programmers to access and store data without worrying about the internal representation of databases.•Today’s most popular database systems are relational databases.
Bina Nusantara University
5
Introduction
•Almost universally, relational databases use a language called Structured Query Language (SQL) to perform queries and to manipulate data.•A programming language connects to, and interacts with, a relational database via an interface-software that facilitates communication between a database management system and a program.•C# programmers communicate with databases and manipulate their data through Microsoft ActiveX Data ObjectsTM (ADO), ADO .NET.
Bina Nusantara University
6
Relational Database Model
•The relational database model is a logical representation of data that allows relationships among data to be considered without concern for the physical structure of data.
number
name department
salary
Location
23603 Jones 413 1100 New Jersey
24568 Kerwin 413 2000 New Jersey
34589 Larson 642 1800 Los Angeles
35761 Myers 611 1400 Orlando
78321 Neumann
413 9000 New Jersey
Record/Row
Primary key Field/Column
Bina Nusantara University
7
Structured Query Language (SQL)
•Basic SELECT QuerySELECT * FROM tableName
(*) indicates that all columns from the tableName table of the database should be selected.Example:
SELECT * FROM Authors
Bina Nusantara University
8
SQL Query KeywordsSQL Keyword Description
SELECT Selects (retrieves) fields from one or more tables
FROM Specifies tables from which to get fields or delete records. Required in every SELECT and DELETE statement.
WHERE Specifies criteria that determine the rows to be retrieved.
INNER JOIN Joins records from multiple tables to produce a single set of records.
GROUP BY Specifies criteria for grouping records.
ORDER BY Specifies criteria for ordering records.
INSERT Inserts data into a specified table.
UPDATE Updates data in a specified table.
DELETE Deletes data from a specified table.
Bina Nusantara University
9
WHERE Clause
•In most cases, users search a database for records that satisfy certain selection criteria.•Only records that match the selection criteria are selected.SELECT fieldName1, fieldName2, … FROM tableName
WHERE criteria
•Example:To select the name, salary, and location fields from table Employee in which the salary is greater than 1800.
Bina Nusantara University
10
WHERE Clause
SELECT name, salary, locationFROM EmployeeWHERE salary > 1800
number
name department
salary
Location
23603 Jones 413 1100 New Jersey
24568 Kerwin 413 2000 New Jersey
34589 Larson 642 1800 Los Angeles
35761 Myers 611 1400 Orlando
78321 Neumann
413 9000 New Jersey
11
ORDER BY Clause•The result of a query can be arranged in ascending or descending order using the optional ORDER BY clause.
SELECT fieldName1, fieldName2,…FROM tableName ORDER BY field ASCSELECT fieldName1, fieldName2,…FROM tableName ORDER BY field DESC
ASC specifies ascending order (lowest to highest)DESC specifies descending order (highest to lowest)Field specifies the field whose values determine the sorting orderBina Nusantara University
Bina Nusantara University
12
ORDER BY Clause
•Example:SELECT name, salary, locationFROM EmployeeORDER BY name ASC
Bina Nusantara University
13
Merging Data from Multiple Tables: INNER JOIN
•Database designers often split related data into separate tables to ensure that a database does not store data redundantly.•Often, it is necessary for analysis purposes to merge data from multiple tables into a single set of data.•Referred to as joining the tables, this is accomplished via an INNER JOIN operation in the SELECT query.•An INNER JOIN merges records from two or more tables by testing for matching values in a field that is common to the tables.
Bina Nusantara University
14
Merging Data from Multiple Tables: INNER JOIN
SELECT fieldName1, fieldName2, …FROM table1INNER JOIN table2
ON table1.fieldName = table2.fieldNameThe ON part of the INNER JOIN clauses specifies the fields from each table that are compared to determine which records are joined.
Bina Nusantara University
15
INSERT Statement
•The INSERT statement inserts a new record in a table. INSERT INTO tableName ( fieldName1, fieldName2, …, fieldNameN ) VALUES ( value1, value2, …, valueN )
Bina Nusantara University
16
ADO .NET Object Model
•ADO.NET is an object-oriented set of libraries that allows you to interact with data sources.•Commonly, the data source is a database, but it could also be a text file, an Excel spreadsheet, or an XML file.•The ADO .NET object model provides an API for accessing database systems programmatically.•ADO .NET was created for the .NET Framework and is the next generation of ActiveX Data ObjectsTM (ADO).•Namespace System.Data is the root namespace for the ADO .NET API.
Bina Nusantara University
17
ADO .NET Object Model
•ADO.NET allows us to interact with different types of data sources and different types of databases.•Different data sources expose different protocols, we need a way to communicate with the right data source using the right protocol.
ADO .NET Object Model
•ADO.NET Data Providers are class libraries that allow a common way to interact with specific data sources or protocols. The library APIs have prefixes that indicate which provider they support.
Provider NameAPI prefix
Data Source Description
ODBC Data Provider
OdbcData Sources with an ODBC interface. Normally older data bases.
OleDb Data Provider
OleDbData Sources that expose an OleDb interface, i.e. Access or Excel.
Oracle Data Provider
Oracle For Oracle Databases.
SQL Data Provider Sql For interacting with Microsoft SQL Server.
Borland Data Provider
BdpGeneric access to many databases such as Interbase, SQL Server, IBM DB2, and Oracle.
Bina Nusantara University
19
ADO .NET Object Model
•ADO.NET includes many objects you can use to work with data. •The SqlConnection Object
•To interact with a database, you must have a connection to it. The connection helps identify the database server, the database name, user name, password, and other parameters that are required for connecting to the data base. A connection object is used by command objects so they will know which database to execute the command on.
20
ADO .NET Object Model
•The SqlCommand Object•The process of interacting with a database means that you must specify the actions you want to occur. This is done with a command object. You use a command object to send SQL statements to the database.
•The SqlDataReader Object•Many data operations require that you only get a stream of data for reading. The data reader object allows you to obtain the results of a SELECT statement from a command object. For performance reasons, the data returned from a data reader is a fast forward-only stream of data. This means that you can only pull the data from the stream in a sequential manner. This is good for speed, but if you need to manipulate data, then a DataSet is a better object to work with.
Bina Nusantara University
21
ADO .NET Object Model
•The DataSet Object•DataSet objects are in-memory representations of data. They contain multiple Datatable objects, which contain columns and rows, just like normal database tables.
•The SqlDataAdapter Object•Sometimes the data you work with is primarily read-only and you rarely need to make changes to the underlying data source. Some situations also call for caching data in memory to minimize the number of database calls for data that does not change. The data adapter makes it easy for you to accomplish these things by helping to manage data in a disconnected mode. The data adapter fills a DataSet object when reading the data and writes in a single batch when persisting changes back to the database.
Bina Nusantara University
22
The SqlConnection Object
•A SqlConnection is an object, just like any other C# object. SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
Common parts of a connection stringConnection String Parameter Name
Description
Data SourceIdentifies the server. Could be local machine, machine domain name, or IP Address.
Initial Catalog Database name.
Integrated SecuritySet to SSPI to make connection with user's Windows login
User ID Name of user configured in SQL Server.
Password Password matching SQL Server User ID.
Bina Nusantara University
23
The SqlConnection Object
•The following shows a connection string, using the User ID and Password parameters:SqlConnection conn = new SqlConnection("Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Pass=YourPass");
Bina Nusantara University
24
Using a SqlConnection
•The purpose of creating a SqlConnection object is so you can enable other ADO.NET code to work with a database.•Other ADO.NET objects, such as a SqlCommand and a SqlDataAdapter take a connection object as a parameter. •The sequence of operations occurring in the lifetime of a SqlConnection are as follows:
1. Instantiate the SqlConnection. 2. Open the connection. 3. Pass the connection to other ADO.NET objects. 4. Perform database operations with the other ADO.NET
objects. 5. Close the connection.
Bina Nusantara University
25
Bina Nusantara University
26
References
•http://www.csharp-station.com/Tutorials/ADODotNet/Lesson01.aspx