Notes - Lesson 11a

download Notes - Lesson 11a

of 16

Transcript of Notes - Lesson 11a

  • 8/4/2019 Notes - Lesson 11a

    1/16

    Lesson 11:Developing Database

    Applications

    Lesson 11: Developing Database Applications 2

    Objectives

    What will you learn

    What is ADO .Net

    What are .Net Data Providers

    How to connect to a database using a wizard

    How to retrieve data from a database using a wizard

    Lesson 11: Developing Database Applications 3

    Introduction

    ADO.NET provides access to data sources such asMicrosoft SQL Server, as well as data sources exposedthrough OLE DB

    Applications can use ADO.NET to connect to these data

    sources and retrieve, manipulate, and update data

  • 8/4/2019 Notes - Lesson 11a

    2/16

    Lesson 11: Developing Database Applications 4

    Introduction

    ADO .NET Namespaces

    System.Data- The root namespace for the ADO .NETAPI

    System.Data.OleDb- It contains classes that aredesigned to work with any data source

    System.Data.SqlClient- Contains classes that areoptimized to work with Microsoft SQL Server databases

    Lesson 11: Developing Database Applications 5

    .NET Data Providers

    .NET Framework Data Provider for SQL Server- forMicrosoft SQL Server version 7.0 or later

    .NET Framework Data Provider for OLE DB- for datasources exposed using OLE DB

    .NET Framework Data Provider for ODBC- for datasources exposed using ODBC

    .NET Framework Data Provider for Oracle- for Oracledata sources, the .NET Framework Data Provider forOracle supports Oracle client software version 8.1.7 andlater

    Lesson 11: Developing Database Applications 6

    Connecting to Data Sources

    Connection

    Command

    DataAdapter

    DataReader

    ADO .Net Architecture

    DataSet

    Data Consumers

    .Net Data Provider

    Database

    Win Forms

    Web Forms

  • 8/4/2019 Notes - Lesson 11a

    3/16

    Lesson 11: Developing Database Applications 7

    Connecting to Data Sources

    Connection Class

    In ADO.NET you use a Connection object to connect toa specific data source. For example, to connect toMicrosoft SQL Server version 7.0 or later, use theSqlConnection object of the .NET Framework DataProvider for SQL Server. Use the correspondingConnection object to connect to a data source.

    Lesson 11: Developing Database Applications 8

    Connecting to Data Sources

    The Data Source Configuration Wizard

    Using the Data Source Configuration Wizard is an easyway of connecting applications to databases

    When the wizard is finished, data will be available in theData Sources Window and is ready for dragging it ontoforms

    Lesson 11: Developing Database Applications 9

    Connecting to Data Sources

    Create a new project:

    Select New then Project in the File menu

    Select Windows Application and click OK

  • 8/4/2019 Notes - Lesson 11a

    4/16

    Lesson 11: Developing Database Applications 10

    Connecting to Data Sources

    Create a connection to a database:

    Select Add New Data Source on the Data menu

    Lesson 11: Developing Database Applications 11

    Connecting to Data Sources

    Select Database on the Choose a Data Source Typepage then select Next

    Lesson 11: Developing Database Applications 12

    Connecting to Data Sources

    Select New Connection to configure a new dataconnection if there are no connections yet

  • 8/4/2019 Notes - Lesson 11a

    5/16

    Lesson 11: Developing Database Applications 13

    Connecting to Data Sources

    Check Yes if you want to save the connection stringthen click Next

    Lesson 11: Developing Database Applications 14

    Connecting to Data Sources

    Expand the Tables node and select the tables you wantto add to your Data Source and then click Finish

    Lesson 11: Developing Database Applications 15

    Connecting to Data Sources

    Add controls to the form:

    Drag the table nodes you want from the Data Sourceswindow onto the form

  • 8/4/2019 Notes - Lesson 11a

    6/16

    Lesson 11: Developing Database Applications 16

    Connecting to Data Sources

    Run the application:

    Press F5 to run the application. The form should show

    the data from the table you just dragged.

    Lesson 11: Developing Database Applications 17

    Connecting to Data Sources

    Using the Dataset Designer

    After this lesson, you will be able to create tables basedon tables in a database without using a wizard

    The Dataset Designer is a tool for creating and editingtyped datasets like TableAdapters, TableAdapterQueriesand Data Tables

    Lesson 11: Developing Database Applications 18

    Connecting to Data Sources

    Add a new dataset item to the project:

    Click Add New Item on the Project menu

    Click DataSet in the Templates box

  • 8/4/2019 Notes - Lesson 11a

    7/16

    Lesson 11: Developing Database Applications 19

    Connecting to Data Sources

    The Dataset you added will be opened in the DatasetDesigner

    Lesson 11: Developing Database Applications 20

    Connecting to Data Sources

    Create a connection to thedatabase:

    Click Server Explorer onthe View menu

    Click the Connect toDatabase button in theServer Explorer

    Add a new connection toyour database

    Lesson 11: Developing Database Applications 21

    Connecting to Data Sources

    Add tables in the dataset:

    Drag tables from theServer Explorer and dropit in the dataset designer

    You should see the ERDof the tables in thedataset designer

  • 8/4/2019 Notes - Lesson 11a

    8/16

    Lesson 11: Developing Database Applications 22

    Connecting to Data Sources

    Creating Data- bound controls

    Data bound controls are controls which can easily bindwith data components

    Some examples of controls that can bind data areDataGrids, ListBoxesand ComboBoxes

    Lesson 11: Developing Database Applications 23

    Connecting to Data Sources

    Create a connection:

    Create a new WindowsApplication Project

    Connect it to a database

    Expand the Tables nodeand select tables in theChoose Your DatabaseObjects dialog box andpress finish

    You should now see thetable nodes in the DataSources Window that you

    just selected

    Lesson 11: Developing Database Applications 24

    Connecting to Data Sources

    Create a Data- boundcontrol:

    Drag a ComboBox, aListBox, or a

    DataGridView from theToolbox Tab and drop itin the form. This will beyour data- bound control

    In the Data SourcesWindow, drag the tablenode you want to viewand drop it in the control

  • 8/4/2019 Notes - Lesson 11a

    9/16

    Lesson 11: Developing Database Applications 25

    Connecting to Data Sources

    Adding a navigator:

    For you to be able to

    navigate through theresults, drag aBindingNavigatorfromData in the Toolbox Taband drop it in the form

    Change theBindingSourceproperty inthe Properties Window sothat your data binds toyour control

    Lesson 11: Developing Database Applications 26

    Connecting to Data Sources

    Run the application:

    Press F5 to run theapplication. You shouldbe able to see the data inthe table you selected inyour data- bound control.Also, you should be ableto navigate through theresults by pressing theMove Next button

    Lesson 11: Developing Database Applications 27

    Connecting to Data Sources

    Creating a TableAdapter with multiple queries

    TableAdaptersare objects that are used to simplify theinteraction of data objects to a database

    It encapsulates the objects needed to communicate witha database and presents methods to access the data

  • 8/4/2019 Notes - Lesson 11a

    10/16

    Lesson 11: Developing Database Applications 28

    Connecting to Data Sources

    Use the Dataset Designer:

    Create a new project

    and connect it to adatabase

    Right-click the Datasetin the Data Sourceswindow

    Choose Edit DataSetwith Designer

    Lesson 11: Developing Database Applications 29

    Connecting to Data Sources

    Add your queries:

    Add a query to the TableAdapterby dragging a Queryfrom the DataSettab of the Toolbox onto the table youwant to query

    The TableAdapterQuery Configuration Wizard shouldappear

    Lesson 11: Developing Database Applications 30

    Connecting to Data Sources

    Choose what Command type you want to use

  • 8/4/2019 Notes - Lesson 11a

    11/16

    Lesson 11: Developing Database Applications 31

    Connecting to Data Sources

    Choose what type of query you want to use

    Lesson 11: Developing Database Applications 32

    Connecting to Data Sources

    You can add conditions in your query

    Lesson 11: Developing Database Applications 33

    Connecting to Data Sources

    You can change the name of your method

  • 8/4/2019 Notes - Lesson 11a

    12/16

    Lesson 11: Developing Database Applications 34

    Connecting to Data Sources

    Execute the query:

    Drag the table node you want from the Data Sources

    window to the formTo call the method you created, you should have a codethat looks like this:

    .(,[]);

    Lesson 11: Developing Database Applications 35

    Connecting to Data Sources

    Press F5 to run the application. The grid should be filledwith your query

    Lesson 11: Developing Database Applications 36

    Connecting to Data Sources

    Reading XML Data into a DataSet

    XML Data can be read into a DataSet

    The output can be a table consisting of all the attributes

    of the given XML data and its values

    In the following example, assume that the given XML filelooks like this:

  • 8/4/2019 Notes - Lesson 11a

    13/16

    Lesson 11: Developing Database Applications 37

    Connecting to Data Sources

    Dominador

    Bogart

    4332533

    10932 Bigge

    Rd.

    Quezon

    City

    94025

    The is theTable Name

    Attributes like and are theTable columns

    Lesson 11: Developing Database Applications 38

    Connecting to Data Sources

    Create the XML file that will be read into the dataset:

    Choose Add New Item from the Project Menu

    Select XML File and click Add

    The XML file should load into the designer and should beready for edit

    Type the previous code into the editor below the XMLdeclaration

    Lesson 11: Developing Database Applications 39

    Connecting to Data Sources

    Adding an XML file

  • 8/4/2019 Notes - Lesson 11a

    14/16

    Lesson 11: Developing Database Applications 40

    Connecting to Data Sources

    The following codes will read data from the XML file tothe Dataset

    myDataSet.ReadXml();dataGridView1.DataSource = myDataSet;dataGridView1.DataMember = "person";

    Note that the DataSourceis the dataset and theDataMember is the table name

    Lesson 11: Developing Database Applications 41

    Connecting to Data Sources

    Creating Lookup Tables

    A lookup table is used to display information from onetable based on the value of a foreign-key field in anothertableFor example, there are three tables, Publisher, Publishesand Product. The Publishes table consists of a pubIDwhich is a foreign key pointing to the Publisher and aprodID which is a foreign key pointing to the Product.Since the product name is in the Product table and thepublisher name is in the Publisher table, and you arepresenting data from the Publishes table, you need tocreate a lookup table which takes the pubID and prodIDso that you can output the product name and its

    corresponding publisher.

    Lesson 11: Developing Database Applications 42

    Connecting to Data Sources

    Publisher, Publishes, Product Relationship

  • 8/4/2019 Notes - Lesson 11a

    15/16

    Lesson 11: Developing Database Applications 43

    Connecting to Data Sources

    Create the Data Source:

    Connect to SampleDB

    databaseSelect the Publisher,Publishes, and Producttables, and then clickFinish

    The DataSet should beadded to your projectand the three tablesshould appear in theData Sources window

    Lesson 11: Developing Database Applications 44

    Connecting to Data Sources

    Create data-bound controlson the form:

    Expand the Productnode in the DataSources window

    Change the drop type ofthe related Publishestable to Details byselecting Details fromthe control list on theOrders node

    Lesson 11: Developing Database Applications 45

    Connecting to Data Sources

    Expand the relatedPublishes node andchange the prodID andpubID columns drop

    type to a combo box byselecting ComboBoxfrom the control list onthe prodID and pubIDnodes

  • 8/4/2019 Notes - Lesson 11a

    16/16

    Lesson 11: Developing Database Applications 46

    Connecting to Data Sources

    Drag the related Publishes node from the DataSources window onto Form1

    Lesson 11: Developing Database Applications 47

    Connecting to Data Sources

    Create the lookup table functionality:

    Drag the main Product node from the Data Sourceswindow directly onto the prodID combo box on Form1

    Drag the main Publisher node from the Data Sourcewindow directly onto the pubID combo box on Form 1

    Run the application:

    Press F5

    The output in the prodID combo box should display theProduct name and the output in the pubID combo boxshould display the Publisher name that corresponds tothe given Product