Update C# client whenever database is updated - c#

I am using MySQL with C# / Visual Studio 2008. I need to know any method so that if one user updates the database, all the logged in users over the network, are notified that a change has been occurred.
If i logged in the database through my application, at that time the serial no of the latest record in the database was 10, and then i someone with the privileges updates the record in the database and at the same time i am being notified that the database has been updated with a new record.
Common example can be of MSN messenger's alert for a friend when he gets online...
Though i really don't want that popup, but alert functionality is something like that...
One more thing to ask:
I want a functionality like if a record's status is not updated (for say about more than 3 hours), then i want all users to get alert about that issue also. This functionality is needed for all of the records, not specifically for new records.

Quite a long answer, but your best bet would be something like SqlCacheDependency.
Also you could use something called the "Reverse AJAX" technique, which is used by Google Mail notifications, and talked about here: How does incoming mail notification on Gmail works?
Besides those two, your own other options AFAIK is simple server polling.
Also, you haven't mentioned what your client is.
Is it a public web site on the internet? Is it an intranet app, it is a WPF app?
If it's a web site, you're best best is client-side callbacks using the Reverse AJAX technique i mentioned.

You probably need to design some kind of poll functionality in your client. For example, you can send a request each minute asking the database "Have you updated anything since [date the client latest got updates]?" If the database answers true, you let the client request the full set of updates. If not, you let it sleep another minute.

Using the SqlDependency Class is a better way to make all your data driven more faster and efficient. It just remove the need of constantly re-query your database checking every time when a changes is made in the data.

Related

C# Best pattern for actions triggered by dates

I've written applications to handle this situation dozens of times over the years. I just want to know if there's a better way.
Here are some examples:
A table contains an expiration date. After that date, I need to make a notification of the expiration (email, messenger, etc).
Someone has not logged in for X days. Need to send a notification.
The way I currently handle it:
Create a table of sent notifications.
Create a view in the database of all expirations/late logins with no record of a notification already sent.
Write a Windows Service to scan the view every X minutes and send the notifications, write them out to the "Sent Notifications" table (which will make it disappear from the view).
Is there any better way of accomplishing this task? I only ask because sometimes some new technology passes me by.
Thanks in advance
I think you could find a possible solution via SSIS (Sql Server Integration Services). You can create a package to monitor the porcess and to send email to those people who need to be informed.

Notify user of a change to data through ajax or some other mechanism

I need to try to notify a users who are modifying the same page that an update was made to an Excel grid SPA. I was thinking about passing pack and forth the date modified timestamp and if the original is in the past from the current in the database it would mean the grid was updated by someone else. Is there a better way to do this?
Since you've mentioned AJAX, I'll assume this is a web application. This sounds like an excellent candidate for bi-directional communication via websockets. I've used SignalR with great success. It will allow you to publish events from the server to any subscribed clients, allowing you to easily update what they are viewing.

Server Sent Events with SQL

I can't seem to find a way to do this, so I decided to ask here. I am making an asp.net site which uses data from a SQL Server database. I am using javascript to get the data and format it as I want.
The issue is that I want to use server sent events in order to get the new entry in my database and display it in the page of the site. So far the only examples I saw were with timers on the server side and on the period they send data to the javascript. But I can't seem to figure out how I should do it so that when a new row enters the database to fire the event.
That should be done on server side but I don't have a clue where to begin.
SqlDependency:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency.aspx
http://dotnet.dzone.com/articles/c-sqldependency-monitoring
Using the SqlDependency Class is a good way to make your data driven application (whether it be Web or Windows Forms) more efficient by removing the need to constantly re-query your database checking for data changes.
That’s how you use the SqlDependency Class for monitoring data changes in your database without having to use something like a timer control to re-query at certain intervals.
Here is an old book mark:
http://rusanu.com/2006/06/17/the-mysterious-notification/

Use a database to handle application Licensing

I was wondering if it was possible, and if so how can, someone efficiently add a licensing aspect to an application using a database? For example if someone has a customer that has an application developed in C# that interacts with a database (sends a query and gets databack), would it be possible to have it so that if that database did not have a "License" the C# application would not function? Ideally what I am trying to do is be able to develop several applications that interact with the same database and instead of having each application manage it's own license I want to use the database since all pipes lead back to the ocean.
If any more information is needed please let me know.
I actually am working with this right now. It's still in a development phase so I do not know how things will work in production. (Though I target very specific set of customers so chances are small that I end up with thousands of users)
What I did was, I added a login screen to my C# WPF application where you'll fill in your password etc. Then this information is send using a SOAP webservice to the server where it'll validate the license. If this function returns true I allow the user to log in, store the credentials of the customer in RAM and for every request my app makes it sends these along, only returning data if license is valid (to prevent people from 'hacking' by simply never shutting down the computer (I have a time-based license)). The way I do it works perfectly, little to no noticeable delays (then again it's still in beta).
I don't know if your apps are licensed seperately or anything so that part you'll have to figure out for yourself.

Data-Driven Websites for Very Small Businesses

I have a client who has a product-based website with hundreds of static product pages that are generated by Microsoft Access reports and pushed up to the ISP via FTP (it is an old design). We are thinking about getting a little more sophisticated and creating a data-driven website, probably using ASP.NET MVC.
Here's my question. Since this is a very small business (a handful of employees), I'd like to avoid enterprise patterns like web services if I can. How does one push updated product information to the website, batch-style? In a SQL Server environment, you can't just push up a new copy of the database, can you?
Clarification: The client already has a system at his facility where he keeps all of his product information and specifications. I would like to refresh the database at the ISP with this information.
You don't mention what exactly the data source is, but the implication is that it's not already in SQL Server. If that's the case, have a look at SSIS.
If the source data is in SQL Server, then I think you'd want to be looking at either transactional replication or log shipping to sync the two databases.
If you are modernizing, and it is a handful of employees, why would you push the product info out batch style?
I don't know exactly what you mean by "data driven", but why not allow the ASP.NET app to query the SQL Server product catalog database directly? Why generate static pages at all?
UPDATE: ok, I see, the real question is, how to update the SQL database running at the ISP.
You create an admin panel so the client can edit the data directly on the server. It is perfectly reasonable to have the client keep all their records on the server as long as the server is backed up nightly. Many cloud and virtual services offer easy ways to do replicated backups.
The additional benefit of this model is that more than one user can be adding or updating records at a time, making the workforce a lot more scalable. Likewise, the users can log in from anywhere they have a web browser to add new records, fix mistakes made in old records, etc.
EDIT: This approach assumes you can convince the client to abandon their current data entry system in favor of a centralized web-based management panel. Even if this isn't the case, the SQL database can be hosted on the server and the client's application could be made to talk to that so you're only ever using one database. From the sounds of it, it's a set of Access forms and macros which you should have source access to.
Assuming that there is no way to sync the data directly between your legacy system DB (is it in Access, or is Access just running the reports) and the SQL Server DB on the website (I'm not aware of any):
The problem with "pushing" the data directly into the SQL server will be that "old" (already in the DB) records won't be updated, but instead removed and then recreated. This is a big problem with foreign keys. Plus, I really don't like the idea of giving the client any access to the db at all.
So considering that, I find that the best is to write a relatively simple page that takes an uploaded file and updates the database. The file will likely be CSV, possibly XML. After a few iterations of writing these pages over the years, here's what I've come up with:
Show file upload box.
On next page load, save file to temp location
Loop through each line (element in XML) and validate all the data. Foreign keys, especially, but also business validations. You can also validate that the header row exists, etc. Don't update the database.
3a. If invalid data exists, save an error message to an array
At the end of the looping, show the view.
4a. If there were errors, show the list of error messages and tell them to re-upload the file.
4b. If there were no errors, create a link that has the file location from #2 and a confirmation flag
After the file location and confirm flag have been submitted run the loop in #3 again, but there's an if (confirmed) {} statement that actually makes the updates to the db.
EDIT: I saw your other post. One of the assumptions I made is that the databases won't be the same. ie, the legacy app will have a table or two. Maybe just products. But the new app will have orders, products, categories, etc, etc. This will complicate "just uploading the file".
Why do you need to push anything?
You just need to create a product management portion of the webpage and a secondly a public facing portion of the webpage. Both portions would touch the same SqlServer database.
.Net has the ability to monitor a database and check for updates. then you can run a query to [push] the data elsewhere.
or use sql to push the data with a trigger on the table(s) in question.
Is this what you were looking for?
You can try Dynamic Data Web Application.
You should have a service that regularly updates the data in the target DB. It will probably run on your source data machine (where the Access-DB is)
The service can use SSIS or ADO.NET to write the data. You can do this over the web, because you have access via TCP/IP to the server I assume.
Please check when the updates are done and how long it takes. If you can do the updates during the night you are fine. If not you should check, if you can still access the web during the import. That is sometimes not the case.
Use wget to push the new data file to the mvc app and once the data is received by the action, the mvc app invokes the processing/importing of the data (maybe in a worker process if you dont want long requests).

Categories