260 likes | 470 Views
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.
E N D
Database, SQL, and ADO .NET- Part 1Session 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. 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.
Introduction 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.
Relational Database Model 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. Record/Row Primary key Field/Column
Structured Query Language (SQL) Basic SELECT Query SELECT * FROM tableName (*) indicates that all columns from the tableName table of the database should be selected. Example: SELECT * FROM Authors
WHERE Clause 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 Example: To select the name, salary, and location fields from table Employee in which the salary is greater than 1800.
WHERE Clause SELECT name, salary, location FROM Employee WHERE salary > 1800
ORDER BY Clause 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
ORDER BY Clause Example: SELECT name, salary, location FROM Employee ORDER BY name ASC
Merging Data from Multiple Tables: INNER JOIN 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.
Merging Data from Multiple Tables: INNER JOIN SELECT fieldName1, fieldName2, … FROM table1 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.
INSERT Statement The INSERT statement inserts a new record in a table. INSERT INTO tableName ( fieldName1, fieldName2, …, fieldNameN ) VALUES ( value1, value2, …, valueN )
ADO .NET Object Model 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 Object Model 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 Object Model 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 Object Model 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.
ADO .NET Object Model 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.
ADO .NET Object Model 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.
The SqlConnection Object 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 SqlConnection Object 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");
Using a SqlConnection 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.
References http://www.csharp-station.com/Tutorials/ADODotNet/Lesson01.aspx