Click here to Skip to main content
12,359,088 members (32,870 online)
Click here to Skip to main content
Articles » Web Development » Ajax » General » Downloads

Stats

99.8K views
3.7K downloads
166 bookmarked
Posted

HTTP Push from SQL Server — Comet SQL

, 27 Nov 2012 CPOL
This article provides an example solution for presenting data in "real-time" from Microsoft SQL Server in an HTML browser. The article presents how to implement Comet functionality in ASP.NET and how to connect Comet with Query Notification from SQL Server.
// Copyright (c) 2010 Daniel Wojciechowski 
// E-mail: at gmail.com daniel.wojciechowski

using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;

namespace HttpPushFromMsSql
{
    public static class Tools
    {
        /// <summary>
        /// Convert DateTime to string containing time with miliseconds
        /// </summary>
        public static string ToTimeStringWithMilisecond(DateTime time)
        {   // Based on http://msdn.microsoft.com/en-us/library/bb882581.aspx
            // Append millisecond pattern (".fff") to current culture's time pattern
            string miliPattern = DateTimeFormatInfo.CurrentInfo.LongTimePattern;
            miliPattern = Regex.Replace(miliPattern, "(:ss|:s)", "$1.fff");
            return time.ToString(miliPattern);
        }

        private static bool notificationEnabled = false;
        public static void EnableNotifications(string connString)
        {
            if (notificationEnabled) // prevent for calling twice 
                return; // TODO: Do it in other way. For example call
                        // those methods on application start:

            // Go to following link to fix problems with user rights to Query Notification:
            // http://www.simple-talk.com/sql/t-sql-programming/using-and-monitoring-sql-2005-query-notification/

            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connString);
            bool startResult = SqlDependency.Start(connString);
            Debug.WriteLineIf(!startResult, "SqlDependency.Start(\"" + connString +
                                           "\") returned false: compatible listener already exists.");
            notificationEnabled = true;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

CoperNick
Software Developer
Poland Poland
I graduated from the Jagiellonian University. After defence of master thesis (link to presentation) I have started professional programming in C and C++ using OpenGL. After that, my Company gave me a chance to switch to .Net framework and C# and I have betrayed my passion for computer graphics. Now I'm C#, SQL and sometimes ASP.NET programmer.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 27 Nov 2012
Article Copyright 2010 by CoperNick
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid