Professional Documents
Culture Documents
Net
Malek Kemmou CEO Arrabeta kemmou@kemmou.com
bioAdapter.Fill(bio_DataSet); repeater1.DataBind();
CEO Arrabeta (consulting firm based in Casablanca Morocco) Newtelligence Alliance Partner Senior Consultant and Senior Trainer Solutions Architecture, integration, interoperability Microsoft Regional Director for Middle East and Africa Ineta MEA Speaker Bureau Speaker at many conferences and events (TechEd, NDC, MDC, DevDays, DevEssentials )
Agenda
Overview of Data Access in .Net Fetching Data Processing Data Data as XML Tips & Tricks
What is ADO.NET?
Managed Code (.NET) Data Access Methodology Complete Integration with the .NET Framework Improved support for the disconnected business model Improved integration with XML Explicit control of Data Access behaviors for .NET applications
Introducing ADO.NET
Part of the .NET framework, ADO.NET was built with the new world of XML, disconnected data, web and HTTP in mind Is a rewrite of ADO for the .NET framework
Not a replacement of ADO for COM developers ADO.NET is a natural evolution of ADO, built around n-tier development and architected with XML at its core
XSL/T, X-Path,
DataSet
Sync
XMLDataDocument
DataAdapter
DataReader
XmlReader
Command Connection
Fetching Data
Connected Model
Create a connection Open Connection Execute Commands Obtain Results Process Rows Close Connection Data Bind
VB.Net
Dim cnn as New SqlConnection((Data Source = MyServer; User
VB.Net
Dim tnx as SqlTransaction = cnn.BeginTransaction Do some Data Access and processing
Execute Commands
Various types of commands
Insert, Update, Delete, stored procedure,
cmd.Parameters.Add("@A_ID",accountID);
Decimal AccountBalance = (Decimal) cmd.ExecuteScalar();
Process Rows
Dynamic Processing
SqlCommand cmd = new SqlCommand("Select Desc, " +"Amt from Activity where AccountID = @A_ID", cnn); cmd.Parameters.Add("@A_ID",accountID);
SqlDataReader results = cmd.ExecuteReader(); While (results.Read()) { Console.Write("Description: " + results.GetString(0)); Console.WriteLine("Amount: " + results.GetDecimal(1)); }
Data Binding
(Web Forms)
public void Page_Load(Object sender, EventArgs e) { // Crer une SqlCommand et obtenir un DataReader SqlConnection cnn = new SqlConnection("server=localhost;uid=sa;"); cnn.Open(); SqlCommand cmd = new SqlCommand("Select * from customers", cnn); SqlDataReader results = cmd.ExecuteReader(); // Lier les rsulats ActivityList.DataSource = results; ActivityList.DataBind(); }
Demo
Connected DataAccess from a Web Page
Processing Data
Disconnected Model
Fill DataSet Navigate the DataSet Update Changes from DataSet Data Bind DataSet can be used as cache
DataSet
A DataSet is a local buffer of tables, or a collection of disconnected recordsets
Keeps track of the relationships between the tables it contains
No connection!
(Not directly anyway)
DataSets: Tables
A DataSet contains a collection of DataTables (the DataTableCollection)
A DataTable represents one table of in-memory data. It contains a collection of columns (the DataColumnCollection) that represents the table's schema
A DataTable also contains a collection of rows (the DataRowCollection), representing the data held by the table. It remembers the original state along with current state, tracking the kinds of changes that have occurred.
SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = selectCommand; DataSet categories = new DataSet("Categories"); adapter.Fill(categories);
Update Changes
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand delete = new SqlCommand("DeleteOrder",cnn); delete.CommandType=CommandType.StoredProcedure; delete.Parameters.Add("@OrderID",typeof(Int32)).SourceColumn="OrderID"; adapter.DeleteCommand = delete; SqlCommand insert = new SqlCommand("AddOrder",cnn); insert.CommandType=CommandType.StoredProcedure; insert.Parameters.Add("@OrderID",typeof(Int32)).SourceColumn="OrderID"; insert.Parameters.Add("@CustD",typeof(Int32)).SourceColumn="CustomerID"; insert.Parameters.Add("@Date",typeof(DateTime)).Value = DateTime.Now; adapter.InsertCommand = insert; SqlCommand update = new SqlCommand("UpdateOrder",cnn); update.CommandType=CommandType.StoredProcedure; update.Parameters.Add("@OrderID",typeof(Int32)).SourceColumn="OrderID"; update.Parameters.Add("@CustD",typeof(Int32)).SourceColumn="CustomerID"; adapter.UpdateCommand = update;
adapter.Update(ordersTable);
Winforms DataBinding
SqlCommand cmd = new SqlCommand("GetAccountInfo", cnn); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameters.Add("@A_ID",accountID); DataSet account = new DataSet; DataAdapter adapter = new DataAdapter(cmd); adapter.Fill(account);
Demo
A Simple WinForm Working with Data
Loading XML
DataSet ds = new DataSet(); ds.ReadXml("inventory.xml");
Inferred Schema
If no schema is defined before calling DataSet.ReadXml(), schema is inferred from data General Rules
An element becomes a table if :
It is repetitive within its parent or it contains more than one simple content Otherwise, it becomes a column
Demo
DataSets and XML
DataTable.GetErrors()
Returns array of DataRow objects that represent rows whose updates failed
DataRow.RowError
Returns a string with a general error message that applies to the entire row
DataRow.GetColumnsInError()
Returns array of DataColumn objects that contributed to error
DataRow.GetColumnError(x)
Returns string description of the error itself x is column ordinal, name, or DataColumn object
Item(x, version)
Allows you to examine the value of a specific version of column x (DataRowVersion.Current, .Default, .Original, or .Proposed)
BeginEdit(), EndEdit()
In conjunction with .Items(x), allows you to modify column values in the row
CancelEdit()
Abandons pending changes to an edited row
RowState
DataRowState.Added, .Deleted, .Detached, .Modified, or .Unchanged
Summary
ADO.Net has tailored objects
.NET Data Providers for connected access
Executing commands DataReader
Questions
I will post session content on my blog : http://www.malekkemmou.ma