• Save
Ado.net session08
Upcoming SlideShare
Loading in...5
×
 

Ado.net session08

on

  • 950 views

 

Statistics

Views

Total Views
950
Views on SlideShare
941
Embed Views
9

Actions

Likes
1
Downloads
0
Comments
1

1 Embed 9

http://niitcourseslides.blogspot.in 9

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Why is the save option disabled for this particular session?
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.

Ado.net session08 Ado.net session08 Presentation Transcript

  • Developing Database Applications Using ADO.NET and XMLObjectives 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 XMLHandling 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 XMLRetrieving 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 View slide
  • Developing Database Applications Using ADO.NET and XMLRetrieving 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 View slide
  • Developing Database Applications Using ADO.NET and XMLRetrieving 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 XMLStoring 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 XMLStoring 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 XMLStoring 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 XMLJust 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 XMLJust 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 XMLDemo: 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 XMLPerforming 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 XMLPerforming 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 XMLExecuting 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 XMLExecuting 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 XMLExecuting 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 XMLExecuting 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 XMLExecuting 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 XMLJust 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 XMLExecuting 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 XMLDemo: 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 XMLSQL 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 XMLEnabling 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 XMLExecuting 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 XMLJust 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 XMLDemo: 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 XMLSummary 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 XMLSummary (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