[Student] Ado.net and SQL

Embed Size (px)

Citation preview

  • 7/30/2019 [Student] Ado.net and SQL

    1/41

    ADO.NET AND

    STRUCTURED QUERY

    LANGUAGE (SQL)Database Terminology, ADO.NET, Connecting toDatabase, SQL Statements

  • 7/30/2019 [Student] Ado.net and SQL

    2/41

    Database

    Relational Database

    Entity

    Table

    AttributeColumn

    Keys

    Review: DatabaseFundamentals

  • 7/30/2019 [Student] Ado.net and SQL

    3/41

    3

    Databases

    Database

    A database is a collection of related data which is

    organized to meet user needs

    Relational database

    database that stores information in tables

    Each column represents a field

    Each row represents a record

    3

    3

  • 7/30/2019 [Student] Ado.net and SQL

    4/41

    Entities

    An enti ty 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.

    4

  • 7/30/2019 [Student] Ado.net and SQL

    5/41

    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

    5

  • 7/30/2019 [Student] Ado.net and SQL

    6/41

    Attributes and Columns

    An attr ibute 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 inventoryof parts.

    A co lumn , orf ield, is a physical

    implementation of an attribute.

    6

  • 7/30/2019 [Student] Ado.net and SQL

    7/41

    Keys

    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 IdEmployee number

    Part number

    Social Security Number

    7

  • 7/30/2019 [Student] Ado.net and SQL

    8/41

    What is ADO.NET

    ADO.NET Objects

    Data Providers

    ADO.NET

  • 7/30/2019 [Student] Ado.net and SQL

    9/41

    What is ADO.NET?

    It is a Microsoft Technology.

    Stands forActiveX Data Objects

    It is a programming language interface toaccess data in a database.

    9

    9

  • 7/30/2019 [Student] Ado.net and SQL

    10/41

    10

    ADO.NET

    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

  • 7/30/2019 [Student] Ado.net and SQL

    11/41

    .NET Framework Data Providers

    A .NET Framework data provider is used for connecting

    to a database, executing commands, and retrieving

    results.

    .NET Frameworkdata provider Description

    SQL Server Provides data access for Microsoft SQL Server. Uses

    the System.Data.SqlClient namespace.

    OLE DB For data sources exposed by using OLE DB. Uses

    the System.Data.OleDb namespace.

    ODBC For data sources exposed by using ODBC. Uses

    the System.Data.Odbc namespace.

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

    11

  • 7/30/2019 [Student] Ado.net and SQL

    12/41

    Connection Object (1/3)

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

    12

  • 7/30/2019 [Student] Ado.net and SQL

    13/41

    Connection Object (2/3)

    Sample web.config (using OleDb provider):

    Using SqlClient provider

    13

  • 7/30/2019 [Student] Ado.net and SQL

    14/41

    Connection Object (3/3)

    How to use the connection string inside the web.config

    file:

    Include using System.Configuration

    Using OleDB connection object

    Using SqlClient connection object

    14

    string conn =

    ConfigurationManager.ConnectionStrings[MyConnectionString"].ConnectionString;

    OleDbConnection myCon = new OleDbConnection(conn);

    string conn =ConfigurationManager.ConnectionStrings[ConnectionString"].ConnectionString;

    SqlConnection myCon = new SqlConnection(conn);

  • 7/30/2019 [Student] Ado.net and SQL

    15/41

    Command Object (1/2)

    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 tofigure out which database to communicate with.

    Sample code:

    15

    SqlCommand cmd;

    cmd= new SqlCommand(sql, conObject);cmd.ExecuteNonQuery();

  • 7/30/2019 [Student] Ado.net and SQL

    16/41

    Command Object (2/2)

    Some Command object methods:

    cmd.ExecuteReader() Returns one or more

    table row(s) for select statement

    cmd.ExecuteNonQuery() Used for executing

    stored procedure or insert/update/delete

    statements

    16

  • 7/30/2019 [Student] Ado.net and SQL

    17/41

    17

    DataReader Object

    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);

    }

  • 7/30/2019 [Student] Ado.net and SQL

    18/41

    18

    DataAdapter Object

    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

  • 7/30/2019 [Student] Ado.net and SQL

    19/41

    Brief Introduction to SQL

    Types of SQL Statements

    Structured Query Language (SQL)

  • 7/30/2019 [Student] Ado.net and SQL

    20/41

    20

    SQL

    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 toeither return a set of records or perform a specific actionon a set of records as specified in the query

  • 7/30/2019 [Student] Ado.net and SQL

    21/41

    SQL Commands

    SELECT

    UPDATE

    INSERT INTO DELETE

    21

  • 7/30/2019 [Student] Ado.net and SQL

    22/41

    SELECT Statement

    Selects/extracts data from one or more database

    tables.

    Syntax:

    22

    SELECT columnName1, columnName2,FROM tableName[WHERE condition]

    [ORDER BY columnName ASC|DESC]

  • 7/30/2019 [Student] Ado.net and SQL

    23/41

    Table Name: Student

    To select all Lastname from the above table, we will use this

    SQL SELECT statement:

    Result:

    23

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU201001423 Luis BCS

    SELECT Statement Example 1

  • 7/30/2019 [Student] Ado.net and SQL

    24/41

    Table Name: Student

    To select all records ofBCS studentsfrom the above table,

    we will use this SQL SELECT statement:

    Result:

    24

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU201001423 Luis BCS

    SELECT Statement Example 2

  • 7/30/2019 [Student] Ado.net and SQL

    25/41

    Table Name: Student

    To display all Course in ascending order from the above

    table, we will use this SQL SELECT statement:

    Result:

    25

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU201001423 Luis BCS

    SELECT Statement Example 3

  • 7/30/2019 [Student] Ado.net and SQL

    26/41

    Table Name: Student

    What is the result of the SQL statement below?

    SELECT Course FROM Student WHERE Lastname IN (Cu,

    Luis, Que)

    Result:

    26

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU201001423 Luis BCS

    SELECT Statement Example 4

  • 7/30/2019 [Student] Ado.net and SQL

    27/41

    Table Name: Student

    If we want to know the lastname and course of all students

    aged 16 to 20, the SQL statement is:

    Result:

    27

    StudentID Lastname Course Age

    200601137 Sabale BCS 17

    200600459 Que HRM 19

    200601020 Lumandas JOU 20201001423 Luis BCS 16

    SELECT Statement Example 5

  • 7/30/2019 [Student] Ado.net and SQL

    28/41

    UPDATE Statement

    Updates data in a database table.

    Syntax:

    28

    UPDATE tableNameSET colName1 = newValue, colName2 = newValue,[WHERE condition]

  • 7/30/2019 [Student] Ado.net and SQL

    29/41

    Table Name: Student

    To change the course of all students to BCS, we will use this

    SQL UPDATE statement:

    Result:

    29

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU201001423 Luis BCS

    UPDATE Statement Example 1

  • 7/30/2019 [Student] Ado.net and SQL

    30/41

    Table Name: Student

    To change the lastname to Sarnoand course to MSCS of

    student id 20061137, the SQL statement is:

    Result:

    30

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU201001423 Luis BCS

    UPDATE Statement Example 2

  • 7/30/2019 [Student] Ado.net and SQL

    31/41

    Table Name: Student

    What is the result of the following command?

    UPDATE Student SET Lastname= Sarno WHERE StudentID

    = 200601137 AND Course = JOU

    Result:

    31

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU201001423 Luis BCS

    UPDATE Statement Example 3

  • 7/30/2019 [Student] Ado.net and SQL

    32/41

    Table Name: Student

    What is the result of the following command?

    UPDATE Student SET Lastname= Masdal WHERE

    StudentID = 200600459 OR Course = JOU

    Result:

    32

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU201001423 Luis BCS

    UPDATE Statement Example 4

  • 7/30/2019 [Student] Ado.net and SQL

    33/41

    INSERT INTO Statement

    Inserts new data into a database table

    Syntax:

    Or:

    You are allowed to omit the list of column names in theINSERT INTO clause, if you enter values for each of the table

    columns.

    33

    INSERT INTO

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

    INSERT INTO tableName

    VALUES (value1, value2, value3,...)

  • 7/30/2019 [Student] Ado.net and SQL

    34/41

    Table Name: Student

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

    Result:

    34

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU

    INSERT INTO Statement Example 1

  • 7/30/2019 [Student] Ado.net and SQL

    35/41

    Table Name: Student

    To insert this data (201001234) to the table:

    Result:

    35

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU

    INSERT INTO Statement Example 2

  • 7/30/2019 [Student] Ado.net and SQL

    36/41

    DELETE Statement

    Delete records in a table

    Syntax:

    Or, to delete all records in a table:

    Note: Be very careful when deleting records. You cannot

    undo this statement.

    36

    DELETE FROM tableNameWHERE someColumn=someValue

    DELETE FROM tableName

  • 7/30/2019 [Student] Ado.net and SQL

    37/41

    Table Name: Student

    To delete the student Lumandas with course JOU in the

    table:

    Result:

    37

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU

    201001423 Luis BCS

    DELETE Statement Example 1

  • 7/30/2019 [Student] Ado.net and SQL

    38/41

    Table Name: Student

    To delete all records in table Student:

    Result:

    38

    StudentID Lastname Course

    200601137 Sabale BCS

    200600459 Que HRM

    200601020 Lumandas JOU

    201001423 Luis BCS

    DELETE Statement Example 2

    St i S l ti R d f

  • 7/30/2019 [Student] Ado.net and SQL

    39/41

    1. Create a DataAdapter.

    2. Create a DataTable.

    3. Fill/ populate the table using the DataAdapter.

    4. Bind to a data-bound control.

    39

    Steps in Selecting Records from a

    Database

    SqlDataAdapter da;

    da = new SqlDataAdapter("SELECT * FROM tableName", con);

    DataTable dt = new DataTable();

    da.Fill(dt);

    sqlDataSource1.DataSource = dt;

    St i I ti U d ti

  • 7/30/2019 [Student] Ado.net and SQL

    40/41

    1. Create an ADO.NET connection to a database.

    2. Create a String the represents the SQL command.

    3. Create a Command object.

    4. Open the connection.

    5. Execute the SQL statement.

    6. Close the connection.

    40

    Steps in Inserting, Updating, or

    Deleting Records in a Database

    SqlConnection con = new SqlConnection(connectionString);

    con.Open();

    SqlCommand com = new SqlCommand(sql, con);

    com.ExecuteNonQuery();

    con.Close();

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

  • 7/30/2019 [Student] Ado.net and SQL

    41/41

    Reference

    Introduction to C# Programming with

    Microsoft.NET

    http://msdn.microsoft.com

    http://csharp-station.com/Tutorial/AdoDotNet/Lesson01