asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

How to Make SQL Dependency Work Where It Doesn't
By Sandeep Bhatia
Rating: 3.1 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

  • download source code
  • Introduction

    The intent of the article is not to let one know about the internals of SQL Dependency nor it is meant to guide readers to scenarios where SQL Dependency should be used. We already have some very good content on the site that details internals and usage of query notification in SQL Server 2005. Some of the articles are listed below in the Suggested Reading section. The purpose of this article is to instead illustrate a mechanism via which a developer can harness SQL dependency and bypass its limitations.

    Constraints Faced When Using SQL Dependency

    SQL Dependency is built on top of the query notification architecture in SQL Server which uses Indexed views to implement query notifications (Refer: Suggested Reading). All views cannot be Indexed hence any limitations that surface in indexing views also surface for using SQL Dependency. To view a list of limitations associated with Indexed views refer to View Restrictions Section in Improving Performance with SQL Server 2005 Indexed Views.

    Sample Scenario

    Consider a sample scenario where we need to get the count of all the employees from the employee table along with the employee and jobs data. Our sample database schema is as below containing two tables as shown here:

    CREATE TABLE Employee 
    (
      EmployeeName VARCHAR(50),
      EmployeeAddress VARCHAR(255),
      JobId INT
    )
    GO
    CREATE TABLE Job
    (
      JobId INT IDENTITY(1,1),
      JobName VARCHAR(50),
      JobDescription VARCHAR(255)
    )
    GO
    

    The simplest case where we can retrieve the dataset of employee names with the jobs involves an OUTER JOIN. If we refer the View Restrictions section in the above referred article on TechNet we will find a restriction on OUTER JOIN. So if we had a requirement where our data needs to be updated or notified for any updates on SQL Server tables we would not be able to use SQL Dependency in this case as a Query subscription will not get built in this case. Below is a simple stored procedure that we will use to retrieve the employee data and the jobs associated irrespective of whether they have any jobs assigned in their name or not.

    CREATE PROC GetEmployeeAndJob
    AS
    BEGIN
      SELECT EmployeeName,EmployeeAddress,JobName
      FROM dbo.Employee EMP
      LEFT OUTER JOIN dbo.Job JOB
      ON EMP.JobId = JOB.JobId
    END
    

    The requirement is to get the data dependency created for the result set of the above procedure. We will not be able to create a direct SQL dependency on the above procedure at the OUTER JOIN prohibits to do that. Let's develop a work around going forward that will help us by pass this constraint of SQL Dependency.

    Logical View

    Refer to the logical view here

    The above view illustrates that we use additional two procedures GetSurrogateEmployee and GetSurrogateJob in the whole setup. The Intent of these two procedures is to create SQL Dependencies on behalf of the main result set. Below is the step wise flow how this would work.

    1. Fire Main Procedure in this case GetEmployeeAndJob.
    2. Get the result set, cache it or refer it for usage later using static variables or some other technique to make references live.
    3. Fire the surrogate procedure GetSurrogateEmployee.
    4. Build a dependency on the procedure in Step 3 with a defined callback.
    5. Fire the surrogate procedure GetSurrogateJob.
    6. Build a dependency on the procedure in Step 4 with a defined callback (same call back as in step 4).

    The above six steps will help you understand that the nature of the surrogate procedure should be such that they comply with all restrictions on Index views in other words they can have a successful dependency built over them. In our case these surrogate procedures are two, divided as per the tables you want to monitor for changes here. The Result set in the main procedure will change if the data in table Job or Employee changes hence the surrogate procedures are as simple as this.

    CREATE PROC SurrogateEmployee
    AS
      SELECT EmployeeName,EmployeeAddress,JobId
      FROM 
      dbo.Employee EMP
    GO
    
    CREATE PROC SurrogateJob
    AS
      SELECT JobId,JobName,JobDescription
      FROM 
      dbo.Job JOB
    GO
    

    Notice the explicit schema binding and simplicity in the queries that make them suitable candidates for notifications in SQL Server.

    SurrogateEmployee will create a dependency that will fire when anything changes in the Employee table, which inturn will invoke a callback that can refresh the referred data in the main procedure (Fire GetEmployeeAndJob again in the callback to refresh the data).

    SurrogateJob will behave the same way but invoke the dependency when the anything changes in the Job table.

    To sum up, the setup here behaves as if the Main Procedure was created with a corresponding SQL Dependency. You can have any logic in the surrogate procedures above till you restrain yourself from using forbidden constructs for SQL dependency.

    SQL Dependency Test Harness

    The methodology above to achieve SQL dependency on custom result sets that don't follow the query notification rules is implemented in the Test Harness below, you can get the corresponding code and change it the way you want to get an insight into the same.

    DependencyController ctrl = new DependencyController();
    string[] procedures = new string[2];
    //These are the procedures that will support the dataset
    //usage for the main procedure that cannot be supported
    //directly with SQL Dependency
    procedures[0] = "SurrogateJob";
    procedures[1] = "SurrogateEmployee";
    //This is the main procedure, as it has the outer join,
    //SQL dependency will not work for the same so we have
    //the above two procedures for rescue here
    string actualprocedure = "GetEmployeeAndJob";
    //My sql server string
    string connstr = "Server=sandeepb;Trusted_Connection=yes;database=msdb";
    //Main procedure that does all plumbing work for you to
    //create dependencies and uses surrogate procedures.
    //For it to work with sql procedure with parameters
    //you just need to modify the plumbing code slightly.
    ctrl.ExecuteProcedureAndCreateDependency(connstr, procedures, actualprocedure);
    

    ExecuteProcedureAndCreateDependency is the procedure that will create a logical SQL Dependency construct by creating actual dependencies on procedures SurrogateJob, SurrogateEmployee passed as a string array here. You can modify the implementation to support parameterized procedures, here we avoid that to focus on the implementation code for the technique.

    The implementation of the ExecuteAndCreateDependency creates dependency on each surrogate procedure as below

    //This is the first time the method is run
    foreach(string surrogate in procedureForCache)
    {
      //create dependency using the surrogate procedures
      Utility.CreateDependency(connStr, surrogate, CachedResultSet, ActualProcedure);
    }

    Dependency creation registers a call back that takes care to update the main result set, refer to the lines of code in CreateDependency.

    DependencyState stateObject = new DependencyState(connStr, procedureForCache, ActualProcedure, cachedResult);

    depend.OnChange += new OnChangeEventHandler(stateObject.CallBackOnQuery);

    After the dependencies are created the main dataset is retrieved by RunAndCache Method that stores the reference for call back based updating later.

    Callback created on dependencies does two jobs, one to create dependency again another to refresh the data of the main procedure.

    //callback again creates the dependencies and fires the
    //main procedure, you need to recreate deps everytime
    //they are fired.
    Utility.CreateDependency(connectionString, spToCache, CachedResult, ActualProcedure);
    //A operation the main procedure when callbacks on
    //surrogate are fired is the main action point here.
    Utility.RunAndCache(connectionString, ActualProcedure,CachedResult);
    //MessageBox.Show(DependencyController.CachedResultSet.Tables[0].Rows.Count.ToString());

    Refer to the attached code to view the detailed implementation, you can run the code after creating the tables and data with the attached script. Modify your connection string accordingly.

    Every time an update is made on the Job or Employee Table you will be able to see the data updated in the referenced dataset, you can check the row count being increased after each run (In this case it just appends the data at each execution.). In the screen shot below, CreateDependency is used to do the first main execution on the sample; every time we run an update on the table as shown below the data value returned by the Check Cached Count will change.

    Conclusion

    The example presented here is just a suggested work around to address the limitations of SQL Dependency. It is not meant to be a generic best known method and may not be the best solution for your particular needs. Determining that is left to the reader since every situation is different.

    Suggested Reading

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES