Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Module 3 Visual programming

Module 3 provides an introduction to relational databases and SQL programming, covering key concepts such as tables, primary and foreign keys, and relationships between tables. It also introduces ADO.NET for data access, explaining how to use data sources, custom statements, and data binding techniques. Additionally, it discusses advanced features of SQL data sources and how to format and manage data lists in a web application context.

Uploaded by

Midhun Manoj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Module 3 Visual programming

Module 3 provides an introduction to relational databases and SQL programming, covering key concepts such as tables, primary and foreign keys, and relationships between tables. It also introduces ADO.NET for data access, explaining how to use data sources, custom statements, and data binding techniques. Additionally, it discusses advanced features of SQL data sources and how to format and manage data lists in a web application context.

Uploaded by

Midhun Manoj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Module 3

An introduction to database programming - Introduction to relational database, how to use SQL to work
with data in database. Introduction to ADO.NET 4. How to use SQL data source, how to use custom
statements and stored procedures, Data list controls, Data binding, advanced features of a SQL data
source.

AN INTRODUCTION ΤΟ RELATIONAL DATABASES

• A relational database uses tables to store and manipulate data.

• Each table consists of one or more records, or rows or tuple, that contain the data for a single entry.
• Each row contains one or more fields, or columns or attributes , with each column representing a
single item of data.

• Most tables contain a primary key that uniquely identifies each row in the table.

• The primary key often consists of a single column, but it can also consist of two or more columns.
• If a primary key uses two or more columns, it’s called a composite primary key.
• In addition to primary keys, some database management systems let you define one or more non-
primary keys.
• In SQL Server, these keys are called unique keys, and they’re implemented using unique key
constraints. Like a primary key, a non-primary key uniquely identifies each row in the table.

• A table can also be defined with one or more indexes. An index provides an efficient way to
access data from a table based on the values in specific columns. An index is automatically
created for a table’s primary and non-primary keys.
RELATIONSHIP BETWEEN TABLES

• The tables in a relational database are related to each other through their key columns. For example,
the CategoryID column is used to relate the Categories and Products tables above. The CategoryID
column in the Products table is called a foreign key because it identifies a related row in the Categories
table.

• Usually, a foreign key corresponds to the primary key in the related table. In SQL Server, however, a
foreign key can also correspond to a unique key in the related table.

When two tables are related via a foreign key, the table with the foreign key is referred to as the foreign
key table and the table with the primary key is referred to as the primary key table.

• The relationships between the tables in a database correspond to the relationships between the
entities they represent.

The most common type of relationship is a one-to- many relationship as illustrated by the Categories
and Products tables. A table can also have a one-to-one relationship or a many-to-many relationship
with another table.
HOW THE COLUMNS IN A TABLE ARE DEFINED

• The data type that’s assigned to a column determines the type of information that can be stored in the
column. Depending on the data type, the column definition can also include its length, precision, and
scale.

• Each column definition also indicates whether or not the column can contain null values. A null value
indicates that the value of the column is not known.

• A column can be defined with a default value. Then, that value is used for the column if another value
isn’t provided when a row is added to the table.

• A column can also be defined as an identity column. An identity column is a numeric column whose
value is generated automatically when a row is added to the table.

• To restrict the values that a column can hold, you define check constraints. Check constraints can be
defined at either the column level or the table level.
QUERY A SINGLE TABLE

• The result of a Select statement is a result table, or result set, like the one shown
above. A result set is a logical set of rows that consists of all of the columns and
rows requested by the Select statement.
• A result set can include calculated columns that are calculated from other
columns in the table.
• To select all of the columns in a table, you can code an asterisk (*) in place of the
column names. For example, this statement will select all of the columns from the
Products table:
Select * From Products
JOIN OPERARTION

• A join lets you combine data from two or more tables into a single result set.
• The most common type of join is an inner join. This type of join returns rows
from both.
Tables only if their related columns match.
ADD,UPDATE,DELETE COMMANDS

• You use the Insert, Update, and Delete statements to maintain the data in a
database table.
• The Insert statement can be used to add one or more rows to a table. Although
the syntax shown above is for adding just one row, there is another syntax for
adding more than one row.
• The Update and Delete statements can be used for updating or deleting one or
more rows in a table using the syntax shown above.
INTRODUCTION TO ADO.NET. NET4
ADO.NET (ActiveX Data Objects for .NET) is a set of classes in the .NET Framework
designed for data access. It provides a way for developers to interact with data
sources, such as databases. ADO.NET allows you to connect to databases, execute
queries or stored procedures, and retrieve and manipulate data.

• ADO.NET uses two types of objects to access the data in a database: datasets,
which can contain one or more data tables, and .NET data provider objects, which
include data adapters, commands, and connections.
• A dataset stores data from the database so it can be accessed by the application.
The .NET data provider objects retrieve data from and update data in the
database.
• To retrieve data from a database and store it in a data table, a data adapter
object issues a Select statement that's stored in a command object. Next, the
command object uses a connection object to connect to the database and retrieve
the data. Then, the data is passed back to the data adapter, which stores the data
in a table within the dataset.
• To update the data in a database based on the data in a data table, the data
adapter object issues an Insert, Update, or Delete statement that's stored in a
command object. Then, the command object uses a connection to connect to the
database and update the data.
• The data provider remains connected to the database only long enough to
retrieve or update the specified data. Then, it disconnects from the database and
the application works with the data via the dataset object. This is referred to as a
disconnected data architecture.

How to work with data without using a data adapter


ADO.NET components for accessing a database directly.

• Instead of using a data adapter to execute commands to retrieve, insert, update,


and delete data from a database, you can execute those commands directly.
• To retrieve data from a database, you execute a command object that contains a
Select statement. Then, the command object uses a connection to connect to the
database and retrieve the data. You can then read the results one row at a time
using a data reader object
• Instead of using a data adapter to execute commands to retrieve, insert, update,
and delete data from a database, you can execute those commands directly.
• To retrieve data from a database, you execute a command object that contains a
Select statement. Then, the command object uses a connection to connect to the
database and retrieve the data. You can then read the results one row at a time
using a data reader object.

ADO.NET 4 CLASSES

• The SqlConnection class


• The SqlCommand class
• The SqlParameter class
• The SqlDataReader class
• The SqlDataAdapter class

1) A SqlConnection object is required to establish a connection to a SQL Server


database.
2) A SqlCommand object is used to execute a SQL command against a SQL
Server database.
3) A SqlParameter object is used to pass variable information to a SQL
command.

4) A sqlDataReader provides read-only, forward-only access to the data in a


database. Because it doesn’t require the overhead of a dataset, it’s more
efficient than using a data adapter. However, it can’t be used to update
data.
5) When the Fill method of a data adapter is used to retrieve data from a
database, the data adapter uses a data reader to load the results into a
dataset.

HOW TO USE SQL DATA SOURCE

What is Data Source

a data source refers to a location or provider from which a database or


application retrieves and stores data. It can be a database, a file, or even an
external system.

CREATING SQL DATASOURCE CONTOL

• To create a SqlDataSource control, drag the control from the Data group of
the Toolbox onto the form. Then, choose Configure Data Source from the
control’s smart tag menu and proceed from there.
HOW TO USE CUSTOM STATEMENTS AND STORED PROCEDURES

• To use custom statements with a SQL data source, select the SQL
Statement option and then enter the statement into the text box. You can
enter Select, Update, Insert, and Delete statements by selecting the
appropriate tab.

• To use stored procedures with a SQL data source, select the Stored
Procedure option and then select the stored procedure you want to use
from the drop-down list that's dis- played.
• When you use custom statements, Visual Studio doesn't generate Update,
Insert, and Delete statements from the Select statement you enter. Because
of that, you have to enter each of these statements yourself or use the
Query Builder to generate them.
DATALIST CONTROLS

the DataList control is a web server control used for displaying repeated
sets of data in a customizable layout. It allows you to define templates for
the display of each item in the list.
Here's a brief overview of the DataList control:
Templates
Data binding
Repeater Control

HOW TO DEFINE TEMPLATES FOR A DATA LIST

• The templates you define for a data list specify what content to display
and what controls to use to display it. At the least, you must create an Item
template that defines the items from the data source that you want to
display.
HOW TO FORMAT A DATA LIST

• You can format a data list by applying a predefined scheme, by using the
Properties dialog box, by using the Properties window, or by editing the
aspx code.

• To apply a scheme, choose Auto Format from the control's smart tag
menu and then select the scheme you want to apply.

• To use the Properties dialog box, choose Property Builder from the
control's smart tag menu and then set the properties for the data list and its
templates.

• To use the Properties window to format a template, expand the style


property for that template and then set its properties.

DATA BINDING

data binding is a technique that connects data sources (such as databases,


objects, or XML) to user interface elements like controls or widgets. This
allows automatic synchronization of data between the data source and the
UI, reducing the need for manual updates.

HOW TO USE DATA BINDING

• You can bind any of the controls that inherit the ListControl class to a data
source. That includes the list box control, the drop-down list control, the
check box list control, the radio button list control, and the bulleted list
control.

• You can use the Data Source Configuration Wizard to select the data
source for a list control, the data field to display in the list, and the data
value to return for the selected item.
• You can also use the DataTextFormatString attribute of a list control to
specify a format string you want to apply to the text that’s displayed in the
control.

ADVANCED FEATURES OF A SQL DATA SOURCE

1. HOW TO CREATE A DATA SOURCE THAT CAN UPDATE THE DATABASE

• To automatically generate Insert, Update, and Delete


statements for a data source, check the first box in the dialog
box that you get by clicking on the Advanced button in the first
dialog box To generate enhanced versions of the Update and
Delete state- ments that use optimistic concurrency, check the
second box too.
2. HOW TO CHANGE THE DATA SOURCE MODE
• The DataSourceMode attribute lets you specify that data
should be retrieved using a data reader rather than being
stored in a dataset. For read-only data, a data reader is usually
more efficient.

3. HOW TO USE CACHING

You might also like