Академический Документы
Профессиональный Документы
Культура Документы
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
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
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.
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
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
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.
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.
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();
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
DataTable
represents
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 Statement
22
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:
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:
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:
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:
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
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:
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:
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:
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 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.
200601137
200600459 200601020
Sabale
Que Lumandas
BCS
HRM JOU
Result:
200601137
200600459 200601020
Sabale
Que Lumandas
BCS
HRM JOU
Result:
DELETE Statement
36
Note: Be very careful when deleting records. You cannot undo this statement.
Lastname
Sabale Que Lumandas Luis
Course
BCS HRM JOU BCS
Result:
39
Create a DataAdapter.
SqlDataAdapter da;
da = new SqlDataAdapter("SELECT * FROM tableName", con);
2.
Create a DataTable.
DataTable dt = new DataTable();
3.
4.
40
2.
3.
4.
5.
6.
Reference