Developing Windows and Web Applications using Visual StudioNET

Document Sample
Developing Windows and Web Applications using Visual StudioNET Powered By Docstoc
					Developing Windows and Web
Applications using Visual Studio.NET
  Presented by Adam Cogan
Admin Stuff


 • Attendance
   – You initial sheet
 • Hands On Lab
   – You get me to initial sheet
 • Homework
 • Certificate
   – At end of 10 sessions
   – If I say if you have completed successfully 
Session 1: Overview


 Agenda

 •   What is ADO.NET?
 •   What’s New in ADO.NET 2005?
 •   DataBinding
 •   The DataGrid
 •   Input Validation
The 10 Sessions

 http://www.ssw.com.au/ssw/events/2005UTSNET/default.aspx
Homework
What is ADO.NET?


 What is ADO.NET, anyway?
What is a DataSet?


 • Think of a DataSet object as a local in memory
   copy of database tables.
 • Why is it disconnected?
    –   Problems we had with the client server model
    –   client apps held onto connections
    –   Client apps and updated and added records at will
    –   Many Trips are bad

 • Changes can be reconciled against any
   datasource at any time.

 • A Dataset is not limited to database tables. It can
   work with XML or for that matter any other data.
SqlConnection cnn = new SqlConnection("Persist Security
  Info=False;Integrated
  Security=SSPI;database=northwind;server=tuna;Connect
  Timeout=5");
SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM
  Products", cnn);
DataSet ds = new DataSet();

cnn.Open();
adp.Fill(ds);
cnn.Close();

dataGridView1.DataSource = ds.Tables[0].DefaultView;
• Recordsets are now DataReaders and
  DataSets
• Data binding controls can bind to these
  objects
  – Listbox
  – ComboBox
  – DataGridView
• Not
  – TreeView
DataSet Structure

                 DataSet

                  1



                  *

                 DataTable
                                     1
             1    1     1

                                    *              *


                                   Constraint   DataRelation


                                    1             1


    *             *          *                                         Legend
                        *
                                                               .NET
   DataRow            DataColumn                               Class


                                                               Composition
• Untyped is
  – A generic DataSet that can store data
  – All columns are an object
  – Eg. Int I = Convert.ToInt32(datarow(“Rank”));
• Typed is
  – Any DataSet that is derived from the base
    DataSet class that applies the information
    contained in the XSD to generate a typed class
  – All columns are strongly typed
  – Eg. Int I = datarow.Rank;
Typed DataSet Structure

                                            TypedDataSet


                                        1
                                              DataSet


                                              1
                                                     1


                *
                                               *

               Customers                     DataTable
                                                                  1
         1               1                    1      1
                                        1
                                                                                *    *
                                                                  *                                  Legend
    *                         *
                                                                                            .NET
                                                                Constraint   DataRelation
 CustomerRow             CustomerIDColumn                                                   Class

                                                                 1             1
                                                                                                        *
                                                                                            Composition 1
                     *                         *     *     *

                                                                                            Generalization
                    DataRow                        DataColumn
• http://www.ssw.com.au/SSW/Standards/
  Rules/RulesToBetterdotNETProjects.aspx#
  DataAdapter
What are the controls


 • DataSet – Represents a in-memory
   cache of data
 • DataGridView – Displays rows and
   columns of data in a grid you can
   customize
 • BindingSource – Encapsulates a data
   source for a form and provides
   navigation, filtering, sorting, and
   updating capabilities
 • BindingNavigator – Provides a
   user interface for navigation and
   manipulation of data bound to
   controls on a form
 • ReportViewer – Displays reports
• Once Datagridview is on form, you get a
  smart tag for configuration settings




                ADO.NET is Cool
• Data source wizard allows you to bind to
  different sources
• Even objects
• Wizard allows you to choose the data to
  display
• Datagridview support photos straight from
  database with no customization
How do I call a SQL stored
procedure?


if(sqlConn.State == ConnectionState.Closed)
     sqlConn.Open();
     SqlCommand cmd = new SqlCommand("sp_my_stored_procedure",sqlConn);
     cmd.CommandTimeout = 180;
     cmd.CommandType = CommandType.StoredProcedure;

   //Add parameter #1
   SqlParameter parm;
   parm = cmd.Parameters.Add(new SqlParameter("@oid", SqlDbType.VarChar,50));
   parm.Direction = ParameterDirection.Input;
   cmd.Parameters["@oid"].Value = OrderID;

   //Add parameter #2
   parm = cmd.Parameters.Add(new SqlParameter("@custName", SqlDbType.VarChar,50));
   parm.Direction = ParameterDirection.Output;

   //Get the data
   SqlDataAdapter tempDA = new SqlDataAdapter();
   tempDA.SelectCommand = cmd;
   DataSet dataSet1 = new DataSet();
   tempDA.Fill(dataSet1);

   DataTable resultTable = dataSet1.Tables[0];
What's New in ADO.NET v2


 • ADO.NET API is Evolving
   – Backward compatible to ADO.NET 1.x
   – SqlClient has close ties to SQL Server 2005
 • Multiple Active Result Sets (MARS)
 • Bulk Copy Operations
 • SQL Server Notifications
 Others:
 • Transactions with SQL Server Snapshot Isolation
 • Database Mirroring in SQL Server 2005
 • Batch Processing
 • Schema Discovery
DataTable Grows Up


 • No longer dependent on DataSet for
    – ReadXML & WriteXML
    – Transfer via Web Services
    – Merge
       • Merge multiple DataTables
       • Note: CANNOT merge DataRow Collections
 • DataSets could not be serialised
    – Now DataTable becomes iXMLSerializable
 • DataTable/DataSet.Load Method
    – Convert from and to DataReader
 • Dataview.ToTable Method
 • Rowstate was readonly
    – Rowstate.SetAdded/SetModified
 • Provider Independent API
Stream Data into DataTable &
DataSet

• DataSet/DataTable.Load

• Get a DataTable/DataSet from a DataReader
  – Load dbDataReaders
     •   OLEleDbDataReader
     •   SqlDataReader
     •   DataTableReader
     •   Etc.


• Enables Merge functionality with DataReaders
  – Like a Union
  – LoadOption (also new to DataAdapter) to deal with
    concurrency issues
DataTableReader


 • Get a DataReader from a DataTable/DataSet
   – Inherits dbDataReader
      • (SqlDataReader, OleDBDataReader, etc…)
   – DataSet/DataTable.CreateDataReader Method
 • DataReader needs a database
   – But not this DataTableReader
   – Iteration without a database connection
   – Populating list controls and more
 • Navigate similar to SqlDataReader
 • Handles changes to underlying data source well
DataView.ToTable


 • Create a DataTable from a DataView!
   – Rather than building from iteration or a new
     database query
 • DistinctRows parameter is a blessing
RowState


 • DataRow.RowState is no longer read only
 • “Unchanged” rows can be modified
   – SetAdded
   – SetModified
Provider Independent API


 • Database Independent Coding
 • System.Data.Common Namespace
 • Provider Factory Class
    – Flip in database type at run time
  ‘example APINameSpace=“System.Data.SQLClient”
  Public Function GetDS(byVal APINameSpace as String)as DataSet
    Dim provider as DbProviderFactory =
      dbProviderFactories.GetFactory(APINameSpace)
    Dim connection As DbConnection-provider.CreateConnection
    Dim adapter as DbDataAdapter = provider.CreateDataAdapter
  ‘code to connect command, connection and dataadapter…
    adapter.fill(ds)
    Return ds
  End Function
Evolution: Performance


 • Batch Updates
 • True Binary Serialization for Remoting
Batch Updating


 • Works with DataAdapter.UpdateCommand
 • Sends groups of queries to the database
 • Fine Tune based on client and network
   resources
   –   Time to build multiple queries
   –   Network latency
   –   Processor
   –   Connection pooling
 • Better control on web servers than
   deployed smart client apps
Binary Serializaton in
Remoting


 • V1.x
    – DataSet is still xml
 • V2.0
    – DataSet is truly serialized as binary
    – Major performance benefit with large amounts
      of data
Batch Update:
Bigger is not always Better!



    Speed of 10,000 Record Update                Speed of 100 Record Update




                           News Flash!
                           February CTP
                           Batching Screams!



              Batch Size                                   Batch Size




 Not official benchmarks. Based one developers own tests
 Local=SQL2005     Remote Hard Wired=SQL2000       Web=SQL2000
Evolution:
SQL Server 2005 Integration


 • Flexibility
   – SQLDependency and SQLNotification
 • Usability
   – MARS (Multiple Active Resultsets)
      • Reuse Connections

 • Performance
   – Asynchronous SQL Command
   – Bulk Copy
SQL Server Query Notification


 •   Tied into SQL Server Service Broker (2005)
 •   System.Data.SQLNotification
 •   System.Data.SqlDependency
 •   ASP.NET has it’s own implementation
     – System.Web.Caching.SqlCacheDependency
     – Backwards compatible to SQL7 through polling
 • Many rules and caveats
     – This one is not documented (yet)
        • Be sure DataBaseCompatibilityLevel is set to Version90
 • Use wisely
MARS


 • Enable reuse of database connections
 • On by default
   – MultipleActiveResultSets=false in connection string to
     turn off
 • Benefits
   – Multiple SqlDataReaders on one connection
   – Allows multiple updates in one transaction
       • note: System.Transactions.TransactionScope
 • Caveats
   – Possible performance costs compared to connection
     pooling
Asynchronous Command
Object

 • Great for middle tier
 • Keep working while waiting for a command to
   complete processing

    Queries               BeginExecuteNonQuery
                          EndExecuteNonQuery

    SqlDataReaders        BeginExecuteReader
                          EndExecuteReader

    XmlDataReaders        BeginExecuteXmlReader
                          EndExecuteXmlReader
Bulk Copy


 • Load data into SQL Server via ADO.NET
 • Nearly as fast as DTS!
   – Sample: Loading 104,225 records into SQL2005

       3000
       2500                   2460
       2000
       1500
       1000

        500
          0        21                       23
                DTS     Batch Update   Bulk Copy
                            (100)
Conclusion


 • ADO.NET Team says:
    – We like ADO.NET and want to keep it!
 • API stays consistent in it’s design
 • ADO.NET 2 improves on
    – Usability
    – Flexibility
    – Performance
 • ADO.NET 2.0 leverages SQL Server 2005
Resources
 • What’s New in ADO.NET 2.0
    Julia Lerman, MSDN Magazine April 2005
 • Caching Improvements in ASP.NET Whidbey
     G. Andrew Duthie, MSDN Online February 2004
 • Improved Caching in ASP.NET 2.0
     Stephen Walther, MSDN Online, June 2004
 • Asynchronous Command Execution in ADO.NET 2.0
     Pablo Castro, MSDN Online, July 2004
 • ADO.NET and System.Xml v.2.0 – The Beta Version
     Alex Homer, Dave Sussman, Mark Fussell, Addison-
   Wesley, March 2005
 • New Features for Web Service Developers in Beta 1 of
   the .NET Framework 2.0
      Elliot Rapp,Yasser Shohoud and Matt Travis, MSDN
   Online, July 2004
Resources cont.


 • Using CLR Integration in SQL Server 2005
      Multiple Authors from Microsoft Corporation,
   MSDN Online, Nov 2004
 • ADO.NET 2.0 Multiple Active Result Sets per
   connection in Sql Server 2005 (MARS) FAQ
      Angel Saenz-Badillos, Weblog Post, Sept 7, 04
   (blogs.msdn.com/angelsb)
 • Transaction Processing in ADO.NET 2.0
      William Ryan, Sept 14, 2004, 15 Seconds website
   (www.15seconds.com)
 • Query Notifications in ADO.NET 2.0
     Bob Beauchemin, MSDN Online, September 2004
 • Whats new in ADO.NET 2.0
       http://msdn2.microsoft.com/library/ex6y04yf(en-
   us,vs.80).aspx
2 things

AdamCogan@ssw.com.au

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:71
posted:6/10/2012
language:
pages:40