Contents tagged with Database
There are several posts out there on using SignalR to signal changes in a database, but because I really didn’t quite like any of them, I decided to write my own.
The possibility to detect changes in a database comes from the SqlDependency class. Other implementations exist, such as SqlCacheDependency and SqlNotificationRequest, but with these we don’t really receive a notification event. The process is a bit tricky, lots of people out there have problems. These classes only work with SQL Server non-Express, but there are equivalents for other databases, such as OracleDependency for Oracle.
Because I like to make things generic and reusable, I decided to encapsulate things in my class. Unfortunately, since SqlDependency does not have an ADO.NET base class, we have to use it directly.
So, my purpose is to:
Have a generic class that, given a database connection and SQL string that illustrates the changes I would like to detect, starts monitoring them, and raises an event whenever it sees changes;
I also want a SignalR hub that I can call from the outside – in this case, the changes event – and broadcasts a message about these changes.
First things first, here is the ChangeNotifier class:
It all starts with the Start method, which receives the name of a connection string stored in Web.config, and the SQL to use for detecting changes. When they occur, it raises the Change event. Action stops when the Stop or Dispose methods are called – do note that this class implements the Dispose Pattern, but in a particular way, because it is not meant to be subclassed. Whenever the OnChange event is raised, we need to set it up again, that’s how SqlDependency works. Because I don’t want to expose directly .NET classes, I created my own event classes and enumerations:
A connection named MyConnection should exist in Web.config, as well as a table SomeTable with a column SomeColumn. Do change those for your own.
Next, a SignalR notification hub:
What it does is merely broadcast to connected parties an OnChange message. We can get a reference to it by calling GetHubContext:
The call to $.connection should match you hub class, but using camel casing. The callback function onChange is the same as you call in ChangeNotificationHub and in the OnChange event handler.
And that’s basically it!
Some common gotchas:
Does not work with SQL Server Express;
There’s a specific order by which the connection and SqlDependency.Start must be called, if you stick to my code, it should work;
Your application pool identity needs to have permissions to connect to the notification queue, see them here;
Won’t work with any SQL query, a number of restrictions apply; see here;
SqlDependency is very sensitive, and you can easily run into problems if you try different things.
Besides the classic ASP.NET data source controls, SharePoint brings along its own. These allow us to retrieve data not just from SharePoint, but also from external sources, such as web services or SQL databases. Using SharePoint Designer you end up using these data sources without even knowing, and, specially, ignoring what other options you have. Let’s look at these controls one by one and see how the can be used. I already talked about the SPDataSource and AggregateDataSource, so I won’t cover these here. I also won’t be covering them in depth, but instead will provide an example of how to use them.
A simple example is:
A BdcDataSource let’s you retrieve data from a BCS data source (external content type). This may happen when you don’t have an external list created or you want to call a specific finder method. You need to specify the namespace, LOB instance, entity, and finder method. If the finder needs parameters, you will need to supply them. It can be customized by hosting it in a DataFormWebPart and by applying XSLT.
Here’s an example:
SoapDataSource is the control that retrieves data from a SOAP web service. You specify the URL of the web service, the action to call and the SOAP envelope, together with any required parameters. It should be hosted in a DataFormWebPart and can thus use XSLT for the formatting of its contents.
An example of calling the Lists.asmx web service:
XsltListViewWebPart (External List)
External lists can be displayed using the XsltListViewWebPart. Nothing really new here.
A simple example (of course, do replace the list and view GUIDs):
The XmlUrlDataSource is used for invoking REST web services. Similar to SoapDataSource, you need to pass it the URL, but also the HTTP method and any parameters. I is also usually hosted in a DataFormWebPart.
Here’s an example of calling the weather service I talked about in another post:
The SPSqlDataSource is what SharePoint uses to access a SQL database. If you use SharePoint Designer to add one such data source, you might be surprised to see that it places a SqlDataSource instead. The thing is, SharePoint uses ASP.NET tag mapping to replace any SqlDataSource control for a SPSqlDataSource. Again, we host it inside a DataFormWebPart for easily formatting its contents and you can pass parameters to the SQL.
An example of the markup:
While SPDataSource is generally the most generic and useful control, it cannot do everything, namely, access external sources. For that, we have other options; the AggregateDataSource can be used to bring together data from all of these sources, except SPHierarchyDataSourceControl and SPCalendarDataSource, but you can easily replace these by SPDataSource. I did not mention SPWorkflowDataSource because it isn't a general-purpose data source control, it is merely used to set or get workflow association parameters.
Let me know if you have any questions!
It is normal in databases to have hierarchical tables, that is, tables that are related with themselves, forming a parent-child relation. For example, consider this:
The parent_id column points to the parent record, which, in some cases, will not exist.
So, imagine we have a number of records, such as:
How can we find the id of the topmost parent? In this case, it will always be 1, of course.
In SQL Server, we have two options:
- A Common Table Expression (CTE);
- A recursive function.
Let’s see how to implement each.
Common Table Expression Approach
We need to write a CTE that starts with some record and goes all the way up until it finds the parent. Let’s wrap it in a nice scalar function:
I won’t explain here how CTEs work, they have been around for quite some time, and there are several posts how there for that.
Recursive Function Approach
The other approach is using a recursive function. The gotcha here is that when we create a function, it is compiled, and if it has a reference to itself – which doesn’t exist first – it will fail. Therefore, we need to first create a dummy function and then change it to do what we want:
You can get results from the two functions by running the following T-SQL queries:
Interesting, both execution plans are exactly the same:
I can’t really recommend one over the other, since from my tests, both took the same amount of time (you will need far more records than the ones from my sample to tell that).
So, any thoughts from database gurus out there?
There's an hidden gem in SQL Server 2008: Change Data Capture (CDC). Using CDC we get full audit capabilities with absolutely no implementation code: we can see all changes made to a specific table, including the old and new values!