introduction to ado.net malek kemmou ceo arrabeta
DESCRIPTION
Agenda Overview of Data Access in.Net Fetching Data Processing Data Data as XML Tips & TricksTRANSCRIPT
![Page 2: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/2.jpg)
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 …)
![Page 3: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/3.jpg)
Agenda
• Overview of Data Access in .Net• Fetching Data• Processing Data• Data as XML• Tips & Tricks
![Page 4: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/4.jpg)
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
![Page 5: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/5.jpg)
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
![Page 6: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/6.jpg)
Overview of Data in .Net
.NET Data Provider.NET Data Provider
DataReaderDataReader
CommandCommandConnectionConnection
SyncSync
Controls,Controls,Design Tools,Design Tools,
Code Generation …Code Generation …
DataSetDataSet
XmlReaderXmlReader
XSL/T, X-Path,XSL/T, X-Path, … …
XMLDataDocumentXMLDataDocument
DataAdapterDataAdapter
![Page 7: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/7.jpg)
Fetching Data
• Connected Model–Create a connection–Open Connection–Execute Commands–Obtain Results–Process Rows–Close Connection–Data Bind
![Page 8: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/8.jpg)
Create & open a Connection
• C#SqlConnection cnn = new SqlConnection(“Data Source = MyServer;
User Id=myUser; password=myPassword”);cnn.Open();
• VB.NetDim cnn as New SqlConnection((“Data Source = MyServer; User
Id=myUser; password=myPassword”)cnn.open
![Page 9: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/9.jpg)
Open a Transaction if needed
• C#SqlTransaction tnx = cnn.BeginTransaction();// do some Data Access and processingIf (somecondition) tnx.Commit();Else tnx.Rollback();• VB.NetDim tnx as SqlTransaction = cnn.BeginTransaction‘ Do some Data Access and processingIf (somecondition) Then
tnx.Commit()Else
tnx.Rollback()End If
![Page 10: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/10.jpg)
Execute Commands
• Various types of commands– Insert, Update, Delete, stored procedure, …
• Optionally transmit parameters
SqlCommand cmd = new SqlCommand("DeleteAccount", cnn);cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@A_ID",typeof(string));param.Value = accountID;cmd.Parameters.Add(param);
Int32 RowsAffected = cmd.ExecuteNonQuery();
![Page 11: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/11.jpg)
Obtain a Single Value
• Use ExecuteScalar
SqlCommand cmd = new SqlCommand( "Select Balance from Accounts where ” +“AccountID = @A_ID", &cnn);
cmd.Parameters.Add("@A_ID",accountID);
Decimal AccountBalance = (Decimal) cmd.ExecuteScalar();
![Page 12: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/12.jpg)
Process Rows
• Dynamic ProcessingSqlCommand 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));
}
![Page 13: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/13.jpg)
Data Binding(Web Forms)
public void Page_Load(Object sender, EventArgs e) {
// Créer une SqlCommand et obtenir un DataReaderSqlConnection cnn = new SqlConnection("server=localhost;uid=sa;");cnn.Open();SqlCommand cmd = new SqlCommand("Select * from customers", cnn);SqlDataReader results = cmd.ExecuteReader();
// Lier les résulatsActivityList.DataSource = results;ActivityList.DataBind(); }
![Page 14: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/14.jpg)
Demo
Connected DataAccess from a Web Page
![Page 15: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/15.jpg)
Processing Data
• Disconnected Model– Fill DataSet– Navigate the DataSet– Update Changes from DataSet– Data Bind– DataSet can be used as cache
![Page 16: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/16.jpg)
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• DataSets are an in-memory relational store • Exposes a rich programming model
– All data is stored in a local cache– Same performance and semantics regardless of whether
the data is loaded from a database, loaded from XML, or is generated by the application.
• No connection!– (Not directly anyway)
![Page 17: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/17.jpg)
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.
![Page 18: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/18.jpg)
Fill DataSet from Database
• Use a DataAdapter
SqlCommand selectCommand = new SqlCommand("Select CategoryName from Categories",cnn);
SqlDataAdapter adapter = new SqlDataAdapter();adapter.SelectCommand = selectCommand;
DataSet categories = new DataSet("Categories");adapter.Fill(categories);
![Page 19: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/19.jpg)
Navigate the DataSet
• Navigate the Row Collection of a Table• Obtain Rows as an Array• Use language expressions as “foreach”
foreach(DataRow customer in myDataSet.Tables["Customer"].Rows) {Console.WriteLine("Orders for customer: " + customer["Name"]);foreach(DataRow order in customer.GetChildRows("cust_orders") ) {
Console.Write("\t Order ID = " + order["OrderID"]);Console.WriteLine("Amount = " + order["Amount"]);
}}
![Page 20: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/20.jpg)
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);
![Page 21: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/21.jpg)
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);
DataGrid accountGrid = new DataGrid();accountGrid.SetDataBinding(myDataSet, "AccountList");
![Page 22: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/22.jpg)
Demo
A Simple WinForm Working with Data
![Page 23: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/23.jpg)
ADO .NET & XML
• DataSets and XML– Load/Save XML Data to/From DataSet– Schema can be loaded/saved as XSD– Schema can be inferred from XML Data
![Page 24: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/24.jpg)
Loading XML
DataSet ds = new DataSet();ds.ReadXml("inventory.xml");
DataTable inventory = ds.Tables["Inventory"];DataRow row = inventory.NewRow();row["TitleID"]=1;row["Quantity"]=25;inventory.Rows.Add(row);
ds.WriteXml("updatedinventory.xml");
![Page 25: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/25.jpg)
Load Schema from XSD
myDataSet.ReadXmlSchema(schemaFile);
– Complex Types converted to tables– Nested Complex Types converted to child
tables– Keys/Constraints converted into unique
constraints– Foreign Key Constraints inferred
![Page 26: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/26.jpg)
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
– Attributes become columns– Relations are created for nested tables
• Hidden columns are created for the keys
• Useful for dynamic data binding
![Page 27: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/27.jpg)
Demo
DataSets and XML
![Page 28: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/28.jpg)
X/Path And XSL/TOver DataSet
XmlDataDocument xmlData = new XmlDataDocument(po);
// Requête X/PathXmlNodeList nodes = xmlData.SelectNodes("//Item[@qty>100]");foreach(XmlNode node in nodes) { DataRow row = xmlData.GetRowFromElement((XmlElement)node); row.Delete();}
// Transformation XSLTXslTransform xsltransform = new XslTransform();xsltransform.Load("po.xsl");XmlReader xReader = xsltransform.Transform(xmlData, null);
![Page 29: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/29.jpg)
Tips & Tricks
• Auto generate Commands for updating DataSet
• Refreshing DataSet data
• Managing and processing errors when updating a DataSet
• Working with row versions and changes
• Passing null values
• Guarantee connection closes when DataReader is finished
• Inserting primary keys
![Page 30: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/30.jpg)
Auto Generate Commandsfor Updating DataSet
• Use CommandBuilder
SqlDataAdapter sda = new SqlDataAdapter(“select x, y, z from table1”,cnn);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);sda.UpdateCommand = scb.GetUpdateCommand();sda.InsertCommand = scb.GetInsertCommand();sda.DeleteCommand = scb.GetDeleteCommand();
sda.Update(ds);
![Page 31: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/31.jpg)
Refreshing DataSet Data
• Fill data with the Fill method of the AdaptermyAdapter1.fill(dataSet12);
• Process data.• Before Updating, use :
DataSet dataSetTemp = new DataSet();myAdapter1.fill(dataSetTemp);dataSet12.Merge(dataSetTemp, true);
![Page 32: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/32.jpg)
Managing and Processing ErrorsWhen updating DataSet
• DataAdapter.ContinueUpdateOnError– Default is False; setting to True allows all updates to complete even if
updates on certain rows generate errors• 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
![Page 33: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/33.jpg)
Working with Row versionsand changes
• Item(x)– Allows you to examine the value of column x, where x is column’s
ordinal or name• 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
![Page 34: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/34.jpg)
Passing null values
• Use DBNull.Value
SqlParameter param = new SqlParameter();param.Value = DBNull.value;
![Page 35: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/35.jpg)
Guarantee connection closeswhen DataReader finishes
• Use CommandBehavior.CloseConnection
private DataReader getCategories() {SqlCommand cmd = new SqlCommand( "Select * from Categories, cnn);DataReader results = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return results;}
![Page 36: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/36.jpg)
Inserting Primary Keys• Use Guids as Primary Keys
• Can be generated on the client• Guarantees to be unique• Doesn’t change when updated on the server
– No problem on child tables
![Page 37: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/37.jpg)
Summary• ADO.Net has tailored objects
– .NET Data Providers for connected access• Executing commands• DataReader
– DataSet for disconnected access, user interaction and caching• ADO.Net and XML are made for each other
![Page 38: Introduction to ADO.Net Malek Kemmou CEO Arrabeta](https://reader035.vdocuments.site/reader035/viewer/2022062219/5a4d1b8f7f8b9ab0599c09d5/html5/thumbnails/38.jpg)
Questions
• I will post session content on my blog :
http://www.malekkemmou.ma