Database, SQL, and ADO .NET- Part 1 Session 11. Mata kuliah : M0874 – Programming II Tahun : 2010. Outline Materi. Introduction Relational Database Model Structured Query Language ADO .NET Object Model. Introduction. A database is an integrated collection of data.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Mata kuliah : M0874 – Programming II
Tahun : 2010
Relational Database Model
Structured Query Language
ADO .NET Object Model
A database is an integrated collection of data.
A database management system (DBMS) provides mechanisms for storing and organizing data in a manner that is consistent with the database’s format.
Database management systems enable programmers to access and store data without worrying about the internal representation of databases.
Today’s most popular database systems are relational databases.
Almost universally, relational databases use a language called Structured Query Language (SQL) to perform queries and to manipulate data.
A programming language connects to, and interacts with, a relational database via an interface-software that facilitates communication between a database management system and a program.
C# programmers communicate with databases and manipulate their data through Microsoft ActiveX Data ObjectsTM (ADO), ADO .NET.
The relational database model is a logical representation of data that allows relationships among data to be considered without concern for the physical structure of data.
Basic SELECT Query
SELECT * FROM tableName
(*) indicates that all columns from the tableName table of the database should be selected.
SELECT * FROM Authors
In most cases, users search a database for records that satisfy certain selection criteria.
Only records that match the selection criteria are selected.
SELECT fieldName1, fieldName2, … FROM tableName WHERE criteria
To select the name, salary, and location fields from table Employee in which the salary is greater than 1800.
SELECT name, salary, location
WHERE salary > 1800
The result of a query can be arranged in ascending or descending order using the optional ORDER BY clause.
SELECT fieldName1, fieldName2,…FROM tableName ORDER BY field ASC
SELECT fieldName1, fieldName2,…FROM tableName ORDER BY field DESC
ASC specifies ascending order (lowest to highest)
DESC specifies descending order (highest to lowest)
Field specifies the field whose values determine the sorting order
SELECT name, salary, location
ORDER BY name ASC
Database designers often split related data into separate tables to ensure that a database does not store data redundantly.
Often, it is necessary for analysis purposes to merge data from multiple tables into a single set of data.
Referred to as joining the tables, this is accomplished via an INNER JOIN operation in the SELECT query.
An INNER JOIN merges records from two or more tables by testing for matching values in a field that is common to the tables.
SELECT fieldName1, fieldName2, …
INNER JOIN table2
ON table1.fieldName = table2.fieldName
The ON part of the INNER JOIN clauses specifies the fields from each table that are compared to determine which records are joined.
The INSERT statement inserts a new record in a table.
INSERT INTO tableName ( fieldName1, fieldName2, …, fieldNameN )
VALUES ( value1, value2, …, valueN )
ADO.NET is an object-oriented set of libraries that allows you to interact with data sources.
Commonly, the data source is a database, but it could also be a text file, an Excel spreadsheet, or an XML file.
The ADO .NET object model provides an API for accessing database systems programmatically.
ADO .NET was created for the .NET Framework and is the next generation of ActiveX Data ObjectsTM (ADO).
Namespace System.Data is the root namespace for the ADO .NET API.
ADO.NET allows us to interact with different types of data sources and different types of databases.
Different data sources expose different protocols, we need a way to communicate with the right data source using the right protocol.
ADO.NET Data Providers are class libraries that allow a common way to interact with specific data sources or protocols. The library APIs have prefixes that indicate which provider they support.
ADO.NET includes many objects you can use to work with data.
The SqlConnection Object
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.
The SqlCommand Object
The process of interacting with a database means that you must specify the actions you want to occur. This is done with a command object. You use a command object to send SQL statements to the database.
The SqlDataReader Object
Many data operations require that you only get a stream of data for reading. The data reader object allows you to obtain the results of a SELECT statement from a command object. For performance reasons, the data returned from a data reader is a fast forward-only stream of data. This means that you can only pull the data from the stream in a sequential manner. This is good for speed, but if you need to manipulate data, then a DataSet is a better object to work with.
The DataSet Object
DataSet objects are in-memory representations of data. They contain multiple Datatable objects, which contain columns and rows, just like normal database tables.
The SqlDataAdapter Object
Sometimes the data you work with is primarily read-only and you rarely need to make changes to the underlying data source. Some situations also call for caching data in memory to minimize the number of database calls for data that does not change. The data adapter makes it easy for you to accomplish these things by helping to manage data in a disconnected mode. The data adapter fills a DataSet object when reading the data and writes in a single batch when persisting changes back to the database.
A SqlConnection is an object, just like any other C# object.
SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
Common parts of a connection string
The following shows a connection string, using the User ID and Password parameters:
SqlConnection conn = new SqlConnection("Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Pass=YourPass");
The purpose of creating a SqlConnection object is so you can enable other ADO.NET code to work with a database.
Other ADO.NET objects, such as a SqlCommand and a SqlDataAdapter take a connection object as a parameter.
The sequence of operations occurring in the lifetime of a SqlConnection are as follows:
Instantiate the SqlConnection.
Open the connection.
Pass the connection to other ADO.NET objects.
Perform database operations with the other ADO.NET objects.
Close the connection.