Вы находитесь на странице: 1из 41

ADO.

NET AND STRUCTURED QUERY LANGUAGE (SQL)


Database Terminology, ADO.NET, Connecting to Database, SQL Statements

Review: Database Fundamentals


Database Relational Database Entity Table Attribute Column Keys

Databases
3

Database
A

database is a collection of related data which is organized to meet user needs that stores information in tables

Relational database
database
Each

column represents a field Each row represents a record

Entities
4

An entity is a class of objects, such as people, products, or services, about which we collect data. For example,
Students

at the university. Employees at a business. Sales transactions in a retail business. Inventory of parts in a warehouse.

Tables

Table
a

group of related records a physical implementation of an entity in a DBMS

Relational database contains one or more tables Primary key


a

field that uniquely identifies each record in a table

Attributes and Columns


6

An attribute is a characteristic of an entity Examples of attributes


Names,

addresses, phone numbers of people. Titles, salaries, job descriptions of employees. Time, date, customer id, sales person id in a sales transaction. Part number, Cost, In-stock-quantity of inventory of parts.

A column, or field, is a physical implementation of an attribute.

Keys
7

Keys are the attributes that are used to uniquely identify each instance of an entity, i.e. each row in a table The uniqueness can naturally occur like Social Security numbers or can be created by the database management system. For example,
Student

Id Employee number Part number Social Security Number

ADO.NET
What is ADO.NET ADO.NET Objects Data Providers

What is ADO.NET?
9

It is a Microsoft Technology. Stands for ActiveX Data Objects It is a programming language interface to access data in a database.

ADO.NET
10

To connect an application to a database, ASP.NET uses ADO.NET With ADO.NET, the connection between an application and a database is a temporary one. ADO.NET objects and provider are needed to access a database from a ASP.NET application:
Connection Command DataReader

.NET Framework Data Providers


11

A .NET Framework data provider is used for connecting to a database, executing commands, and retrieving results.
Description
Provides data access for Microsoft SQL Server. Uses the System.Data.SqlClient namespace. For data sources exposed by using OLE DB. Uses the System.Data.OleDb namespace. For data sources exposed by using ODBC. Uses the System.Data.Odbc namespace. For Oracle data sources. The .NET Framework Data Provider for Oracle supports Oracle client software version 8.1.7 and later, and uses the System.Data.OracleClient namespace.

.NET Framework data provider


SQL Server OLE DB ODBC Oracle

Connection Object (1/3)


12

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. In ASP.NET, we are going to store the connection string needed by the connection object in a web.config file.

Connection Object (2/3)


13

Sample web.config (using OleDb provider):

Using SqlClient provider

Connection Object (3/3)


14

How to use the connection string inside the web.config file:


Include using System.Configuration string = connection object Using conn OleDB ConfigurationManager.ConnectionStrings[MyConnection String"].ConnectionString;

OleDbConnection myCon = new OleDbConnection(conn);


Using conn SqlClient string =

connection object ConfigurationManager.ConnectionStrings[ConnectionSt ring"].ConnectionString;


SqlConnection myCon = new SqlConnection(conn);

Command Object (1/2)


15

The process of interacting with a database means that you must specify the actions you want to occur. A command object sends SQL statements to the database. A command object uses a connection object to figure out which database to communicate with. Sample code:
SqlCommand cmd; cmd= new SqlCommand(sql, conObject); cmd.ExecuteNonQuery();

Command Object (2/2)


16

Some Command object methods:


Returns one or more table row(s) for select statement cmd.ExecuteNonQuery() Used for executing stored procedure or insert/update/delete statements
cmd.ExecuteReader()

DataReader Object
17

Reads a forward-only, read-only stream of data from a data source. Sample code:
SqlCommand cmd; cmd= new SqlCommand(SELECT * FROM Students, myCon); SqlDataReader dr= cmd.ExecuteReader(); while (dr.Read()) { string name = dr.GetString(0); string year = dr.GetString(1); string course = dr.GetString(2); lstIOutput.Items.Add(Name = + name); lstIOutput.Items.Add(Year = + year); lstIOutput.Items.Add(Course = + course); }

DataAdapter Object
18

Populates a DataSet/ DataTable and resolves updates with the data source.
DataSet
contains

a collection of zero or more tables represented by DataTable objects

DataTable
represents

a single table of memory-resident data Defined in the System.Data namespace

Structured Query Language (SQL)


Brief Introduction to SQL Types of SQL Statements

SQL
20

SQL (Structured Query Language): a set of commands to access and manipulate the data stored in many database management systems SQL commands perform database tasks such as storing, retrieving, updating, deleting, and sorting. In SQL, we will write queries. A query is a formalized instruction to a database to either return a set of records or perform a specific action on a set of records as specified in the query

SQL Commands
21

SELECT UPDATE INSERT INTO DELETE

SELECT Statement
22

Selects/extracts data from one or more database tables. Syntax:


SELECT columnName1, columnName2, FROM tableName [WHERE condition] [ORDER BY columnName ASC|DESC]

SELECT Statement Example 1


23

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

To select all Lastname from the above table, we will use this SQL SELECT statement:

Result:

SELECT Statement Example 2


24

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

To select all records of BCS students from the above table, we will use this SQL SELECT statement:

Result:

SELECT Statement Example 3


25

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

To display all Course in ascending order from the above table, we will use this SQL SELECT statement:

Result:

SELECT Statement Example 4


26

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

What is the result of the SQL statement below? SELECT Course FROM Student WHERE Lastname IN (Cu, Luis, Que) Result:

SELECT Statement Example 5


27

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

Age
17 19 20 16

If we want to know the lastname and course of all students aged 16 to 20, the SQL statement is:

Result:

UPDATE Statement
28

Updates data in a database table.


Syntax:
UPDATE tableName SET colName1 = newValue, colName2 = newValue, [WHERE condition]

UPDATE Statement Example 1


29

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

To change the course of all students to BCS, we will use this SQL UPDATE statement:

Result:

UPDATE Statement Example 2


30

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

To change the lastname to Sarno and course to MSCS of student id 20061137, the SQL statement is:

Result:

UPDATE Statement Example 3


31

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

What is the result of the following command? UPDATE Student SET Lastname= Sarno WHERE StudentID = 200601137 AND Course = JOU Result:

UPDATE Statement Example 4


32

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

What is the result of the following command? UPDATE Student SET Lastname= Masdal WHERE StudentID = 200600459 OR Course = JOU Result:

INSERT INTO Statement


33

Inserts new data into a database table


Syntax:

INSERT INTO tableName (column1, column2, column3,...) VALUES (value1, value2, value3,...)

Or: You are allowed to omit the list of column names in the INSERT INTO clause, if you enter values for each of the table columns.

INSERT INTO tableName VALUES (value1, value2, value3,...)

INSERT INTO Statement Example 1


34

Table Name: Student


StudentID Lastname Course

200601137
200600459 200601020

Sabale
Que Lumandas

BCS
HRM JOU

To insert this data (201001520, Rosario, MID) to the table:

Result:

INSERT INTO Statement Example 2


35

Table Name: Student


StudentID Lastname Course

200601137
200600459 200601020

Sabale
Que Lumandas

BCS
HRM JOU

To insert this data (201001234) to the table:

Result:

DELETE Statement
36

Delete records in a table Syntax:


DELETE FROM tableName WHERE someColumn=someValue

Or, to delete all records in a table:


DELETE FROM tableName

Note: Be very careful when deleting records. You cannot undo this statement.

DELETE Statement Example 1


37

Table Name: Student


StudentID
200601137 200600459 200601020 201001423

Lastname
Sabale Que Lumandas Luis

Course
BCS HRM JOU BCS

To delete the student Lumandas with course JOU in the table:

Result:

DELETE Statement Example 2


38

Table Name: Student


StudentID 200601137 200600459 200601020 201001423 Lastname Sabale Que Lumandas Luis Course BCS HRM JOU BCS

To delete all records in table Student: Result:

39

Steps in Selecting Records from a Database


1.

Create a DataAdapter.
SqlDataAdapter da;
da = new SqlDataAdapter("SELECT * FROM tableName", con);

2.

Create a DataTable.
DataTable dt = new DataTable();

3.

Fill/ populate the table using the DataAdapter.


da.Fill(dt);

4.

Bind to a data-bound control.


sqlDataSource1.DataSource = dt;

40

Steps in Inserting, Updating, or Deleting Records in a Database


1.

Create an ADO.NET connection to a database.


SqlConnection con = new SqlConnection(connectionString);

2.

Create a String the represents the SQL command.


String sql = [INSERT INTO] or [UPDATE] or [DELETE] command

3.

Create a Command object.


SqlCommand com = new SqlCommand(sql, con);

4.

Open the connection.


con.Open();

5.

Execute the SQL statement.


com.ExecuteNonQuery();

6.

Close the connection.


con.Close();

Reference

Introduction to C# Programming with Microsoft.NET http://msdn.microsoft.com http://csharpstation.com/Tutorial/AdoDotNet/Lesson01

Вам также может понравиться