Vous êtes sur la page 1sur 18

Mapping XML to Databases

Objectives
In this lesson, you will learn to:
• Map XML to databases
• Implement various query languages
• Display data from multiple tables using VC#

©NIIT Creating Data Centric Applications Using ADO.NET Slide 1 of 18


Mapping XML to Databases

Understanding XML Data Transfer


• XML can be used for transferring data from client-side application to a
server side application and vice versa. Server can host a database or an
application. The data transfer can happen between applications or
application and database. XML data transfer can happen in the following
ways:
• Map XML to databases
• Query Languages

• Map XML to databases

• Mappings form the basis for data transfer between XML documents
and databases. Following are the two types of mappings:
• Table-Based Mapping
• Query Languages

©NIIT Creating Data Centric Applications Using ADO.NET Slide 2 of 18


Mapping XML to Databases

Understanding XML Data Transfer


(Contd.)
• Table-Based Mapping
• Table-based mapping is used for data transfer between an XML
document and a relational database. You can also transfer data
between relational databases by using the table-based mapping.

• Object-Relational Mapping

• In the object-relational mapping, the data in the XML document is


organized in a hierarchical manner. This type of mapping is used by
mostly XML-enabled relational databases such as SQL server.

©NIIT Creating Data Centric Applications Using ADO.NET Slide 3 of 18


Mapping XML to Databases

Understanding XML Data Transfer


(Contd.)
• Query Languages

• Query languages are embedded in XML document and are used for
data transfer between applications.

• Template-Based Query Languages Construction


• SQL-Based Query Languages
• XML Query Languages

©NIIT Creating Data Centric Applications Using ADO.NET Slide 4 of 18


Mapping XML to Databases

Displaying Data from Multiple Tables


using VC#
• Consider an example. An application needs to be created to display a
Windows Form. The form should display a combo box that comprises all
Customer IDs. When a user selects a customer ID, the corresponding
order IDs should be displayed in a list box.
• To create an application for the above scenario, perform the following
steps:
• Create a Windows application
• Add a combo box and list box to the form
• Connect to the database
• Create the data adapters and connections
• Generate the Dataset that will contain the related data tables

©NIIT Creating Data Centric Applications Using ADO.NET Slide 5 of 18


Mapping XML to Databases

Displaying Data from Multiple Tables


using VC#(Contd.)
• Create the Relationship.
• Fill the combo box with customer Ids.
• Fill the dataset with data from the Customers and the Orders table.
• Create an event handler to retrieve the order details for a selected
customer.
• Display the related records in the list box.
• Follow easy to understand naming conventions for the system and
processes.
• Design the system in such a manner that it meets the immediate
system requirements and can be modified later for enhanced
features.
• Test the modules.

©NIIT Creating Data Centric Applications Using ADO.NET Slide 6 of 18


Mapping XML to Databases

From the Expert’s Desk


• This section provides:

• Best practices on ADO.NET and Data Access Components


• Tips and Tricks
• FAQs on Data Binding and Data Reader.

©NIIT Creating Data Centric Applications Using ADO.NET Slide 7 of 18


Mapping XML to Databases

Best Practices
Applying Best Practices to a Project
• The best practices that you can follow in a project are:
• Use the Appropriate Data-Access Object
• Use SQL Data Types With SQL Server
• Use Centralized Data-Access Functions
• Use Centralized Data-Access Functions

©NIIT Creating Data Centric Applications Using ADO.NET Slide 8 of 18


Mapping XML to Databases

Tips and Tricks


Establishing Quick Database Connection in .NET

• While establishing connection to a database, you may find problems in


creating the database connection string. Following sites provide connection
strings that can be used in the applications
• http://www.connectionstrings.com
• http://www.able-consulting.com/ADO_Conn.htm

©NIIT Creating Data Centric Applications Using ADO.NET Slide 9 of 18


Mapping XML to Databases

FAQs
• When using the SqlDataReader, how do you know the number of records
that are found?

The SqlDataReader provides means of reading a "forward-only" stream of


rows from a SQL Server database.  Therefore, the number of rows read
from the database is not known until the last row is read.

This is the same type of problem we had back in classic ADO where the
Recordset's RecordCount was always -1 for a server-side, forward-only
cursor. The solution in classic ADO was to use a static or keyset cursor. 
Or use a client-side cursor location, which always used a static cursor. 
However in ADO.NET 1.0, there is no such thing as a server-side,
static or keyset cursor. There is only the server-side, forward-only
DataReader. Or there is the client-side, static DataSet.
•   

©NIIT Creating Data Centric Applications Using ADO.NET Slide 10 of 18


Mapping XML to Databases

FAQs(Contd.)
You can work-around this issue by either Increment a counter while looping
through all of the rows, or use two SELECT statements in your query. The
first one returns the row count and the second one returns the actual
rows. e.g. "SELECT COUNT(*) FROM myTableName; SELECT * FROM
myTableName". Then you can use the NextResult method to move from the first
to the second result set, or use a DataSet instead of the
SqlDataReader. Then use the DataSet‘s Table Rows count.  e.g. 
DataSet.Tables("Products").Rows.Count.

• How can I bind a data grid to a relationship through code?

To bind a data grid to a relationship through code, you set the DataSource
property to the dataset that contains the relationship, and you set the
DataMember property to the relationship. You must refer to the relationship
through its parent table .

©NIIT Creating Data Centric Applications Using ADO.NET Slide 11 of 18


Mapping XML to Databases

FAQs(Contd.)
For example, suppose you define a relationship named SalesInvoices that
relates rows in a parent table named Sales to rows in a child table named
Invoices. Then, if the relationship and tables are stored in a dataset
named DsPayables2, you can bind a data grid named grdInvoices to the
relationship using this code:
grdInvoices.DataSource = DsPayables2
grdInvoices.DataMember = "Sales.SalesInvoices"

• Why do I get a "Login failed" error message when I try to connect to a


SQL Server database from a web application?

This error typically occurs while using Windows NT Integrated security to


connect to the database. This error occurs because ASP.NET runs as a
separate Windows process with its own login name: ASPNET.

©NIIT Creating Data Centric Applications Using ADO.NET Slide 12 of 18


Mapping XML to Databases

FAQs(Contd.)
When you try to access a SQL Server database from a web application, then,
ASP.NET will try to use this login name to log in to SQL Server. For that to
work, you first have to create a Windows user named ASPNET, and you have
to create a SQL Server user account named ASPNET and grant it access to
the database. A simpler solution is not to use Windows NT Integrated
security. Instead, you can name the specific SQL Server account you want
to use when you create the connection. If you’re using MSDE on your own
system, for example, you can probably just use the default system
administrator account, sa.

©NIIT Creating Data Centric Applications Using ADO.NET Slide 13 of 18


Mapping XML to Databases

Challenge

©NIIT Creating Data Centric Applications Using ADO.NET Slide 14 of 18


Mapping XML to Databases

Challenge (Contd.)
• Across:
• 13. Used for connecting to a database, retrieving data, storing
the data dataset, reading the retrieved data, and updating the
database. (11)
• 134. Method of DataTable class to filter data(6)
• 142. Language used to define the elements and attributes of
XML documents(3)

• Down:
• 2. Object that creates a fixed customized view of given
DataTable object(8)
• 4. Provides the bridge between the DataSet object and the data
source. (11)
• 7. Type of binding, to display multiple values for a column from
the dataset rows(7)
• 9. ADO.NET uses the ___________ data architecture(12)

©NIIT Creating Data Centric Applications Using ADO.NET Slide 15 of 18


Mapping XML to Databases

Challenge (Contd.)
• 12. Component that represents the column that uniquely identifies a
row in a DataTable(10)
• 13. A recordset in ADO is similar to a _________ in ADO.NET (7)
• 22. Used to retrieve data from a data source in a read-only and
forward-only mode(10)
• 39. Method used to stop the connection with a data source(5)
• 41. Represents a table in the DataTableCollection of a dataset(9)
• 54. Dataset, which does not have any associated XML schema(7)
• 56. Control, which displays data from multiple records as well as
multiple columns. (8)
• 59. Data command to enter data into a database(6)
• 79. Fundamental format for data transfer in ADO.NET(3)

©NIIT Creating Data Centric Applications Using ADO.NET Slide 16 of 18


Mapping XML to Databases

Solution to Challenge

©NIIT Creating Data Centric Applications Using ADO.NET Slide 17 of 18


Mapping XML to Databases

Summary
• XML can be used for transferring data from client-side application to a
server side application and vice versa. Server can host a database or
an application.
• XML data transfer can happen in the following ways:
• Map XML to databases
• Query Languages
• Query languages are embedded in XML document and are used for
data transfer between applications.
• You can display data from multiple tables using VC#
• The best practices that you can follow in a project are:
• Use the Appropriate Data-Access Object
• Use SQL Data Types With SQL Server
• Use Centralized Data-Access Functions
• Use Centralized Data-Access Functions

©NIIT Creating Data Centric Applications Using ADO.NET Slide 18 of 18

Vous aimerez peut-être aussi