Intro to SQL # For Twitterizer

In this post we’re talking a little bit about an add-on tool for SQL called SQL #.  The idea is to give programmers useful functions in T-SQL and Enterprise Manager.  It is a library of CLR (Common Language Runtime) functions so it works on SQL 2005+.  Getting started is pretty easy and is on the home page of the website:

The simplest way to start is to create an empty database called “Sharp”, the run the script they provide and open a new query window.

You can review a lot of the features here, but, what really piqued my interest was RegEx, String, and Twitter features.  I have wasted a lot of coding time in the past re-validating data passed from the middle tier.  Why doesn’t Microsoft put the .Net Framework into Enterprise Manager?  They gave us SMO (SQL Management Objects) so we could mess with SQL in C# (probably to the dismay of all DBA’s that pay attention), so why not the other way?  Solomon Rutzky tired of that question and built SQL#.

Let’s start with a few examples.  I’ll be using the AdventureWorks database on a SQL 2008 R2 (or as we like to call it: SQL 2010…)  If you don’t have that database, you can get it here.  These examples are taken/modified from another great blog.

/* SQL# Calculating business days */
SELECT  wo.DueDate,
        [WorkingDays]=Sharp.SQL#.Date_BusinessDays(wo.DueDate, wo.EndDate, 3),
FROM    Production.WorkOrder AS wo
WHERE   wo.EndDate > wo.DueDate ;

/* SQL# Calculating the distance between two points */
SELECT  [Meters]=a1.SpatialLocation.STDistance(a2.SpatialLocation),
FROM    Person.Address AS a1
        JOIN Person.Address AS a2
        ON a2.AddressID = 2
           AND a1.AddressID = 1;

/* SQL# Delete files older than n-days via T-SQL  */
SELECT SQL#.File_Delete(files.Location + '' + files.Name)
FROM SQL#.File_GetDirectoryListing(@StartingDirectory, @Recursive, @DirectoryNamePattern, @FileNamePattern) files
WHERE files.LastWriteTime < (GETDATE() - 3)

Let’s move onto selecting your posts from Twitter.  First make sure you’ve followed these instructions:

/* SQL# Return a table of tweets  */
DECLARE		@ConsumerKey		NVARCHAR(100),
		@ConsumerSecret		NVARCHAR(100),
		@AccessToken		NVARCHAR(100),
		@AccessTokenSecret	NVARCHAR(100)
SELECT		@ConsumerKey = 'x',
		@ConsumerSecret = 'y',
		@AccessToken = '7-z',
		@AccessTokenSecret = 'z'

SELECT		StatusText,Created,ScreenName,UserName
FROM		SQL#.Twitter_GetFriendsTimeline(@ConsumerKey, @ConsumerSecret,
                                        @AccessToken, @AccessTokenSecret, NULL)

This is the same functionality I demo’d in C# in my first post.

In conclusion, this gives me an alternative to LINQ for business logic and it allows me to keep it in the stored procedures.  Which is especially nice when I have to write any reports where I don’t get to pass into a C# layer.


Twitterizer & .Net: Adding a feed to your website

Create a Twitter user:

Register your application:
This gives you:
    <add key=”AccessToken” value=”blahblahblah”/>
    <add key=”AccessTokenSecret” value=”blahblahblah”/>
    <add key=”ConsumerKey” value=”blahblahblah”/>
    <add key=”ConsumerSecret” value=”blahblahblah”/>
Add it to your web.config

Go to:
Download the Twitterizer package:

Twitterizer2.dll and Newtonsoft.Json.dll
in your project references

Make an ASPX page with:
                    <div id=”homeDev”>
                        <fieldset style=”width: 230px”>
                            <legend>Development Buzz</legend>

Do this in the .CS
 public partial class Home : System.Web.UI.Page
        private string _data1;
        public string Data1
            get { return _data1; }
            set { _data1 = value; }
        protected void Page_Load(object sender, EventArgs e)

        protected void ConnectTwitter()
            OAuthTokens tokens = new OAuthTokens();
            tokens.AccessToken = ConfigurationManager.AppSettings[“AccessToken”];
            tokens.AccessTokenSecret = ConfigurationManager.AppSettings[“AccessTokenSecret”];
            tokens.ConsumerKey = ConfigurationManager.AppSettings[“ConsumerKey”];
            tokens.ConsumerSecret = ConfigurationManager.AppSettings[“ConsumerSecret”];
            TwitterResponse<TwitterStatusCollection> statuses = TwitterTimeline.HomeTimeline(tokens);
            for (int i = 0; i < statuses.ResponseObject.Count; i++)
                Data1 += “<p>”;
                Data1 += statuses.ResponseObject[i].Text.ToString();
                Data1 += “</p>”;

© Copyright Duke Hall - Designed by Pexeto