SlideShare a Scribd company logo
1 of 28
Developing Database Applications Using ADO.NET and XML
Objectives


                In this session, you will learn to:
                   Retrieve and store large binary data
                   Perform bulk copy operations
                   Enable SQL notification to maintain and update a cache




     Ver. 1.0                         Session 8                         Slide 1 of 28
Developing Database Applications Using ADO.NET and XML
Handling Binary Large Objects in a Database


                While working with data, you may need to move large
                objects (LOB) between the client application and the
                database server.
                LOBs are of different formats:
                 Binary Large Object (BLOB): If a LOB is stored in a database
                  in a binary format, it is referred to as BLOB.
                 Character Large Object (CLOB): If a LOB is stored in the
                  database in a textual format, it is referred to as CLOB.




     Ver. 1.0                       Session 8                         Slide 2 of 28
Developing Database Applications Using ADO.NET and XML
Retrieving BLOB Data


                •   To access the DataReader object in a stream fashion, you
                    can change the DbCommand object’s behavior to a
                    sequential stream when you execute the
                    ExecuteReader() method.
                •   In the stream mode, you must get the bytes from the stream
                    in the order of each column that is being returned.
                •   Refer to following code snippet that retrieves photographs
                    and stores them into a file:
                    byte[] outbyte = new               Declare the BLOB byte[]
                    byte[bufferSize];                  buffer, which will be filled by
                                                       GetBytes. bufferSize
                    connection.Open();                 Open the connection and read
                                                       refers to the size of the BLOB
                    SqlDataReader myReader =           buffer. the DataReader.
                                                       data into
                    command.ExecuteReader
                    (CommandBehavior.
                    SequentialAccess);
                    while (myReader.Read()){
     Ver. 1.0                           Session 8                             Slide 3 of 28
Developing Database Applications Using ADO.NET and XML
Retrieving BLOB Data (Contd.)


                string fileName =              Create a file to store the image.
                @"C:Patient" + ".bmp";
                fs = new                       Write the BLOB to a .bmp file.
                FileStream(fileName,
                FileMode.OpenOrCreate,
                FileAccess.Write);
                bw = new BinaryWriter(fs);     Stream the BLOB to the
                                               FileStream object, fs.
                startIndex = 0;                Reset the starting position.
                                               bw is an object of
                retval =                       Read the bytes into outbyte[]
                                               BinaryWriter.
                myReader.GetBytes(1,           and store the number of bytes
                                               returned.
                startIndex, outbyte, 0,
                bufferSize);
                while (retval == bufferSize)   Continue to read and write while
                {                              bytes are remaining.
                   bw.Write(outbyte);
                   bw.Flush();


     Ver. 1.0                   Session 8                             Slide 4 of 28
Developing Database Applications Using ADO.NET and XML
Retrieving BLOB Data (Contd.)


                  startIndex +=             Reposition the start index to the
                                            end of the last buffer and fill the
                  bufferSize; retval =      buffer.
                  myReader.GetBytes(1,
                  startIndex, outbyte,
                  0, bufferSize);
                }
                bw.Write(outbyte, 0,        Write the remaining buffer.
                (int)retval - 1);
                bw.flush();




     Ver. 1.0                   Session 8                           Slide 5 of 28
Developing Database Applications Using ADO.NET and XML
Storing BLOB Data


                •   You can write BLOB data to a database by executing
                    INSERT or UPDATE statement.
                •   If the BLOB data is stored in a text format, you can pass the
                    data as a string parameter.
                •   The following code snippet updates a photograph with a
                    new one from the file:
                     command.CommandText = "Select Retrieve a pointer to the
                                                    photograph.
                     TEXTPTR(PatientPhoto)
                                                    The SQL Server TEXTPTR
                     from Patients where            function is first called to get a
                     PatientId='1004'";             pointer to the field of the record
                     photoPtr =                     to be updated.
                     (byte[])command.ExecuteScalar();




     Ver. 1.0                             Session 8                          Slide 6 of 28
Developing Database Applications Using ADO.NET and XML
Storing BLOB Data (Contd.)


                using (SqlCommand command =
                connection.CreateCommand())
                command.CommandText =
                "UPDATETEXT                  The SQL Server UPDATETEXT
                                             function writes the BLOB data in
                Patients.PatientPhoto        chunks of a specified size.
                @Pointer @Offset null       The pointer entire existing the photograph.
                                             Delete the to the start of data.
                                                current offset to insert data.
                @Data"; SqlParameter ptrParm The data being sent to the database.
                = command.Parameters.Add
                ("@Pointer",
                SqlDbType.Binary, 16);
                ptrParm.Value = photoPtr;
                SqlParameter photoParm =
                command.Parameters.Add("@Dat
                a", SqlDbType.Image);
                SqlParameter offsetParm =
                command.Parameters.Add("@Off
                set", SqlDbType.Int);

     Ver. 1.0                         Session 8                           Slide 7 of 28
Developing Database Applications Using ADO.NET and XML
Storing BLOB Data (Contd.)



                offsetParm.Value = 0;
                using (FileStream file = new     Read chunks of the file into
                FileStream("C:newPhoto.gif“,   the buffer and send the
                                                 chunks to the database.
                FileMode.Open,
                FileAccess.Read)){
                int count = file.Read (buffer,
                0, bufferSize);
                while (count != 0)
                { photoParm.Value = buffer;
                photoParm.Size = count;
                command.ExecuteNonQuery();
                currentIndex += count;
                offsetParm.Value =
                currentIndex;
                count = file.Read(buffer, 0,
                bufferSize);}}

     Ver. 1.0                Session 8                         Slide 8 of 28
Developing Database Applications Using ADO.NET and XML
Just a minute


                Fill in the blanks:
                    The SQL Server _______ function is first called to get a
                    pointer to the field of the record to be updated.




                Answer:
                    TEXTPTR



     Ver. 1.0                          Session 8                          Slide 9 of 28
Developing Database Applications Using ADO.NET and XML
Just a minute


                Fill in the blanks:
                    The SQL Server _______ function writes the BLOB data in
                    chunks of a specified size.




                Answer:
                    UPDATETEXT



     Ver. 1.0                         Session 8                       Slide 10 of 28
Developing Database Applications Using ADO.NET and XML
Demo: Handling BLOB Data From a Database


               Problem Statement:
                  A new employee has joined Tebisco. The details of this
                  employee need to be inserted to the Employees table. The
                  details are as follows:
                    Employee code: 000017
                    First name: Peter
                    Last name: Martin
                  In addition, the photograph of the employee should be
                  uploaded to the database. It should be displayed after the
                  photograph is inserted. Create an application that will perform
                  the required tasks.




    Ver. 1.0                         Session 8                            Slide 11 of 28
Developing Database Applications Using ADO.NET and XML
Performing Bulk Copy Operations


                •   There can be occasions when you need to copy large
                    amounts of data from one location to another.
                •   The SqlBulkCopy class allows you to write bulk in SQL
                    Server based tables.
                •   The data is copied from the data source to the destination
                    table, specified by the DestinationTableName property,
                    by using the WritetoServer() method of the
                    SqlBulkCopy class.




     Ver. 1.0                           Session 8                      Slide 12 of 28
Developing Database Applications Using ADO.NET and XML
Performing Bulk Copy Operations (Contd.)


                •   The following figure displays the various parameters that
                    can be passed to the WritetoServer() method in order
                    to copy data in bulk.
                                             WritetoServer()
                         Data                  DataRow Array            SQL
                        Source                 DataTable               Server
                                               IDataReader
                                               DataRowState




                    Copies all rows from the DataRow array to a destination table

                    Copies all rows in the DataTable to a destination table

                    Copies all rows in the IDataReader interface to a destination table

                    Copies only rows that match the row state in the DataTable to a
                    destination


     Ver. 1.0                                    Session 8                                Slide 13 of 28
Developing Database Applications Using ADO.NET and XML
Executing Single Bulk Copy Operations


                A single bulk copy command is executed to perform a single
                operation against a database.
                To perform the bulk copy operation, you need to perform the
                following steps:
                •   Connect to the source server    using (SqlConnection
                                                    sourceConnection = new
                    to get the data to be copied.
                                                    SqlConnection(connectionString)){
                •   Connect to the destination                sourceConnection.Open();
                    server.                         SqlCommand commandSourceData = new
                                                    SqlCommand("SELECT * FROM
                •   Create a SqlBulkCopy            sourceTable;", sourceConnection);
                    object.                         SqlDataReader reader =
                                                    commandSourceData.ExecuteReader();
                •   Set the
                    DestinationTableName
                    property to the name of the
                    target table.
                •   Call the WriteToServer()
                    method.
     Ver. 1.0                          Session 8                           Slide 14 of 28
Developing Database Applications Using ADO.NET and XML
Executing Single Bulk Copy Operations (Contd.)


                A single bulk copy command is executed to perform a single
                operation against a database.
                To perform the bulk copy operation, you need to perform the
                following steps:
                •   Connect to the source server
                    to get the data to be copied.
                •   Connect to the destination      using (SqlConnection
                    server.                         destinationConnection = new
                                                    SqlConnection(connectionString))
                •   Create a SqlBulkCopy            {                         destin
                    object.                         ationConnection.Open();
                                                    }
                •   Set the
                    DestinationTableName
                    property to the name of the
                    target table.
                •   Call the WriteToServer()
                    method.
     Ver. 1.0                          Session 8                           Slide 15 of 28
Developing Database Applications Using ADO.NET and XML
Executing Single Bulk Copy Operations (Contd.)


                A single bulk copy command is executed to perform a single
                operation against a database.
                To perform the bulk copy operation, you need to perform the
                following steps:
                •   Connect to the source server
                    to get the data to be copied.
                •   Connect to the destination
                    server.
                •   Create a SqlBulkCopy            using (SqlBulkCopy bulkCopy =
                                                    new SqlBulkCopy
                    object.                         (destinationConnection))
                •   Set the
                    DestinationTableName
                    property to the name of the
                    target table.
                •   Call the WriteToServer()
                    method.
     Ver. 1.0                          Session 8                           Slide 16 of 28
Developing Database Applications Using ADO.NET and XML
Executing Single Bulk Copy Operations (Contd.)


                A single bulk copy command is executed to perform a single
                operation against a database.
                To perform the bulk copy operation, you need to perform the
                following steps:
                •   Connect to the source server
                    to get the data to be copied.
                •   Connect to the destination
                    server.
                •   Create a SqlBulkCopy
                    object.
                •   Set the                         bulkCopy.DestinationTableName =
                    DestinationTableName            "targetTable";
                    property to the name of the
                    target table.
                •   Call the WriteToServer()
                    method.
     Ver. 1.0                          Session 8                           Slide 17 of 28
Developing Database Applications Using ADO.NET and XML
Executing Single Bulk Copy Operations (Contd.)


                A single bulk copy command is executed to perform a single
                operation against a database.
                To perform the bulk copy operation, you need to perform the
                following steps:
                •   Connect to the source server
                    to get the data to be copied.
                •   Connect to the destination
                    server.
                •   Create a SqlBulkCopy
                    object.
                •   Set the
                    DestinationTableName
                    property to the name of the
                    target table.
                •   Call the WriteToServer()        bulkCopy.WriteToServer(reader);
                    method.
     Ver. 1.0                          Session 8                           Slide 18 of 28
Developing Database Applications Using ADO.NET and XML
Just a minute


                Which class is used for bulk copy of records?




                Answer:
                   SqlBulkCopy



     Ver. 1.0                       Session 8                   Slide 19 of 28
Developing Database Applications Using ADO.NET and XML
Executing Multiple Bulk Copy Operations


                •   Multiple bulk copy command is executed to perform multiple
                    operations against a database.
                •   It can be done by using a single instance of
                    a SqlBulkCopy class.
                •   Performing multiple bulk copy operations by using the same
                    instance of SqlBulkCopy is usually more efficient than by
                    using a separate instance for each operation.




     Ver. 1.0                           Session 8                      Slide 20 of 28
Developing Database Applications Using ADO.NET and XML
Demo: Performing Bulk Copy Operations


                Problem Statement:
                   The employee details of Tebisco are stored in the Employees
                   table of the HR database. The records of this table have got
                   deleted accidentally. However, a backup copy of the table is
                   available. This table is of the name EmployeeBackUp. The
                   records from the EmployeeBackUp table need to be copied to
                   the Employees table.
                   You are a developer in Tebisco. You have been told to create
                   an application that will copy the records from EmployeeBackUp
                   to Employees table. Create an application that will perform the
                   required task in the shortest possible time.




     Ver. 1.0                         Session 8                          Slide 21 of 28
Developing Database Applications Using ADO.NET and XML
SQL Notification


                Data caching improves the performance of applications
                because it prevents repeated roundtrips to the database
                server. In this way, database resources are conserved.
                However, ensuring that the cache maintains updated
                records is a challenging task. You need to know when the
                cache expires, that is, when the data has been modified in
                the database server.
                To provide this functionality, SQL notification, which is
                based on the Service Broker infrastructure, is used.




     Ver. 1.0                       Session 8                       Slide 22 of 28
Developing Database Applications Using ADO.NET and XML
Enabling SQL Notification


                To use query notifications, you need to:
                •   Enable Service Broker for     ALTER DATABASE DATABASE_NAME
                                                  SET ENABLE_BROKER;
                    the database.
                                                  CREATE QUEUE
                                                       ContactChangeMessages;
                •   Ensure that the user ID       CREATE SERVICE
                                                  USE DATABASE_NAME
                                                       ContactChangeNotifications
                    used to connect to the        GRANT SUBSCRIBE QUERY
                                                       ON QUEUE
                    database has the necessary    NOTIFICATIONS TO
                                                       ContactChangeMessages
                                                  database_principal
                    permissions.                       ([http://schemas.microsoft
                                                       .com/SQL/Notifications/Pos
                                                  SqlClientPermission permission
                                                       tQueryNotification]);
                                                  = new
                                                  SqlClientPermission(Permission
                                                  State.Unrestricted);
                                                  try{
                                                  permission.Demand();
                                                        return true;
                                                     }


     Ver. 1.0                         Session 8                           Slide 23 of 28
Developing Database Applications Using ADO.NET and XML
Executing SQL Notification


                To execute SQL notification, you need to:
                  Initialize the                 SqlDependency.Start(connectio
                   SqlDependency class            nString);
                   using the Start()
                   method. This method
                   takes a connection string
                   as a parameter.
                  Set the Notification           using (SqlCommand cmd = new
                   property of the                SqlCommand(sql, cn))
                   SqlCommand object. It is       {
                   set by passing an object          cn.Open();
                   of SqlCommand to the              dep = new
                   constructor of                    SqlDependency(cmd);
                   SqlDependency object.
                  Declare the OnChange           dep.OnChange += dep_OnChange;
                   event on the
                   SqlDependency object.
     Ver. 1.0                         Session 8                         Slide 24 of 28
Developing Database Applications Using ADO.NET and XML
Just a minute


                How would you enable Service Broker for a database called
                Northwind?




                Answer:
                   ALTER DATABASE Northwind SET ENABLE_BROKER;



     Ver. 1.0                      Session 8                      Slide 25 of 28
Developing Database Applications Using ADO.NET and XML
Demo: Implementing SQL Notification


                Problem Statement:
                   The details of the various positions in Tebisco are stored in a
                   Position table, which is maintained by the HR management
                   team. This table has the following fields:
                     Position code
                     Description
                     Budgeted strength
                     Year
                     Current strength
                   The budgeted strength and the current strength fields of this
                   table are frequently modified. As a result, the management of
                   Tebisco has decided to cache the records of this table. Create
                   an application that will notify the vacancy initiation application
                   about any change in values of these two fields for the current
                   year.


     Ver. 1.0                          Session 8                             Slide 26 of 28
Developing Database Applications Using ADO.NET and XML
Summary


               In this session, you learned that:
                 While working with large objects (LOB), it is advisable to use
                  streaming techniques. This conserves the system resources.
                 If a LOB is stored in the database in a binary format, it is
                  referred to as Binary Large Object (BLOB).
                 The normal operation of the DataReader object is to read one
                  row at a time. To access the DataReader object in a stream
                  fashion, you can change the DbCommand object’s behavior to a
                  sequential stream when you execute the ExecuteReader()
                  method.
                 You can use the SQL Server UPDATETEXT function to write the
                  BLOB data in chunks of a specified size. The UPDATETEXT
                  function requires a pointer to the BLOB field being updated, so
                  the SQL Server TEXTPTR function is first called to get a pointer
                  to the field of the record to be updated.


    Ver. 1.0                          Session 8                          Slide 27 of 28
Developing Database Applications Using ADO.NET and XML
Summary (Contd.)


                The SqlBulkCopy class is used to copy large amounts of
                 data to the SQL Server database tables.
                A single bulk copy command is executed to perform a single
                 operation against a database.
                Multiple bulk copy command is executed to perform multiple
                 operations against a database.
                Multiple bulk copy operation can be done by using a
                 single instance of a SqlBulkCopy class (for SQL Server).
                Query notifications are useful for applications that need to
                 refresh displays or caches.
                     To use query notifications, you need to:
                        Enable query notifications for your database.
                        Ensure that the user ID used to connect to the database has the
                         necessary permissions.
                    To execute SQL notifications, you need to use the
                     SqlDependency class.


    Ver. 1.0                           Session 8                                   Slide 28 of 28

More Related Content

What's hot

introduction to Mongodb
introduction to Mongodbintroduction to Mongodb
introduction to MongodbASIT
 
Transforming Big Data with Spark and Shark - AWS Re:Invent 2012 BDT 305
Transforming Big Data with Spark and Shark - AWS Re:Invent 2012 BDT 305Transforming Big Data with Spark and Shark - AWS Re:Invent 2012 BDT 305
Transforming Big Data with Spark and Shark - AWS Re:Invent 2012 BDT 305mjfrankli
 
Introduction to MapReduce and Hadoop
Introduction to MapReduce and HadoopIntroduction to MapReduce and Hadoop
Introduction to MapReduce and HadoopMohamed Elsaka
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introductionsethfloydjr
 
Advanced Sharding Techniques with Spider (MUC2010)
Advanced Sharding Techniques with Spider (MUC2010)Advanced Sharding Techniques with Spider (MUC2010)
Advanced Sharding Techniques with Spider (MUC2010)Kentoku
 
Java ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and LocalizationJava ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and LocalizationAndreas Jakl
 
OpenDremel's Metaxa Architecture
OpenDremel's Metaxa ArchitectureOpenDremel's Metaxa Architecture
OpenDremel's Metaxa ArchitectureCamuel Gilyadov
 
Processing massive amount of data with Map Reduce using Apache Hadoop - Indi...
Processing massive amount of data with Map Reduce using Apache Hadoop  - Indi...Processing massive amount of data with Map Reduce using Apache Hadoop  - Indi...
Processing massive amount of data with Map Reduce using Apache Hadoop - Indi...IndicThreads
 
It's the memory, stupid! CodeJam 2014
It's the memory, stupid!  CodeJam 2014It's the memory, stupid!  CodeJam 2014
It's the memory, stupid! CodeJam 2014Francesc Alted
 
Blosc: Sending Data from Memory to CPU (and back) Faster than Memcpy by Franc...
Blosc: Sending Data from Memory to CPU (and back) Faster than Memcpy by Franc...Blosc: Sending Data from Memory to CPU (and back) Faster than Memcpy by Franc...
Blosc: Sending Data from Memory to CPU (and back) Faster than Memcpy by Franc...PyData
 
Blosc Talk by Francesc Alted from PyData London 2014
Blosc Talk by Francesc Alted from PyData London 2014Blosc Talk by Francesc Alted from PyData London 2014
Blosc Talk by Francesc Alted from PyData London 2014PyData
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Barry DeCicco
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryAlexandre Morgaut
 
Scalable XQuery Processing with Zorba on top of MongoDB
Scalable XQuery Processing with Zorba on top of MongoDBScalable XQuery Processing with Zorba on top of MongoDB
Scalable XQuery Processing with Zorba on top of MongoDBWilliam Candillon
 
VLDB 2009 Tutorial on Column-Stores
VLDB 2009 Tutorial on Column-StoresVLDB 2009 Tutorial on Column-Stores
VLDB 2009 Tutorial on Column-StoresDaniel Abadi
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know Norberto Leite
 

What's hot (19)

introduction to Mongodb
introduction to Mongodbintroduction to Mongodb
introduction to Mongodb
 
Transforming Big Data with Spark and Shark - AWS Re:Invent 2012 BDT 305
Transforming Big Data with Spark and Shark - AWS Re:Invent 2012 BDT 305Transforming Big Data with Spark and Shark - AWS Re:Invent 2012 BDT 305
Transforming Big Data with Spark and Shark - AWS Re:Invent 2012 BDT 305
 
mongodb tutorial
mongodb tutorialmongodb tutorial
mongodb tutorial
 
Introduction to MapReduce and Hadoop
Introduction to MapReduce and HadoopIntroduction to MapReduce and Hadoop
Introduction to MapReduce and Hadoop
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
 
Advanced Sharding Techniques with Spider (MUC2010)
Advanced Sharding Techniques with Spider (MUC2010)Advanced Sharding Techniques with Spider (MUC2010)
Advanced Sharding Techniques with Spider (MUC2010)
 
Java ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and LocalizationJava ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and Localization
 
OpenDremel's Metaxa Architecture
OpenDremel's Metaxa ArchitectureOpenDremel's Metaxa Architecture
OpenDremel's Metaxa Architecture
 
Processing massive amount of data with Map Reduce using Apache Hadoop - Indi...
Processing massive amount of data with Map Reduce using Apache Hadoop  - Indi...Processing massive amount of data with Map Reduce using Apache Hadoop  - Indi...
Processing massive amount of data with Map Reduce using Apache Hadoop - Indi...
 
It's the memory, stupid! CodeJam 2014
It's the memory, stupid!  CodeJam 2014It's the memory, stupid!  CodeJam 2014
It's the memory, stupid! CodeJam 2014
 
Blosc: Sending Data from Memory to CPU (and back) Faster than Memcpy by Franc...
Blosc: Sending Data from Memory to CPU (and back) Faster than Memcpy by Franc...Blosc: Sending Data from Memory to CPU (and back) Faster than Memcpy by Franc...
Blosc: Sending Data from Memory to CPU (and back) Faster than Memcpy by Franc...
 
Blosc Talk by Francesc Alted from PyData London 2014
Blosc Talk by Francesc Alted from PyData London 2014Blosc Talk by Francesc Alted from PyData London 2014
Blosc Talk by Francesc Alted from PyData London 2014
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06
 
Updates
UpdatesUpdates
Updates
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love Story
 
Cloud jpl
Cloud jplCloud jpl
Cloud jpl
 
Scalable XQuery Processing with Zorba on top of MongoDB
Scalable XQuery Processing with Zorba on top of MongoDBScalable XQuery Processing with Zorba on top of MongoDB
Scalable XQuery Processing with Zorba on top of MongoDB
 
VLDB 2009 Tutorial on Column-Stores
VLDB 2009 Tutorial on Column-StoresVLDB 2009 Tutorial on Column-Stores
VLDB 2009 Tutorial on Column-Stores
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know
 

Similar to Ado.net session08

Building services using windows azure
Building services using windows azureBuilding services using windows azure
Building services using windows azureSuliman AlBattat
 
Ado.net session05
Ado.net session05Ado.net session05
Ado.net session05Niit Care
 
INT 222.pptx
INT 222.pptxINT 222.pptx
INT 222.pptxSaunya2
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile appsIvano Malavolta
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Svetlin Nakov
 
Introduction to Apache Tajo: Data Warehouse for Big Data
Introduction to Apache Tajo: Data Warehouse for Big DataIntroduction to Apache Tajo: Data Warehouse for Big Data
Introduction to Apache Tajo: Data Warehouse for Big DataGruter
 
Gain Proficiency in Batch Processing with Spring Batch
Gain Proficiency in Batch Processing with Spring BatchGain Proficiency in Batch Processing with Spring Batch
Gain Proficiency in Batch Processing with Spring BatchInexture Solutions
 
JDK1.7 features
JDK1.7 featuresJDK1.7 features
JDK1.7 featuresindia_mani
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12Vince Vo
 
Lecture14Slides.ppt
Lecture14Slides.pptLecture14Slides.ppt
Lecture14Slides.pptVideoguy
 
Hibernate jj
Hibernate jjHibernate jj
Hibernate jjJoe Jacob
 
05 entity framework
05 entity framework05 entity framework
05 entity frameworkglubox
 
Azure, Cloud Computing & Services
Azure, Cloud Computing & ServicesAzure, Cloud Computing & Services
Azure, Cloud Computing & ServicesAlan Dean
 
Class 26: Objectifying Objects
Class 26: Objectifying ObjectsClass 26: Objectifying Objects
Class 26: Objectifying ObjectsDavid Evans
 

Similar to Ado.net session08 (20)

NodeJs Modules.pdf
NodeJs Modules.pdfNodeJs Modules.pdf
NodeJs Modules.pdf
 
Building services using windows azure
Building services using windows azureBuilding services using windows azure
Building services using windows azure
 
Ado.net session05
Ado.net session05Ado.net session05
Ado.net session05
 
INT 222.pptx
INT 222.pptxINT 222.pptx
INT 222.pptx
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile apps
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015
 
Introduction to Apache Tajo: Data Warehouse for Big Data
Introduction to Apache Tajo: Data Warehouse for Big DataIntroduction to Apache Tajo: Data Warehouse for Big Data
Introduction to Apache Tajo: Data Warehouse for Big Data
 
Gain Proficiency in Batch Processing with Spring Batch
Gain Proficiency in Batch Processing with Spring BatchGain Proficiency in Batch Processing with Spring Batch
Gain Proficiency in Batch Processing with Spring Batch
 
Lecture 11.pdf
Lecture 11.pdfLecture 11.pdf
Lecture 11.pdf
 
JDK1.7 features
JDK1.7 featuresJDK1.7 features
JDK1.7 features
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
Lecture14Slides.ppt
Lecture14Slides.pptLecture14Slides.ppt
Lecture14Slides.ppt
 
Performance .NET Core - M. Terech, P. Janowski
Performance .NET Core - M. Terech, P. JanowskiPerformance .NET Core - M. Terech, P. Janowski
Performance .NET Core - M. Terech, P. Janowski
 
Hibernate jj
Hibernate jjHibernate jj
Hibernate jj
 
Intake 37 linq3
Intake 37 linq3Intake 37 linq3
Intake 37 linq3
 
05 entity framework
05 entity framework05 entity framework
05 entity framework
 
Azure, Cloud Computing & Services
Azure, Cloud Computing & ServicesAzure, Cloud Computing & Services
Azure, Cloud Computing & Services
 
Class 26: Objectifying Objects
Class 26: Objectifying ObjectsClass 26: Objectifying Objects
Class 26: Objectifying Objects
 

More from Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 

Recently uploaded

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Ado.net session08

  • 1. Developing Database Applications Using ADO.NET and XML Objectives In this session, you will learn to: Retrieve and store large binary data Perform bulk copy operations Enable SQL notification to maintain and update a cache Ver. 1.0 Session 8 Slide 1 of 28
  • 2. Developing Database Applications Using ADO.NET and XML Handling Binary Large Objects in a Database While working with data, you may need to move large objects (LOB) between the client application and the database server. LOBs are of different formats:  Binary Large Object (BLOB): If a LOB is stored in a database in a binary format, it is referred to as BLOB.  Character Large Object (CLOB): If a LOB is stored in the database in a textual format, it is referred to as CLOB. Ver. 1.0 Session 8 Slide 2 of 28
  • 3. Developing Database Applications Using ADO.NET and XML Retrieving BLOB Data • To access the DataReader object in a stream fashion, you can change the DbCommand object’s behavior to a sequential stream when you execute the ExecuteReader() method. • In the stream mode, you must get the bytes from the stream in the order of each column that is being returned. • Refer to following code snippet that retrieves photographs and stores them into a file: byte[] outbyte = new Declare the BLOB byte[] byte[bufferSize]; buffer, which will be filled by GetBytes. bufferSize connection.Open(); Open the connection and read refers to the size of the BLOB SqlDataReader myReader = buffer. the DataReader. data into command.ExecuteReader (CommandBehavior. SequentialAccess); while (myReader.Read()){ Ver. 1.0 Session 8 Slide 3 of 28
  • 4. Developing Database Applications Using ADO.NET and XML Retrieving BLOB Data (Contd.) string fileName = Create a file to store the image. @"C:Patient" + ".bmp"; fs = new Write the BLOB to a .bmp file. FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); bw = new BinaryWriter(fs); Stream the BLOB to the FileStream object, fs. startIndex = 0; Reset the starting position. bw is an object of retval = Read the bytes into outbyte[] BinaryWriter. myReader.GetBytes(1, and store the number of bytes returned. startIndex, outbyte, 0, bufferSize); while (retval == bufferSize) Continue to read and write while { bytes are remaining. bw.Write(outbyte); bw.Flush(); Ver. 1.0 Session 8 Slide 4 of 28
  • 5. Developing Database Applications Using ADO.NET and XML Retrieving BLOB Data (Contd.) startIndex += Reposition the start index to the end of the last buffer and fill the bufferSize; retval = buffer. myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize); } bw.Write(outbyte, 0, Write the remaining buffer. (int)retval - 1); bw.flush(); Ver. 1.0 Session 8 Slide 5 of 28
  • 6. Developing Database Applications Using ADO.NET and XML Storing BLOB Data • You can write BLOB data to a database by executing INSERT or UPDATE statement. • If the BLOB data is stored in a text format, you can pass the data as a string parameter. • The following code snippet updates a photograph with a new one from the file: command.CommandText = "Select Retrieve a pointer to the photograph. TEXTPTR(PatientPhoto) The SQL Server TEXTPTR from Patients where function is first called to get a PatientId='1004'"; pointer to the field of the record photoPtr = to be updated. (byte[])command.ExecuteScalar(); Ver. 1.0 Session 8 Slide 6 of 28
  • 7. Developing Database Applications Using ADO.NET and XML Storing BLOB Data (Contd.) using (SqlCommand command = connection.CreateCommand()) command.CommandText = "UPDATETEXT The SQL Server UPDATETEXT function writes the BLOB data in Patients.PatientPhoto chunks of a specified size. @Pointer @Offset null The pointer entire existing the photograph. Delete the to the start of data. current offset to insert data. @Data"; SqlParameter ptrParm The data being sent to the database. = command.Parameters.Add ("@Pointer", SqlDbType.Binary, 16); ptrParm.Value = photoPtr; SqlParameter photoParm = command.Parameters.Add("@Dat a", SqlDbType.Image); SqlParameter offsetParm = command.Parameters.Add("@Off set", SqlDbType.Int); Ver. 1.0 Session 8 Slide 7 of 28
  • 8. Developing Database Applications Using ADO.NET and XML Storing BLOB Data (Contd.) offsetParm.Value = 0; using (FileStream file = new Read chunks of the file into FileStream("C:newPhoto.gif“, the buffer and send the chunks to the database. FileMode.Open, FileAccess.Read)){ int count = file.Read (buffer, 0, bufferSize); while (count != 0) { photoParm.Value = buffer; photoParm.Size = count; command.ExecuteNonQuery(); currentIndex += count; offsetParm.Value = currentIndex; count = file.Read(buffer, 0, bufferSize);}} Ver. 1.0 Session 8 Slide 8 of 28
  • 9. Developing Database Applications Using ADO.NET and XML Just a minute Fill in the blanks: The SQL Server _______ function is first called to get a pointer to the field of the record to be updated. Answer: TEXTPTR Ver. 1.0 Session 8 Slide 9 of 28
  • 10. Developing Database Applications Using ADO.NET and XML Just a minute Fill in the blanks: The SQL Server _______ function writes the BLOB data in chunks of a specified size. Answer: UPDATETEXT Ver. 1.0 Session 8 Slide 10 of 28
  • 11. Developing Database Applications Using ADO.NET and XML Demo: Handling BLOB Data From a Database Problem Statement: A new employee has joined Tebisco. The details of this employee need to be inserted to the Employees table. The details are as follows:  Employee code: 000017  First name: Peter  Last name: Martin In addition, the photograph of the employee should be uploaded to the database. It should be displayed after the photograph is inserted. Create an application that will perform the required tasks. Ver. 1.0 Session 8 Slide 11 of 28
  • 12. Developing Database Applications Using ADO.NET and XML Performing Bulk Copy Operations • There can be occasions when you need to copy large amounts of data from one location to another. • The SqlBulkCopy class allows you to write bulk in SQL Server based tables. • The data is copied from the data source to the destination table, specified by the DestinationTableName property, by using the WritetoServer() method of the SqlBulkCopy class. Ver. 1.0 Session 8 Slide 12 of 28
  • 13. Developing Database Applications Using ADO.NET and XML Performing Bulk Copy Operations (Contd.) • The following figure displays the various parameters that can be passed to the WritetoServer() method in order to copy data in bulk. WritetoServer() Data DataRow Array SQL Source DataTable Server IDataReader DataRowState Copies all rows from the DataRow array to a destination table Copies all rows in the DataTable to a destination table Copies all rows in the IDataReader interface to a destination table Copies only rows that match the row state in the DataTable to a destination Ver. 1.0 Session 8 Slide 13 of 28
  • 14. Developing Database Applications Using ADO.NET and XML Executing Single Bulk Copy Operations A single bulk copy command is executed to perform a single operation against a database. To perform the bulk copy operation, you need to perform the following steps: • Connect to the source server using (SqlConnection sourceConnection = new to get the data to be copied. SqlConnection(connectionString)){ • Connect to the destination sourceConnection.Open(); server. SqlCommand commandSourceData = new SqlCommand("SELECT * FROM • Create a SqlBulkCopy sourceTable;", sourceConnection); object. SqlDataReader reader = commandSourceData.ExecuteReader(); • Set the DestinationTableName property to the name of the target table. • Call the WriteToServer() method. Ver. 1.0 Session 8 Slide 14 of 28
  • 15. Developing Database Applications Using ADO.NET and XML Executing Single Bulk Copy Operations (Contd.) A single bulk copy command is executed to perform a single operation against a database. To perform the bulk copy operation, you need to perform the following steps: • Connect to the source server to get the data to be copied. • Connect to the destination using (SqlConnection server. destinationConnection = new SqlConnection(connectionString)) • Create a SqlBulkCopy { destin object. ationConnection.Open(); } • Set the DestinationTableName property to the name of the target table. • Call the WriteToServer() method. Ver. 1.0 Session 8 Slide 15 of 28
  • 16. Developing Database Applications Using ADO.NET and XML Executing Single Bulk Copy Operations (Contd.) A single bulk copy command is executed to perform a single operation against a database. To perform the bulk copy operation, you need to perform the following steps: • Connect to the source server to get the data to be copied. • Connect to the destination server. • Create a SqlBulkCopy using (SqlBulkCopy bulkCopy = new SqlBulkCopy object. (destinationConnection)) • Set the DestinationTableName property to the name of the target table. • Call the WriteToServer() method. Ver. 1.0 Session 8 Slide 16 of 28
  • 17. Developing Database Applications Using ADO.NET and XML Executing Single Bulk Copy Operations (Contd.) A single bulk copy command is executed to perform a single operation against a database. To perform the bulk copy operation, you need to perform the following steps: • Connect to the source server to get the data to be copied. • Connect to the destination server. • Create a SqlBulkCopy object. • Set the bulkCopy.DestinationTableName = DestinationTableName "targetTable"; property to the name of the target table. • Call the WriteToServer() method. Ver. 1.0 Session 8 Slide 17 of 28
  • 18. Developing Database Applications Using ADO.NET and XML Executing Single Bulk Copy Operations (Contd.) A single bulk copy command is executed to perform a single operation against a database. To perform the bulk copy operation, you need to perform the following steps: • Connect to the source server to get the data to be copied. • Connect to the destination server. • Create a SqlBulkCopy object. • Set the DestinationTableName property to the name of the target table. • Call the WriteToServer() bulkCopy.WriteToServer(reader); method. Ver. 1.0 Session 8 Slide 18 of 28
  • 19. Developing Database Applications Using ADO.NET and XML Just a minute Which class is used for bulk copy of records? Answer: SqlBulkCopy Ver. 1.0 Session 8 Slide 19 of 28
  • 20. Developing Database Applications Using ADO.NET and XML Executing Multiple Bulk Copy Operations • Multiple bulk copy command is executed to perform multiple operations against a database. • It can be done by using a single instance of a SqlBulkCopy class. • Performing multiple bulk copy operations by using the same instance of SqlBulkCopy is usually more efficient than by using a separate instance for each operation. Ver. 1.0 Session 8 Slide 20 of 28
  • 21. Developing Database Applications Using ADO.NET and XML Demo: Performing Bulk Copy Operations Problem Statement: The employee details of Tebisco are stored in the Employees table of the HR database. The records of this table have got deleted accidentally. However, a backup copy of the table is available. This table is of the name EmployeeBackUp. The records from the EmployeeBackUp table need to be copied to the Employees table. You are a developer in Tebisco. You have been told to create an application that will copy the records from EmployeeBackUp to Employees table. Create an application that will perform the required task in the shortest possible time. Ver. 1.0 Session 8 Slide 21 of 28
  • 22. Developing Database Applications Using ADO.NET and XML SQL Notification Data caching improves the performance of applications because it prevents repeated roundtrips to the database server. In this way, database resources are conserved. However, ensuring that the cache maintains updated records is a challenging task. You need to know when the cache expires, that is, when the data has been modified in the database server. To provide this functionality, SQL notification, which is based on the Service Broker infrastructure, is used. Ver. 1.0 Session 8 Slide 22 of 28
  • 23. Developing Database Applications Using ADO.NET and XML Enabling SQL Notification To use query notifications, you need to: • Enable Service Broker for ALTER DATABASE DATABASE_NAME SET ENABLE_BROKER; the database. CREATE QUEUE ContactChangeMessages; • Ensure that the user ID CREATE SERVICE USE DATABASE_NAME ContactChangeNotifications used to connect to the GRANT SUBSCRIBE QUERY ON QUEUE database has the necessary NOTIFICATIONS TO ContactChangeMessages database_principal permissions. ([http://schemas.microsoft .com/SQL/Notifications/Pos SqlClientPermission permission tQueryNotification]); = new SqlClientPermission(Permission State.Unrestricted); try{ permission.Demand(); return true; } Ver. 1.0 Session 8 Slide 23 of 28
  • 24. Developing Database Applications Using ADO.NET and XML Executing SQL Notification To execute SQL notification, you need to:  Initialize the SqlDependency.Start(connectio SqlDependency class nString); using the Start() method. This method takes a connection string as a parameter.  Set the Notification using (SqlCommand cmd = new property of the SqlCommand(sql, cn)) SqlCommand object. It is { set by passing an object cn.Open(); of SqlCommand to the dep = new constructor of SqlDependency(cmd); SqlDependency object.  Declare the OnChange dep.OnChange += dep_OnChange; event on the SqlDependency object. Ver. 1.0 Session 8 Slide 24 of 28
  • 25. Developing Database Applications Using ADO.NET and XML Just a minute How would you enable Service Broker for a database called Northwind? Answer: ALTER DATABASE Northwind SET ENABLE_BROKER; Ver. 1.0 Session 8 Slide 25 of 28
  • 26. Developing Database Applications Using ADO.NET and XML Demo: Implementing SQL Notification Problem Statement: The details of the various positions in Tebisco are stored in a Position table, which is maintained by the HR management team. This table has the following fields:  Position code  Description  Budgeted strength  Year  Current strength The budgeted strength and the current strength fields of this table are frequently modified. As a result, the management of Tebisco has decided to cache the records of this table. Create an application that will notify the vacancy initiation application about any change in values of these two fields for the current year. Ver. 1.0 Session 8 Slide 26 of 28
  • 27. Developing Database Applications Using ADO.NET and XML Summary In this session, you learned that:  While working with large objects (LOB), it is advisable to use streaming techniques. This conserves the system resources.  If a LOB is stored in the database in a binary format, it is referred to as Binary Large Object (BLOB).  The normal operation of the DataReader object is to read one row at a time. To access the DataReader object in a stream fashion, you can change the DbCommand object’s behavior to a sequential stream when you execute the ExecuteReader() method.  You can use the SQL Server UPDATETEXT function to write the BLOB data in chunks of a specified size. The UPDATETEXT function requires a pointer to the BLOB field being updated, so the SQL Server TEXTPTR function is first called to get a pointer to the field of the record to be updated. Ver. 1.0 Session 8 Slide 27 of 28
  • 28. Developing Database Applications Using ADO.NET and XML Summary (Contd.)  The SqlBulkCopy class is used to copy large amounts of data to the SQL Server database tables.  A single bulk copy command is executed to perform a single operation against a database.  Multiple bulk copy command is executed to perform multiple operations against a database.  Multiple bulk copy operation can be done by using a single instance of a SqlBulkCopy class (for SQL Server).  Query notifications are useful for applications that need to refresh displays or caches. To use query notifications, you need to:  Enable query notifications for your database.  Ensure that the user ID used to connect to the database has the necessary permissions.  To execute SQL notifications, you need to use the SqlDependency class. Ver. 1.0 Session 8 Slide 28 of 28

Editor's Notes

  1. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  2. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  3. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  4. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  5. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  6. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  7. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  8. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  9. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  10. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  11. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  12. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  13. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  14. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  15. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  16. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  17. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  18. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  19. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  20. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  21. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  22. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  23. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  24. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  25. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  26. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  27. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
  28. Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.