Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adam Calderon – C# MVP Application Development Practice Lead InterKnowlogy.

Similar presentations


Presentation on theme: "Adam Calderon – C# MVP Application Development Practice Lead InterKnowlogy."— Presentation transcript:

1 Adam Calderon – C# MVP Application Development Practice Lead InterKnowlogy

2  ADO.NET New Features  Binding Model Changes

3  DbProviderFactories  SqlConnection  Web.config  SqlCommand  TransactionScope  SqlDataAdaptor  DataSet

4  CreateCommand  CreateCommandBuilder  CreateConnection  CreateDataAdaptor  CreateParameter

5 // Data Comes from Inputs on Form string provider = ProviderNameBox.Text; string connString = ConnectionStringBox.Text; string commandText = CommandTextBox.Text; // Get the provider DbProviderFactory fact = DbProviderFactories.GetFactory(provider); // Create the connection DbConnection conn = fact.CreateConnection(); conn.ConnectionString = connString; // Create the data adapter DbDataAdapter adapter = fact.CreateDataAdapter(); adapter.SelectCommand = conn.CreateCommand(); adapter.SelectCommand.CommandText = commandText; // Run the query DataTable table = new DataTable(); adapter.Fill(table); // Shows the results Results.DataSource = table; Results.DataBind();

6  Asynchronous Operation Support  Failover Partner  MultipleActiveResultSets (MARS)  Connection String Builders

7  ConnectionStrings element ConfigurationManager.ConnectionStrings[“MySql”].ConnectionString  Protecting Connection Strings  Use aspnet_regiis.exe to encrypt web.config

8  Async Methods  Requires Async attribute on connection string  Limited to non-query command  Reader or XmlReader  Begin/End  ExecuteNonQuery  ExecuteReader  ExecuteXmlReader  3 Patterns for Async Implementation

9 // Start a non-blocking execution IAsyncResult iar = cmd.BeginExecuteReader(); // Do work while command running …. // Block the execution until done SqlDataReader reader = cmd.EndExecuteReader(iar); // Process data here WorkWithData(reader);

10 // Start a non-blocking execution IAsyncResult iar = cmd.BeginExecuteReader(); do { // Do work while command running …. } while (!iar.IsComleted); // Sync up and process data SqlDataReader reader = cmd.EndExecuteReader(iar); // Process data here WorkWithData(reader);

11 // Start a non-blocking execution IAsyncResult iar = cmd.BeginExecuteReader( new AsyncCallback(WorkWithData),cmd); // … later in your code Public void WorkWithData(IAsyncResult ar) { // Retrieve the context of the call SqlCommand cmd = (SqlCommand) ar.AsyncState; // Complete the async operation SqlDataReader reader = cmd.EndExecuteReader(ar); }

12  Supports Dispose Pattern  Determines if local or distributed transaction are needed  Objects that support ITransaction can participate

13 using (TransactionScope ts = new System.Transactions.TransactionScope()) { bool success = true; using (SqlConnection conn = new SqlConnection(ConnString)) { SqlCommand cmd = new SqlCommand(UpdateCmd, conn); cmd.Connection.Open(); try { cmd.ExecuteNonQuery(); } catch (SqlException ex) { // Error handling code goes here lblMessage.Text = ex.Message; success = false; } // Must call to complete; otherwise abort if(success) ts.Complete(); }

14  New Properties  AcceptChangesDuringUpdate  FillLoadOption ▪ OverwriteChanges (Current and Orginal) ▪ PreserveChanges (Original Only) ▪ Upsert (Current Version of row)  ReturnProviderSpecificTypes  UpdateBatchSize

15  New Properties  RemotingFormat ▪ Binary or Xml  SchemaSerializationMode (IncludeShema/ExcludeSchema)  New Methods  CreateDataReader – Creates a reader off a Data Table

16 DataSet data = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter( "SELECT * FROM employees;SELECT * FROM customers", ConfigurationManager.ConnectionStrings["LocalNWind"].ConnectionString); adapter.Fill(data); // Access the whole data set record by record DataTableReader reader = data.CreateDataReader(); do { while (reader.Read()) Response.Write(String.Format("{0} ", reader[1])); Response.Write(" "); } while (reader.NextResult()); reader.Close();

17

18  New Data Source Properties  Binding Expressions  Data Source Controls

19  DataSourceID  AppendDataBoundItems

20  DataBinder.Eval  Can be used anywhere  Eval (shortcut for DataBinder.Eval)  Can be used in the context of a data bound control 

21  XPathBinder.Eval  Used with Xml data anywhere  Xpath (shortcut for XPathBinder.Eval)  Can be used in the context of a data bound control   XPathBinder.Select  Returns a nodeset that can be assigned to bound controls Data Source  DataSource=‘

22  Bind Method   Two-Way Binding  Dynamic Expressions  Evaluated when page compiles 

23  Tabular Data Source Controls  AccessDataSource  ObjectDataSource  SqlDataSource  Hierarchical Data Source Controls  SiteMapDataSource  XmlDataSource  DataSourceView

24  Properties for all of the actions (Delete,Insert,Update,Select)  Command  Parameters  CommandType  FilterExpression / FilterParameters (only works with DataSet set for the DataSourceMode)

25  DataSourceMode  Determines how data is returned (DataSet or DataReader)  CancelSelectOnNull  Canceled data retrieval if parameter is null

26  Command contain own set of parameters  Declarative Parameters  ControlParameter  CookieParameter  FormParameter  ProfileParameter  QueryStringParameter  SessionParameter

27  Caching Behavior  CacheDuration (seconds)  CacheExpirationPolicy (absolute,sliding)  CacheKeyDependency (expiring the key)  SqlCacheDependency (table dependent)  Created with each distinct SelectCommand, ConnectionString and SelectParameters

28

29  Async Features of ADO.NET can help in some situations  ConnectionString builders  New TransactionScope simplifies transactions  Binding Expressions simplify page development  New DataSource objects simplify binding to Sql Server and Objects

30  More info on InterKnowlogy:  Contact Information Phone: x274 Blog:  About Adam Calderon  Microsoft MVP – C#  Microsoft UI Server Frameworks Advisory Council  Developer / Author / Speaker / Teacher


Download ppt "Adam Calderon – C# MVP Application Development Practice Lead InterKnowlogy."

Similar presentations


Ads by Google