1 11/10/05CS360 Windows Programming ADO.NET. 2 11/10/05CS360 Windows Programming ADO.NET  Behind every great application is a database manager o Amazon.

Slides:



Advertisements
Similar presentations
Chapter 10 ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a programming interface to access data in a database.
Advertisements

Introduction to Database Processing with ADO.NET.
Coding ADO.NET Objects: Connection, Command, DataReader.
1 ADO.NET. 2.NET Framework Data Namespaces System.Data –Base set of classes and interfaces for ADO.NET System.Data.Common –Classes shared by the.NET Data.
ADO. NET. What is “ADO.Net”? ADO.Net is a new object model for dealing with databases in.Net. Although some of the concepts are similar to the classical.
Chapter 12 ADO.NET Yingcai Xiao. Introduction to Database.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions.
C# programming with database Basic guideline. First step Install SQL Server 2008/2010 (Professional edition if possible) Install Visual Studio 2008/2010.
SQL Azure Database Windows Azure SQL Database is a feature-rich, fully managed relational database service that offers a highly productive experience,
ADO.NET – part II August 2004 [ Marmagna Desai]. CONTENTS ADO vs ADO.NET ADO.NET – Managed providers Connecting to Database SqlConnection Selecting Database.
11 Updating a Database Table Textbook Chapter 14.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
Accessing SQL Server and OLE DB from.NET Svetlin Nakov Telerik Corporation
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the.
Neal Stublen Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating.
ADO.Net CS795. What is ADO.Net? Database language spoken by managed applications ADO.net database accesses go through modules: data providers –SQL Server.Net.
.NET Data Access and Manipulation ADO.NET. Overview What is ADO.NET? Disconnected vs. connected data access models ADO.NET Architecture ADO.NET Core Objects.
MySQL Connection using ADO.Net Connecting to MySQL from.NET Languages.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL ADO.Net Basics Ruwan Wijesinghe Trainer.
TOP10 DEV SKILLS TO MAKE YOUR DBA HAPPY Kevin Kline Director of Engineering Services, SQL Sentry SQL Server MVP since 2004 Twitter, FB, LI: KEKline Blog:
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
Module 2: Connecting to Data Sources. Overview Choosing a.NET Data Provider Defining a Connection Managing a Connection Handling Connection Exceptions.
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Module 7: Accessing Data by Using ADO.NET
Neal Stublen Tonight’s Agenda  Database Errors  Parameterized queries  ToolStrip control  Master-detail relationships  Custom.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Introduction to ADO.NET ADO.NET - Lesson 01  Training time: 10 minutes  Author:
Presented by Joseph J. Sarna Jr. JJS Systems, LLC
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
An Introduction to ADO.Net Marmagna Desai.NET Seminar, Fall-2003.
Module 3: Performing Connected Database Operations.
ADO.NET connections1 Connecting to SQL Server and Oracle.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
ADO.Net CS795. What is ADO.Net? Database language spoken by managed applications ADO.net database accesses go through modules: data providers –SQL Server.Net.
HNDIT Rapid Application Development
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Coding ADO.NET Objects: Connection, Command, DataReader.
Module 4 Introduction ADO.NET.
C# .NET Software Development
Active Data Objects Using.Net ADO.Net Farooq Ahmed Amna Umber Summayya Shehzad.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 The SqlCommand Object ADO.NET - Lesson 03  Training time: 15 minutes  Author:
Data Access. ADO.NET ADO.NET is the primary library for building database solutions within the.NET Framework. ADO.NET does not replace ADO. ADO and OLEDB.
ADO .NET from. ADO .NET from “ADO .Net” Evolution/History of ADO.NET MICROSOFT .NET “ADO .Net” Evolution/History of ADO.NET History: Most applications.
.NET Data Access and Manipulation
C# 1 CSC 298 ADO.NET. C# 2 ADO.NET  A data access technology that maps very well to the world of the web (disconnected architecture)  data is retrieved.
C# MySQL onnect-C-to-MySQL 1.
 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.
ASP.NET Programming with C# and SQL Server First Edition
Data Access with ADO.NET
Introduction to ADO.NET
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET and Stored Procedures
How to Create Login Form using vb.net and SqlServer Database
Lecture 6 VB.Net SQL Server.
Introduction to Database
SQL commands from C# and ASP.net
ADO.Net and Stored Procedures
Tonga Institute of Higher Education
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
MySQL Connection using ADO.Net
Chapter 10 ADO.
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Introduction to Database Programming through ADO. NET
Presentation transcript:

1 11/10/05CS360 Windows Programming ADO.NET

2 11/10/05CS360 Windows Programming ADO.NET  Behind every great application is a database manager o Amazon o eBay  Programming is about managing application data  UI code is just goo :)

3 11/10/05CS360 Windows Programming ADO.NET  Elegant, easy-to-use database API for managed applications  Many database APIs o ODBC o DAO o RDO o OLE DB

4 11/10/05CS360 Windows Programming Two Providers  ADO.NET database access goes through two data providers o SQL Server.NET provider interfaces with Microsoft SQL Server o OLE DB.NET provider interfaces to databases through unmanaged providers

5 11/10/05CS360 Windows Programming Providers  Decide on a provider before you write any code  Microsoft SQL Server 2000 is installed on Winter  I have provided you all with accounts and databases  You can connect using the SQL Server provider as described next

6 11/10/05CS360 Windows Programming Namespace  Need to use the SqlClient namespace  using System.Data.SqlClient;

7 11/10/05CS360 Windows Programming Connecting to a Database 1. Create a connection object encapsulating a connection string 2. Open the connection by calling Open on the connection object 3. Create a command object encapsulating both an SQL command and the connection the command will use 4. Call a method on the command object to execute the command 5. Close the connection by calling Close on the connection object

8 11/10/05CS360 Windows Programming 1. Create a connection  Create a connection object encapsulating a connection string SqlConnection conn = new SqlConnection (“server=winter.cs.pacificu.edu; database=cs360Test; uid=shereen; pwd=abc123”);

9 11/10/05CS360 Windows Programming 1. Create a connection  Other available parameters o Connect Timeout: specifies the maximum length of time in seconds that you’re willing to wait when opening a connection (default = 15) o Integrated Security: enables and disables integrated security  Default is false, use is authenticated based on user name on password not Windows access

10 11/10/05CS360 Windows Programming 2. Open the connection  Open the connection by calling Open on the connection object  conn.Open();  But what if a connection could not be established?  Place within a try and catch  Open throws a SqlException

11 11/10/05CS360 Windows Programming 3. Open the connection try { conn.Open(); } catch (sqlException ex) { // handle exception } finally { conn.Close(); }

12 11/10/05CS360 Windows Programming 4. Create a command object  Create a command object encapsulating both an SQL command and the connection the command will use  We now have an open connection to the database  We use command classes to execute commands on the database o Select o Insert

13 11/10/05CS360 Windows Programming 4. Create a command object SqlCommand cmd = new SqlCommand(); cmd.CommandText = “delete from titles where title_id = ‘BU1032’”; cmd.Connection = conn; cmd.CommandTimeout = 10; cmd.ExecuteNonQuery();

14 11/10/05CS360 Windows Programming ExecuteNonQuery Method  Used for executing o INSERT, UPDATE, DELETE  Return value is number of rows affected o CREATE DATABASE o CREATE TABLE o Other SQL commands that don’t return values  Return value is -1

15 11/10/05CS360 Windows Programming ExecuteScalar Method  Executes an SQL command and returns the first row of the first column in the result set  Executes functions that return single-row, single-column result sets o COUNT, AVG, MIN, MAX and SUM

16 11/10/05CS360 Windows Programming Example of ExecuteScalar SqlCommand cmd = new sqlCommand (“select max (advance) from titles”, conn); Decimal amount = (decimal)cmd.ExecuteScalar(); Console.WriteLine(“ExecuteScalar returned {0:c}”, amount);  An incorrect cast throws an InvalidCastException

17 11/10/05CS360 Windows Programming Example of ExecuteScalar  BLOB (Binary Large Objects) o Such as images stored as bitmaps MemoryStream stream = new MemoryStream(); byte[] blob = (byte[]) cmd.ExecuteScalar(); stream.Write(blob, 0, blob.Length); Bitmap bitmap = new Bitmap(stream); Bitmap.Dispose();

18 11/10/05CS360 Windows Programming Writing BLOBs to a Database FileStream stream = new FileStream (“Logo.jpg, FileMode.Open); byte[] blob = new byte[stream.Length]; Stream.Read(blob, 0, (int) stream.Length); Stream.Close(); SqlCommand cmd = new SqlCommand (“insert into pub_info (pub_id, logo) values conn); blob); cmd.ExecuteNonQuery();

19 11/10/05CS360 Windows Programming ExecuteReader Method  Performs database queries and obtains the results as quickly and efficiently as possible  Returns an SqlDataReader object  Inherits from DataReader  Contains methods and properties to iterate over the result set  Fast, forward-only, read-only mechanism

20 11/10/05CS360 Windows Programming ExecuteReader Example SqlCommand cmd = new SqlCommand (“select * from titles”, conn); SqlDataReader reader = cmd.ExecuteReader(); while(reader.Read()) Console.WriteLine(reader[“title”]);  Each call to Read returns one result  Fields can be referenced by name or index

21 11/10/05CS360 Windows Programming ExecuteReader  Do you need to know a database’s schema in order to query it?  No, example follows

22 11/10/05CS360 Windows Programming GetName Method SqlCommand cmd = new SqlCommand (“select * from titles”, cmd); SqlDataReader reader = cmd.ExecuteReader(); for(int i=0; i<reader.FieldCount; i++) Console.WriteLine(reader.GetName(i));  GetName used to retrieve field names

23 11/10/05CS360 Windows Programming GetValue Method  GetValue is used to retrieve field values  Returns a generic object  Other Get methods o GetInt32 and GetDecimal

24 11/10/05CS360 Windows Programming GetValue Example SqlCommand cmd = new SqlCommand (“select * from titles where advance != 0, conn); SqlDataReader reader = cmd.ExecuteReader(); int index = reader.GetOrdinal(“advance”); while(reader.Read()) Console.WriteLine(“{0:c}, reader.GetDecimal(index));

25 11/10/05CS360 Windows Programming Closing Readers  Readers need to be closed  reader.Close();

26 11/10/05CS360 Windows Programming Transacted Commands  Suppose an application will transfer funds from one bank account to another  The updates should be grouped together  Why?

27 11/10/05CS360 Windows Programming Transactions Example SqlCommand cmd = new SqlCommand (“update accounts set balance = balance ” + “where account_id = ‘1111’”, conn); cmd.ExecuteNonQuery(); cmd.CommandText = “update accounts set balance = balance ” + “where account_id = ‘2222’”; cmd.ExecuteNonQuery();  What’s the problem?

28 11/10/05CS360 Windows Programming Better Solution SqlTransaction trans = null; try { trans = conn.BeginTransaction (IsolationLevel.Serializable); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.Transaction = trans; cmd.CommandText = “update accounts set balance = balance where account_id = ‘1111’”; cmd.ExecuteNonQuery();

29 11/10/05CS360 Windows Programming Continued cmd.CommandText = “update accounts set balance = balance where account_id = ‘2222’”; cmd.ExecuteNonQuery(); trans.Commit(); } catch(SqlException) { if(trans!=null) trans.Rollback(); } finally { conn.Close() }

30 11/10/05CS360 Windows Programming Parameterized Commands  Used for executing the same command on a database with different values  Especially useful for user input!

31 11/10/05CS360 Windows Programming Example SqlCommand cmd = new SqlCommand(“update accounts set balance = balance where account_id conn); SqlDbType.Money); SqlDbType.Char); = -1000; = “1111”; cmd.ExecuteNonQuery(); = 1000; = “2222”; cmd.ExecuteNonQuery();