Introduction to SQL (III)

Similar documents
Chapter 5: Advanced SQL" Chapter 5: Advanced SQL"

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

CS425 Fall 2017 Boris Glavic Chapter 6: Advanced SQL

Chapter 4: Intermediate SQL

Chapter 4: Intermediate SQL

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Textbook: Chapter 4. Chapter 5: Intermediate SQL. CS425 Fall 2016 Boris Glavic. Chapter 5: Intermediate SQL. View Definition.

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

Chapter 4: Advanced SQL

Chapter 4: SQL Database System Concepts 4.1 Silberschatz, Korth and Sudarshan

How to program applications. CS 2550 / Spring 2006 Principles of Database Systems. SQL is not enough. Roadmap

Lecture 4 Advanced SQL

Chapter 4: Intermediate SQL

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Database System Concepts

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Database Application Development

Lecture 6 - More SQL

JDBC, Transactions. Niklas Fors JDBC 1 / 38

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

More Database Programming. CS157A Chris Pollett Nov. 2, 2005.

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

Chapter 13 Introduction to SQL Programming Techniques

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

CMPT 354 Database Systems I

Database in Applica.on Development. Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

IBM -

CSE 135. Three-Tier Architecture. Applications Utilizing Databases. Browser. App. Server. Database. Server

Unit 2: SQL AND PL/SQL

The SQL data-definition language (DDL) allows defining :

Chapter 3: Introduction to SQL

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

DATABASE MANAGEMENT SYSTEMS UNIT II- RELATIONAL MODEL

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

Chapter 3: Introduction to SQL

Lecture 2. Introduction to JDBC

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

SQL in a Server Environment (ii)

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

First lecture of this chapter is in slides (PPT file)

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

Principles of Data Management

JDBC Programming: Intro

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

QQ Group

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

Database Application Development

Embedded SQL. csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Meraji Winter 2018

JDBC Installation Transactions Metadata

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

General Overview - rel. model. Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Reminder: our Mini-U db

Database Application Development

EMBEDDED SQL. SE 3DB3 Fall 2016 MICHAEL LIUT DEPARTMENT OF COMPUTING AND SOFTWARE MCMASTER UNIVERSITY

Three-Tier Architecture

Embedded SQL. csc343, Introduction to Databases Diane Horton with examples from Ullman and Widom Fall 2014

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Databases 2012 Embedded SQL

Slides by: Ms. Shree Jaswal

Databases and JDBC. by Vlad Costel Ungureanu for Learn Stuff

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

UNIT III - JDBC Two Marks

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

SQL in a Server Environment

Database System Concepts"

Enterprise Systems. Lecture 02: JDBC. Behzad BORDBAR

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

数据库系统概论讲义, 第 8 章编程 SQL,2015,3

SQL Environment: Module Types. System Aspects of SQL. SQL Environment: Introduction. SQL Environment: Introduction. SQL Environment: Privileges

SQL: Programming Midterm in class next Thursday (October 5)

DB Programming. Database Systems

JDBC MOCK TEST JDBC MOCK TEST IV

Introduction to JDBC. JDBC: Java Database Connectivity. Why Access a Database with Java? Compilation. Six Steps. Packages to Import

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

BUSINESS INTELLIGENCE LABORATORY. Data Access: Relational Data Bases. Business Informatics Degree

Introduction to SQL & Database Application Development Using Java

Database Applications. SQL/PSM Embedded SQL JDBC

ADO.NET. Two Providers ADO.NET. Namespace. Providers. Behind every great application is a database manager

CSCB20. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

System Aspects of SQL

Database connectivity (II)

Lecture 27 10/30/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

Database Application Programs PL/SQL, Java and the Web

Java Database Connectivity

SQL in Programming Languages Read chapter 5 of Atzeni et al. BD: Modelli e Linguaggi di Interrogazione and section 8.

Unit I. By Prof.Sushila Aghav MIT

DATABASE DESIGN - 1DL400

3) execute() Usage: when you cannot determine whether SQL is an update or query return true if row is returned, use getresultset() to get the

CSE 565 Computer Security Fall 2018

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

Database Application Development Part 2 - Chapter

Announcements. SQL: Part IV. Transactions. Summary of SQL features covered so far. Fine prints. SQL transactions. Reading assignments for this week

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

Java Database Connectivity (JDBC) 25.1 What is JDBC?

DATABASE DESIGN - 1DL400

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

Non-interactive SQL. EECS Introduction to Database Management Systems

Transcription:

Introduction to SQL (III) 1

Roadmap of This Lecture Transactions Integrity Constraints SQL Data Types and Schemas Authorization Embedded SQL 2

Transactions Unit of work Atomic transaction either fully executed or rolled back as if it never occurred Isolation from concurrent transactions Transactions begin implicitly Ended by commit work or rollback work But default on most databases: each SQL statement commits automatically Can turn off auto commit for a session (e.g. using API) In SQL:1999, can use: begin atomic. end 3

Integrity Constraints Integrity constraints guard against accidental damage to the database, by ensuring that authorized changes to the database do not result in a loss of data consistency. A checking account must have a balance greater than $10,000.00 A salary of a bank employee must be at least $4.00 an hour A customer must have a (non-null) phone number 4

Integrity Constraints on a Single Relation not null primary key unique check (P), where P is a predicate 5

Not Null and Unique Constraints not null Declare name and budget to be not null name varchar(20) not null budget numeric(12,2) not null unique ( A 1, A 2,, A m ) The unique specification states that the attributes A1, A2, Am form a candidate key. Candidate keys are permitted to be null (in contrast to primary keys). 6

The check clause check (P) where P is a predicate Example: ensure that semester value is one of fall, winter, spring or summer: create table section ( course_id varchar (8), sec_id varchar (8), semester varchar (6), year numeric (4,0), building varchar (15), room_number varchar (7), time slot id varchar (4), primary key (course_id, sec_id, semester, year), check (semester in ( Fall, Winter, Spring, Summer )) ); 7

Referential Integrity Ensures that a value that appears in one relation for a given set of attributes also appears for a certain set of attributes in another relation. Example: If Biology is a department name appearing in one of the tuples in the instructor relation, then there exists a tuple in the department relation for Biology. Let A be a set of attributes. Let R and S be two relations that contain attributes A and where A is the primary key of S. A is said to be a foreign key of R if for any values of A appearing in R these values also appear in S. 8

Cascading Actions in Referential Integrity create table course ( course_id char(5), title varchar(20), dept_name varchar(20), primary key (course_id) foreign key (dept_name) references department) create table course ( dept_name varchar(20), foreign key (dept_name) references department on delete cascade on update cascade,... ) alternative actions to cascade: set null, set default 9

Integrity Constraint Violation During Transactions E.g. create table person ( ID char(10), name char(40), mother char(10), father char(10), primary key ID, foreign key father references person, foreign key mother references person) How to insert the first tuple without causing constraint violation? insert father and mother of a person before inserting person OR, set father and mother to null initially, update after inserting all persons (not possible if father and mother attributes declared to be not null) OR defer constraint checking (rarely supported) 10

Complex Check Clauses check (time_slot_id in (select time_slot_id from time_slot)) why not use a foreign key here? Every section has at least one instructor teaching the section. how to write this? Unfortunately: subquery in check clause not supported by pretty much any database Alternative: triggers (later) create assertion <assertion-name> check <predicate>; Also not supported by anyone 11

Index Creation create table student (ID varchar (5), name varchar (20) not null, dept_name varchar (20), tot_cred numeric (3,0) default 0, primary key (ID)) create index studentid_index on student(id) Indices are data structures used to speed up access to records with specified values for index attributes e.g. select * from student where ID = 12345 can be executed by using the index to find the required record, without looking at all records of student More on indices later. 12

Built-in Data Types in SQL date: Dates, containing a (4 digit) year, month and date Example: date 2005-7-27 time: Time of day, in hours, minutes and seconds. Example: time 09:00:30 time 09:00:30.75 timestamp: date plus time of day Example: timestamp 2005-7-27 09:00:30.75 interval: period of time Example: interval 1 day Subtracting a date/time/timestamp value from another gives an interval value Interval values can be added to date/time/timestamp values 13

User-Defined Types create type construct in SQL creates user-defined type create type Dollars as numeric (12,2) final create table department (dept_name varchar (20), building varchar (15), budget Dollars); No subtypes can be Defined from Dollar 14

Domains create domain construct in SQL-92 creates user-defined domain types create domain person_name char(20) not null Types and domains are similar. Domains can have constraints, such as not null, specified on them. Domains are not strongly typed. create domain degree_level varchar(10) constraint degree_level_test check (value in ( Bachelors, Masters, Doctorate )); 15

Large-Object Types Large objects (photos, videos, CAD files, etc.) are stored as a large object: blob: binary large object -- object is a large collection of uninterpreted binary data (whose interpretation is left to an application outside of the database system) clob: character large object -- object is a large collection of character data When a query returns a large object, a pointer is returned rather than the large object itself. 16

Authorization Forms of authorization on parts of the database: Read - allows reading, but not modification of data. Insert - allows insertion of new data, but not modification of existing data. Update - allows modification, but not deletion of data. Delete - allows deletion of data. Forms of authorization to modify the database schema Index - allows creation and deletion of indices. Resources - allows creation of new relations. Alteration - allows addition or deletion of attributes in a relation. Drop - allows deletion of relations. 17

Authorization Specification in SQL The grant statement is used to confer authorization grant <privilege list> on <relation name or view name> to <user list> <user list> is: a user-id public, which allows all valid users the privilege granted A role (more on this later) Granting a privilege on a view does not imply granting any privileges on the underlying relations. The grantor of the privilege must already hold the privilege on the specified item (or be the database administrator). 18

Privileges in SQL select: allows read access to relation, or the ability to query using the view Example: grant users U 1, U 2, and U 3 the select authorization on the instructor relation: grant select on instructor to U 1, U 2, U 3 insert: the ability to insert tuples update: the ability to update using the SQL update statement delete: the ability to delete tuples. all privileges: used as a short form for all the allowable privileges 19

Revoking Authorization in SQL The revoke statement is used to revoke authorization. revoke <privilege list> on <relation name or view name> from <revokee list> Example: revoke select on branch from U 1, U 2, U 3 <privilege list> may be all to revoke all privileges the revokee may hold. If <revokee list> includes public, all users lose the privilege except those granted it explicitly. If the same privilege was granted twice to the same user by different grantors, the user may retain the privilege after the revocation. All privileges that depend on the privilege being revoked are also revoked. Question: What if the grantor and the grantee have the same privilege on a relation, and the grantee wants to revoke the privilege of the grantor? 20

Roles create role instructor; grant instructor to Amit; Privileges can be granted to roles: grant select on takes to instructor; Roles can be granted to users, as well as to other roles create role teaching_assistant; grant teaching_assistant to instructor; instructor inherits all privileges of teaching_assistant Chain of Roles create role dean; grant instructor to dean; grant dean to Satoshi; 21

Authorization on Views create view geo_instructor as (select * from instructor where dept_name = Geology ); grant select on geo_instructor to geo_staff Suppose that a geo-staff member issues select * from geo_instructor; Clearly the geo-staff should be able to issue the query? Need to deal with the case where geo-staff does not have authorization to instructor 22

Authorizations on Schema references privilege to create foreign key grant reference (dept_name) on department to Mariano; why is this required? Because a foreign key guarantees the existence of the value in the other table -- can perform existence check on the other table! 23

Transfer of Privileges Transfer of privileges grant select on department to Amit with grant option; revoke select on department from Amit, Satoshi cascade; revoke select on department from Amit, Satoshi restrict; 24

Embedded SQL The SQL standard defines embeddings of SQL in a variety of programming languages such as C, Java, and Cobol. A language to which SQL queries are embedded is referred to as a host language, and the SQL structures permitted in the host language comprise embedded SQL. The basic form of these languages follows that of the System R embedding of SQL into PL/I. EXEC SQL statement is used to identify embedded SQL request to the preprocessor EXEC SQL <embedded SQL statement > END_EXEC Note: this varies by language (for example, the Java embedding uses # SQL {. }; ) 25

Example Query From within a host language, find the ID and name of students who have completed more than the number of credits stored in variable credit_amount. Specify the query in SQL and declare a cursor for it EXEC SQL declare c cursor for select ID, name from student where tot_cred > :credit_amount END_EXEC 26

Embedded SQL (Cont.) The open statement causes the query to be evaluated EXEC SQL open c END_EXEC The fetch statement causes the values of one tuple in the query result to be placed on host language variables. EXEC SQL fetch c into :si, :sn END_EXEC si holds the ID and sn holds the name Repeated calls to fetch get successive tuples in the query result A variable called SQLSTATE in the SQL communication area (SQLCA) gets set to 02000 to indicate no more data is available The close statement causes the database system to delete the temporary relation that holds the result of the query. EXEC SQL close c END_EXEC Note: above details vary with language. For example, the Java embedding defines Java iterators to step through result tuples. 27

Updates Through Cursors Can update tuples fetched by cursor by declaring that the cursor is for update declare c cursor for select * from instructor where dept_name = Music for update To update tuple at the current location of cursor c update instructor set salary = salary + 100 where current of c 28

JDBC and ODBC API (application-program interface) for a program to interact with a database server Application makes calls to Connect with the database server Send SQL commands to the database server Fetch tuples of result one-by-one into program variables SQL queries are created at runtime and hence dynamic SQL ODBC (Open Database Connectivity) works with C, C++, C#, and Visual Basic Other API s such as ADO.NET sit on top of ODBC JDBC (Java Database Connectivity) works with Java 29

JDBC JDBC is a Java API for communicating with database systems supporting SQL. JDBC supports a variety of features for querying and updating data, and for retrieving query results. JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes. Model for communicating with the database: Open a connection Create a statement object Execute queries using the Statement object to send queries and fetch results Exception mechanism to handle errors 30

JDBC Code public static void JDBCexample(String dbid, String userid, String passwd) { } try { } Class.forName ("oracle.jdbc.driver.oracledriver"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@db.yale.edu:2000:univdb", userid, passwd); Statement stmt = conn.createstatement(); Do Actual Work. stmt.close(); conn.close(); catch (SQLException sqle) { } System.out.println("SQLException : " + sqle); 31

JDBC Code (Cont.) Update to database try { stmt.executeupdate( "insert into instructor values( 77987, Kim, Physics, 98000)"); } catch (SQLException sqle) { System.out.println("Could not insert tuple. " + sqle); } Execute query and fetch and print results ResultSet rset = stmt.executequery( "select dept_name, avg (salary) from instructor group by dept_name"); while (rset.next()) { System.out.println(rset.getString("dept_name") + " " + rset.getfloat(2)); } 32

JDBC Code Details Getting result fields: rs.getstring( dept_name ) and rs.getstring(1) equivalent if dept_name is the first argument of select result. Dealing with Null values int a = rs.getint( a ); if (rs.wasnull()) Systems.out.println( Got null value ); 33

Prepared Statement PreparedStatement pstmt = conn.preparestatement( "insert into instructor values(?,?,?,?)"); pstmt.setstring(1, "88877"); pstmt.setstring(2, "Perry"); pstmt.setstring(3, "Finance"); pstmt.setint(4, 125000); pstmt.executeupdate(); pstmt.setstring(1, "88878"); pstmt.executeupdate(); WARNING: always use prepared statements when taking an input from the user and adding it to a query NEVER create a query by concatenating strings "insert into instructor values( " + ID + ", " + name + ", " + " + dept name + ", " balance + ") What if name is D Souza? 34

SQL Injection Suppose query is constructed using "select * from instructor where name = " + name + " " Suppose the user, instead of entering a name, enters: X or Y = Y then the resulting statement becomes: "select * from instructor where name = " + "X or Y = Y" + " " which is: select * from instructor where name = X or Y = Y User could have even used X ; update instructor set salary = salary + 10000; -- Prepared statement internally uses: "select * from instructor where name = X\ or \ Y\ = \ Y Always use prepared statements, with user inputs as parameters 35

Metadata Features ResultSet metadata E.g., after executing query to get a ResultSet rs: ResultSetMetaData rsmd = rs.getmetadata(); for(int i = 1; i <= rsmd.getcolumncount(); i++) { System.out.println(rsmd.getColumnName(i)); System.out.println(rsmd.getColumnTypeName(i)); } How is this useful? Print the scheme for this relation 36

Metadata (Cont) Database metadata DatabaseMetaData dbmd = conn.getmetadata(); ResultSet rs = dbmd.getcolumns(null, "univdb", "department", "%"); // Arguments to getcolumns: Catalog, Schema-pattern, Table-pattern, // and Column-Pattern // Returns: One row for each column; row has a number of attributes // such as COLUMN_NAME, TYPE_NAME while( rs.next()) { System.out.println(rs.getString("COLUMN_NAME"), rs.getstring("type_name")); } And where is this useful? Only those specified columns are retrieved 37

Transaction Control in JDBC By default, each SQL statement is treated as a separate transaction that is committed automatically bad idea for transactions with multiple updates Can turn off automatic commit on a connection conn.setautocommit(false); Transactions must then be committed or rolled back explicitly conn.commit(); or conn.rollback(); conn.setautocommit(true) turns on automatic commit. 38

Other JDBC Features Calling functions and procedures CallableStatement cstmt1 = conn.preparecall("{? = call some function(?)}"); CallableStatement cstmt2 = conn.preparecall("{call some procedure(?,?)}"); Handling large object types getblob() and getclob() that are similar to the getstring() method, but return objects of type Blob and Clob, respectively get data from these objects by getbytes() associate an open stream with Java Blob or Clob object to update large objects blob.setblob(int parameterindex, InputStream inputstream). 39

SQLJ JDBC is overly dynamic, errors cannot be caught by compiler SQLJ: embedded SQL in Java #sql iterator deptinfoiter ( String dept_name, int avgsal); deptinfoiter iter = null; #sql iter = { select dept_name, avg(salary) from instructor group by dept name }; while (iter.next()) { String deptname = iter.dept_name(); int avgsal = iter.avgsal(); System.out.println(deptName + " " + avgsal); } iter.close(); 40

ODBC Open DataBase Connectivity(ODBC) standard standard for application program to communicate with a database server. application program interface (API) to open a connection with a database, send queries and updates, get back results. Applications such as GUI, spreadsheets, etc. can use ODBC 41

ODBC (Cont.) Each database system supporting ODBC provides a "driver" library that must be linked with the client program. When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action, and fetch results. ODBC program first allocates an SQL environment, then a database connection handle. Opens database connection using SQLConnect(). Parameters for SQLConnect: connection handle, the server to which to connect the user identifier, password Must also specify types of arguments: SQL_NTS denotes previous argument is a null-terminated string. 42

ODBC Code int ODBCexample() { RETCODE error; HENV env; /* environment */ HDBC conn; /* database connection */ SQLAllocEnv(&env); SQLAllocConnect(env, &conn); SQLConnect(conn, db.yale.edu", SQL_NTS, "avi", SQL_NTS, "avipasswd", SQL_NTS); {. Do actual work } } SQLDisconnect(conn); SQLFreeConnect(conn); SQLFreeEnv(env); 43

ODBC Code (Cont.) Program sends SQL commands to the database by using SQLExecDirect Result tuples are fetched using SQLFetch() SQLBindCol() binds C language variables to attributes of the query result When a tuple is fetched, its attribute values are automatically stored in corresponding C variables. Arguments to SQLBindCol() ODBC stmt variable, attribute position in query result The type conversion from SQL to C. The address of the variable. For variable-length types like character arrays, The maximum length of the variable Location to store actual length when a tuple is fetched. Note: A negative value returned for the length field indicates null value Good programming requires checking results of every function call for errors; we have omitted most checks for brevity. 44

ODBC Code (Cont.) Main body of program char deptname[80]; float salary; int lenout1, lenout2; HSTMT stmt; char * sqlquery = "select dept_name, sum (salary) from instructor group by dept_name"; SQLAllocStmt(conn, &stmt); error = SQLExecDirect(stmt, sqlquery, SQL NTS); if (error == SQL SUCCESS) { SQLBindCol(stmt, 1, SQL C CHAR, deptname, 80, &lenout1); SQLBindCol(stmt, 2, SQL C FLOAT, &salary, 0, &lenout2); while (SQLFetch(stmt) == SQL SUCCESS) { printf (" %s %g\n", deptname, salary); } } SQLFreeStmt(stmt, SQL DROP); 45

ODBC Prepared Statements Prepared Statement SQL statement prepared: compiled at the database Can have placeholders: E.g. insert into account values(?,?,?) Repeatedly executed with actual values for the placeholders To prepare a statement SQLPrepare(stmt, <SQL String>); To bind parameters SQLBindParameter(stmt, <parameter#>, type information and value omitted for simplicity..) To execute the statement retcode = SQLExecute( stmt); To avoid SQL injection security risk, do not create SQL strings directly using user input; instead use prepared statements to bind user inputs 46

More ODBC Features Metadata features finding all the relations in the database and finding the names and types of columns of a query result or a relation in the database. By default, each SQL statement is treated as a separate transaction that is committed automatically. Can turn off automatic commit on a connection SQLSetConnectOption(conn, SQL_AUTOCOMMIT, 0)} Transactions must then be committed or rolled back explicitly by SQLTransact(conn, SQL_COMMIT) or SQLTransact(conn, SQL_ROLLBACK) 47

ODBC Conformance Levels Conformance levels specify subsets of the functionality defined by the standard. Core Level 1 requires support for metadata querying Level 2 requires ability to send and retrieve arrays of parameter values and more detailed catalog information. SQL Call Level Interface (CLI) standard similar to ODBC interface, but with some minor differences. 48

ADO.NET API designed for Visual Basic.NET and C#, providing database access facilities similar to JDBC/ODBC Partial example of ADO.NET code in C# using System, System.Data, System.Data.SqlClient; SqlConnection conn = new SqlConnection( Data Source=<IPaddr>, Initial Catalog=<Catalog> ); conn.open(); SqlCommand cmd = new SqlCommand( select * from students, conn); SqlDataReader rdr = cmd.executereader(); while(rdr.read()) { Console.WriteLine(rdr[0], rdr[1]); /* Prints first 2 attributes of result*/ } rdr.close(); conn.close(); Translated into ODBC calls Can also access non-relational data sources such as OLE-DB XML data Entity framework 49