C# .NET Software Development

Slides:



Advertisements
Similar presentations
17. Data Access ADO.Net Architecture New Features of ADO.NET
Advertisements

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.
Introduction to Database Processing with ADO.NET.
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.
.NET Mobile Application Development Data in Distributed Systems Accessing Data with.NET.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
ADO.NET – part II August 2004 [ Marmagna Desai]. CONTENTS ADO vs ADO.NET ADO.NET – Managed providers Connecting to Database SqlConnection Selecting Database.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
ODBC, OLE DB, and ADO Introduction Dr. Ron Eaglin.
Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.
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.
1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
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.
CIS 375—Web App Dev II ASP.NET 10 Database 2. 2 Introduction to Server-Side Data Server-side data access is unique in that Web pages are basically ___________.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
Databases and Data Access  Introduction to ADO.NET  ADO.NET objects  ADP.NET namespaces  Differences between ADO and ADO.NET.
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.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
Chapter 3 Introduction to ADO.NET 3.1 The ADO and ADO.NET ActiveX Data Object (ADO) is developed based on Object Linking and Embedding (OLE) and Component.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL ADO.Net Basics Ruwan Wijesinghe Trainer.
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.
ASP.NET Rina Zviel-Girshin Lecture 5
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
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
© 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.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
 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.
1 Data Classes- DataView DataGridView Control. Objectives 2  Data Class  DataAdapter  DataReader  DataSet  DataTable  DataView  DataGridView Control.
C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 – May 2007 Chair of Software Engineering Lecture 10: Database Lisa (Ling) Liu.
Why ADO.NET Not your father’s Data Access.
Mauricio Featherman, Ph.D. Washington St. University
ADO.NET AND STORED PROCEDURES - Swetha Kulkarni. RDBMS ADO.NET Provider  SqlClient  OracleClient  OleDb  ODBC  SqlServerCE System.Data.SqlClient.
Accessing Data with Microsoft Visual C# Applications.
Why ADO.NET Not your father’s Data Access.
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.
Module 3: Working with Local Data. Overview Using DataSets Using XML Using SQL Server CE.
1 11/15/05CS360 Windows Programming ADO.NET Continued.
HNDIT Rapid Application Development
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Module 4 Introduction ADO.NET.
Active Data Objects Using.Net ADO.Net Farooq Ahmed Amna Umber Summayya Shehzad.
Session 8: Data Management (with Stored Procedures)
Module 5 Data Classes DataView – DataGridView Control 1.
Module 2: Using ADO.NET to Access Data. Overview ADO.NET Architecture Creating an Application That Uses ADO.NET to Access Data Changing Database Records.
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
ASP.NET Programming with C# and SQL Server First Edition
Introduction to ADO.NET
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET and Stored Procedures
ADO.NET Framework.
PROG Advanced Web Apps 5/23/2018 Notes on ADO.NET (1) Wendi Jollymore, ACES.
Programming the Web Using ASP.Net
Lecture 6 VB.Net SQL Server.
Tonga Institute of Higher Education
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter 10 ADO.
PROG Advanced Web Apps 4/13/2019 Programming Data Pages Wendi Jollymore, ACES.
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Presentation transcript:

C# .NET Software Development ADO.NET C# .NET Software Development

ADO.NET Interfaces to and programmatically works with databases Provides an abstraction layer between the database and the user Simplifies and standardizes how data connections are handled Data manipulation is done in a disconnected environment. This is accomplished by connecting to the database for just long enough to retrieve the data and store it in a local cache. The data is stored in a memory cache on the client machine and manipulated there. A disconnected environment promotes scalability.

ADO.NET Objects ADO.NET Objects include: DataConnection DataAdapter Command DataReader DataSet DataTable DataColumn DataRow DataRelation

ADO.NET Client Database DataAdapter Command DataReader Connection Disconnected DataSet (In-Memory) Client DataAdapter Command DataReader Connection Database

Getting The Data To get the data into local cache: A connection is established Data is transferred from the database The connection is closed To create the connection using a data connection object: Connection objects implement the IDbConnection interface The connection objects themselves are specific to the database being connected to SqlConnection SqlCeConnection OracleConnection OdbcConnection OleDbConnection

Connection String When creating an SqlConnection object, pass it a connection string that gives the object information about the connection. Some of the possible entries in the connection string are: For the complete list of possible entries, see SqlConnection.ConnectionString Property in the MSDN documentation. Entry Explanation Database The name of the database to connect to Server The URL of the server to connect to User ID The User ID to use to log into the database Password The password of the user logging in Encrypt Set true to encrypt all data between the client and the database

Example Connection String The creation of an SqlConnection object looks like the following: the connection string contains various entries separated by semi-colons The order of the entries does not matter SqlConnection conn = new SqlConnection( "Database=northwind; server=localhost; user id=demo; password=demo");

Establishing a Connection To connect to the database, the code looks like the following: To close the connection invoke the Close method: Some ADO.NET constructs open and close the connection for you The rule is if you open it, you close it You should close the database in a finally block conn.Open(); conn.Close();

DataSet The DataSet class models a relational database in memory. It is the local cache Tables are represented by the DataTable class DataTables are made up of DataColumns DataRows Constraints can be placed upon columns in DataTables Relationships between tables can be created In the System.Data namespace

DataSet Pictorially:

DataSet In Code: DataSet ds = new DataSet("MyDataSet"); DataTable dt1 = new DataTable("Persons"); ds.Tables.Add(dt1); // Initialize Table dt1.Columns.Add(new DataColumn("Name", typeof(string))); dt1.Columns.Add(new DataColumn("Age", typeof(int))); dt1.Columns.Add(new DataColumn("Salary", typeof(decimal))); // put some rows in Table dt1.Rows.Add(new object[] { "Sheldon", 31, 21000m }); dt1.Rows.Add(new object[] { "Kurt", 28, 15000m }); dt1.Rows.Add(new object[] { "Mike", 23, 18000m }); dt1.Rows.Add(new object[] { "Dennis", 145, 1000000m });

Accessing Data in the DataSet The previous example creates a dataset and populates it with a table. The DataSet is created with the name "MyDataSet" that is passed to the constructor. A table is created named "Persons", once again the name is passed in the constructor. We can access an entry in the table as follows: ds.Tables["Persons"].Rows[0]["Name"] = "Edgar";

Column Constraints Constraints are restrictions that are applied to DataColumns AllowDBNull Unique AutoIncrement DataColumn dc = new DataColumn("ID", typeof(int)); dc.Unique = true; dc.AutoIncrement = true; dc.AutoIncrementSeed = 10; dc.AutoIncrementStep = 2;

Data Relation A DataRelation defines a relationship between tables The constructor of the DataRelation class takes name of the relation the parent column(s) the child column(s) The parent and child columns can correspond to primary key/foreign key relationship in a database DataRelation relation = new DataRelation( "CustomerOrderInfo", invoiceDS.Tables["Customers"].Columns["ID"], invoiceDS.Tables["Orders"].Columns["CustomerID"]); invoiceDS.Relations.Add(relation);

Data Relation The following example demonstrates how to exercise the relation on a DataRow from the previous example DataRow[] results = invoiceDS.Tables["Customers"].Rows[0].GetChildRows(relation);

Typed DataSets ADO.NET allows for strongly typed DataSets The Typed DataSet allows for compile-time type checking With a Typed DataSet the syntax to index into a table is shortened You gain intellisense and autocomplete // Untyped DataSet access ds.Tables["MyTable"].Columns["Name"] // Corresponding Typed DataSet ds.MyTable.NameColumn

DataAdapter A DataAdapter acts as a bridge between the DataSet and the database DataAdapters are database specific

DataAdapter Use the DataAdapter to fill a DataSet Note that the DataAdapter opens and closes the connection for you SqlConnection conn = new SqlConnection( "Database=northwind; server=localhost; user id=demo; password=demo"); DataSet ds = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", conn); adapter.Fill(ds);

DataAdapter If the DataSet is modified, you can automatically update the database as follows: adapter.Update(ds);

Command Objects A Command object is a .NET class that contains a database command Command objects are database provider-specific There are three types of commands that the SqlCommand object can represent: Stored procedures SQL Commands Table-direct commands Not supported by MS SQL Server The type of the command is controlled by the CommandType property of the SqlCommand class

CommandType.Text The command object represents an SQL command This is the default SqlCommand command = new SqlCommand( "SELECT * FROM Employees", connection); // this is the default, it is technically not needed command.CommandType = CommandType.Text; adapter.SelectCommand = command; adapter.Fill(dataSet);

CommandType.StoredProcedure The command object represents a stored procedure SqlCommand command = new SqlCommand( "Ten Most Expensive Products", connection); command.CommandType = CommandType.StoredProcedure; adapter.SelectCommand = command; adapter.Fill(dataSet);

Stored Procedures Stored procedures are precompiled collections of SQL commands and flow-control statements that reside with the database in the DBMS Stored procedures can contain conditional logic (a normal SQL query cannot) The use of stored procedures reduces bandwidth use Stored procedures are precompiled, thus they have optimal execution. Stored procedures abstract the database from the programmer Stored procedures allow the DBA to manipulate the data and return a result set to the to the requesting method The programmer need not know the intricacies of the database Abstraction is good

Stored Procedures To access a Stored Procedure in ADO.NET, you must use a Command object You need to do the following to call a stored procedure using MS SQL Server: Create an SqlCommand object Set the CommandType property to CommandType.StoredProcedure Set the CommandText property to the name of the stored procedure. Set up the parameters for the stored procedure (if there are any). Invoke the stored procedure.

Stored Procedure Example // create a dataset for the results DataSet dataSet = new DataSet(); // create a connection object SqlConnection connection = new SqlConnection( "Database=Northwind; Server=localhost; user id=demo; password=demo"); SqlCommand command = new SqlCommand("CustOrdersDetail", connection); command.CommandType = CommandType.StoredProcedure; // create a parameter object SqlParameter param = new SqlParameter("@OrderID", SqlDbType.Int); param.Direction = ParameterDirection.Input; param.Value = 10254; // add the parameter to the parameter list for the command object command.Parameters.Add(param); // invoke the stored procedure and get the results SqlDataAdapter adapter = new SqlDataAdapter("CustOrdersDetail", connection); adapter.SelectCommand = command; adapter.Fill(dataSet);

Command Objects A Command object can also execute commands that go directly to the database with the following methods Method Description ExecuteNonQuery Executes non-select commands on the database and returns the number of rows affected ExecuteReader Executes a query and returns the result in a DataReader object. ExecuteScalar Executes a command on the database and returns one value. If the command returns one value, it is returned. If the command returns a table view, the value of the first column, first row is returned. ExecuteXmlReader Executes a query and returns the results in an XmlReader object.

DataReader The DataReader represents a read-only, forward-only set of rows from a database. DataReader classes are provider-specific connection.Open(); SqlCommand command = new SqlCommand( "select LastName from employees", connection); SqlDataReader reader = command.ExecuteReader(); while(reader.Read()) { Console.WriteLine(reader["LastName"]); } connection.Close();

DataView A DataView represents a filtered view of the data in a table or tables contained in a DataSet One of the major functions of the DataView is to allow data binding within both Windows and Web Forms adapter.SelectCommand = new SqlCommand("SELECT * FROM Products", connection); adapter.Fill(dataSet); DataView view = new DataView(dataSet.Tables[0]); view.RowFilter = "UnitPrice > 20.00 and UnitPrice < 30.00"; 3

Data Binding DataBinding is the process of binding a data cache object, such as a DataSet or DataTable, to a visual control To bind assign the object to the control's DataSource property Some GUI controls are only designed to display a column of data and not an entire table. To limit what is displayed, set the controls DisplayMember property as follows: myDataGrid.DataSource = myDataTable; myListBox.DataSource = myDataTable; myListBox.DisplayMember = "Col Three";

Data Binding