Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.

Similar presentations


Presentation on theme: "Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010."— Presentation transcript:

1

2 Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010

3 Bina Nusantara University 3 Outline Materi Introduction Relational Database Model Structured Query Language ADO.NET Object Model

4 Bina Nusantara University 4 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.

5 Bina Nusantara University 5 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 Objects TM (ADO), ADO.NET.

6 Bina Nusantara University 6 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. numbernamedepartmentsalaryLocation 23603Jones4131100New Jersey 24568Kerwin4132000New Jersey 34589Larson6421800Los Angeles 35761Myers6111400Orlando 78321Neuman n 4139000New Jersey Record/Row Primary keyField/Column

7 Bina Nusantara University 7 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

8 Bina Nusantara University 8 SQL Query Keywords SQL KeywordDescription SELECTSelects (retrieves) fields from one or more tables FROMSpecifies tables from which to get fields or delete records. Required in every SELECT and DELETE statement. WHERESpecifies criteria that determine the rows to be retrieved. INNER JOINJoins records from multiple tables to produce a single set of records. GROUP BYSpecifies criteria for grouping records. ORDER BYSpecifies criteria for ordering records. INSERTInserts data into a specified table. UPDATEUpdates data in a specified table. DELETEDeletes data from a specified table.

9 Bina Nusantara University 9 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.

10 Bina Nusantara University 10 WHERE Clause SELECT name, salary, location FROM Employee WHERE salary > 1800 numbernamedepartmentsalaryLocation 23603Jones4131100New Jersey 24568Kerwin4132000New Jersey 34589Larson6421800Los Angeles 35761Myers6111400Orlando 78321Neuman n 4139000New Jersey

11 11 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 Bina Nusantara University

12 12 ORDER BY Clause Example: SELECT name, salary, location FROM Employee ORDER BY name ASC

13 Bina Nusantara University 13 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.

14 Bina Nusantara University 14 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.

15 Bina Nusantara University 15 INSERT Statement The INSERT statement inserts a new record in a table. INSERT INTO tableName ( fieldName1, fieldName2, …, fieldNameN ) VALUES ( value1, value2, …, valueN )

16 Bina Nusantara University 16 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 Objects TM (ADO). Namespace System.Data is the root namespace for the ADO.NET API.

17 Bina Nusantara University 17 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.

18 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. Provider Name API prefix Data Source Description ODBC Data ProviderOdbc Data Sources with an ODBC interface. Normally older data bases. OleDb Data ProviderOleDb Data Sources that expose an OleDb interface, i.e. Access or Excel. Oracle Data ProviderOracleFor Oracle Databases. SQL Data ProviderSqlFor interacting with Microsoft SQL Server. Borland Data ProviderBdp Generic access to many databases such as Interbase, SQL Server, IBM DB2, and Oracle.

19 Bina Nusantara University 19 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.

20 20 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.

21 Bina Nusantara University 21 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.

22 Bina Nusantara University 22 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 Connection String Parameter Name Description Data Source Identifies the server. Could be local machine, machine domain name, or IP Address. Initial CatalogDatabase name. Integrated SecuritySet to SSPI to make connection with user's Windows login User IDName of user configured in SQL Server. PasswordPassword matching SQL Server User ID.

23 Bina Nusantara University 23 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");

24 Bina Nusantara University 24 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: 1.Instantiate the SqlConnection. 2.Open the connection. 3.Pass the connection to other ADO.NET objects. 4.Perform database operations with the other ADO.NET objects. 5.Close the connection.

25 Bina Nusantara University 25

26 Bina Nusantara University 26 References http://www.csharp- station.com/Tutorials/ADODotNet/Lesson01.aspxhttp://www.csharp- station.com/Tutorials/ADODotNet/Lesson01.aspx


Download ppt "Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010."

Similar presentations


Ads by Google