I want to create application which update their control properties at the time of table values updated from database by another user. i find a solution by refreshing a control every second by help of timer, but it is not good i want to change after updating a values not to request every second SQL Server?
Basically you need to have SQL post when the table is updated. This called a push model (the opposite is a pull model which you are now doing).
You can use SqlDependency for this. See http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency%28v=vs.100%29.aspx
However, there is some overhead associated with a push approach. Many apps just do a query every n seconds (3 - 10) and then only update the control when the properties changed.
It really is a optimisation trade off. Is making a network request (pull model) more "expensive" than having SQL server doing the trigger and then send the update message (push model)?
You can use SqlDependency for this, without polling.
Implementing SQL Server 2005 Query Notifications in C# Windows Application
Related
I have a list( XPCollection) bound to a XPLiteObject (Attente)
We work on a LAN, I'd like when the client (in the client app)change something in he database(CRUD operation) the list(XPCollection) for the server app should be updated(add the new rows if there is, and update data)
How to manage that?
I have tow app, the server one and the client one, the client app look for the SqlServer database and make change.
XPO does not track changes made in the database. A possible workaround is to use the feature described in this blog: XPO Beta feature: SqlDependency support.
XPO does not expose SqlDependency notifications, so there is no way to track the exact moment when the changes were made. You can simply repeat queries in a small time interval. For example, execute the XPCollection.Reload method on every 1000th millisecond.
This will not impact the application performance, because XPO will return data from cache, without performing actual query to the SQL Server. However, when another user updates data, the query will be performed to update the cache. So, the data in the client application will always be up to date.
I have a windows application form. The table is called messages and whenever user inserts updates or deletes a message, the date/timestamp is stored in the column UserAction.
This column information should be retrieved and shown in the UI as a link. Clicking that link has an action and the link should be seen as read.
Can someone help me in giving some ideas to achieve this action?
Thanks in advance.
I am not sure how you can trigger the event from database and display the details in the UI but I think below is doable:
Add INSERT/DELETE triggers in your table with the code to insert data in UserAction table.
Build an UI which reads the data from UserAction and displays in the UI.
You may want to refresh your UI to refresh the data from database UserAction at certain interval.
This way, you UI will poll for any changes UserAction at certain interval and refresh the page with refreshed data.
You can use Query Notifications, although there are some limitations to what can be monitored.
SQL Server 2005 introduced query notifications, new functionality that allows an application to request a notification from SQL Server when the results of a query change. Query notifications allow programmers to design applications that query the database only when there is a change to information that the application has previously retrieved.
This functionality is exposed via the SqlDependency class. You should review the Remarks section as part of evaluating whether Query Notifications are for you:
SqlDependency was designed to be used in ASP.NET or middle-tier services where there is a relatively small number of servers having dependencies active against the database. It was not designed for use in client applications, where hundreds or thousands of client computers would have SqlDependency objects set up for a single database server. If you are developing an application where you need reliable sub-second notifications when data changes, review the sections Planning an Efficient Query Notifications Strategy and Alternatives to Query Notifications in the Planning for Notifications topic in SQL Server Books Online.
You could try something like this:
CREATE TRIGGER dbo.trMessagesUpdateTimeStamp
ON dbo.Messages
FOR INSERT, UPDATE, DELETE
AS
UDPATE dbo.Messages
SET UserAction = GETDATE()
FROM Inserted i
WHERE dbo.YourTableHere.ID = i.ID
Points to check out:
the INSERT case could easily more easily be handled by a DEFAULT constraint on the UserAction column - that way, upon INSERT the defined default value (e.g. GETDATE()) would be inserted
you need to be able to join the Inserted pseudo table that holds all the rows that were modified by the last statement (and this can be multiple rows) with the Messages table - I've assumed here that you have some kind of an ID column that serves as primary key on Messages - adapt this as needed.
This handles the database part of the story - I'm not quite sure from your question what you want to do in the UI ...
I would like to write C# code which would help me to query a database continuously.
If there is an entry made (not sure when will the entry be made) to a database by some source, it should be processed by my code. (Simply, my code should always monitor the database for an entry).
You basically have two main options.
Firstly, you can add an extra column to your table that is a bit field, perhaps called "HasBeenProcessed". You can then schedule checking for any columns whose HasBeenProcessed is 0. Processed that, then update the column to be 1.
Alternatively, you can look at this StackoverFlow post that describes using the SqlDependency class to have an event raised when a resultset appears differently.
If you are using SQL Server 2008 R2 or SQL Server 2012, you can use StreamInsight to setup events against data changes and patterns. If you are using a previous version > 2005, you can use Notification Services (although StreamInsight is far superior and essentially supercedes Notification Services).
Add a WebService to your code.
Add a insert & update trigger to your table.
Create a CLR function that you can call from the trigger.
In that CLR function, call the webservice with all necessary data.
In the webservice, do whatever you need to do based on the passed parameters.
Add a configuration table, and enter the webservice URL.
Pass the url as argument to your CLR function.
You should call the webservice asynchronously, so the delay is minimized.
Note:
I think it's also possible to call a web-service from pure SQL, so there's not necessarely a need for the CLR.
What are you trying to accomplish? If it's keeping a relatively static but still possibly changing data set (e.g. list of vendors) up to date in a cache, Simon Whitehead's proposal of SqlDependency will serve you well. If it's processing something more akin to requests (e.g. orders), service broker with activation, either internal or external, should be a good fit. Either way, the point is don't have your code constantly going to the database saying "is there something for me?". Instead, have the db tell your application when there's something to do and act accordingly.
I am in the process of creating a friendlist using ASP.NET/C# and MSSQL 08. Simple datalist that lists the profile image and name of my friends.
Next to the name, I have a label showing current status of my friend. Like for instance, Online, Offile, Away etc.
My question is, how can I change the value of this label, without having a timer that calls the database all the time asking for the current status?
I would like to have the database (sql server 2008) tell me when a change as occured and tell my business logic to update the status label.
Is this possible?
Thanks!
To accomplish what you are looking for.. And this is just how I would do it, is to create a view based on the table with only the items that are needed to accomplish the task.. For instance, UserID | Online_Status.. Then using AJAX, make a call. It would be so small to the user that they would not even notice the bandwidth usage/processing... etc..etc...
This is pretty much exactly what you said you didn't want, but even if you had 1 million users and space them like 3-5 minutes apart.. You should be ok considering it would take milliseconds to perform the check.
Just my two cents..
I don't think you should do it like that. There are techniques to do this using comet but it will consume a lot of resources from your server clearly reducing the number of users that can access your site/app. The problem is that the the server and client needs to have a socket open for the server to be able to push data to the client.
What I would do is to have the client ask if there are any updates, keeping the payload to a minimum. If the server says there is data that changed the client makes another request to get that data.
You could use the SqlDependency class to get notified when the result of a database query changes.
There is an excellent article on MSDN explaining the SqlDependency class.
To use the SqlDependency class in the context of ASP.Net consider the strategy explained in the following video of MIX 2011.
Hope, this helps.
I believe this is what for the SqlCacheDependency is designed for. If you are using SQL Server 2005 or higher*, it implements a push-notification model from SQL Server to your application to notify you of when a change occurs in your dataset. So each time the cache is invalidated you can get the latest data, but until then it was just will read from your cached dataset and save a trip to the database. The documentation for it is here.
*However*,
As stated in the comments and such, this isn't really what SQL Server is designed for at its core, and I don't know to hand actually how efficient this solution is. If I understand your problem correctly, you would need a cache dependency PER USER which could very well be completely unscalable using this solution. Rather than second-guess what is going to be the most efficient solution, you really should develop, test, measure and find out for yourself. Every situation is going to be different, there is no "right way".
* In Sql Server 2000 and 7 it uses a pull-model.
All options given to this moment are valid ones and that's how most websites do it today; however, the OP is asking for some sort push notification mechanism as opposed to pull, and I think for that kind of thing, websockets are the way to do it.
I want to sync 2 database specific records.
Let suppose I have two databases;
1.Shop
2.Stock
Now lets suppose user change the price of a specific product in stock. I want to change this product price in shop also!
What I work out is that - assuming Internet connection is stable,
When price change in stock I invoke a web service this service will insert entries in web data table price.
Now on shop side I ping that web data table using web service every 20 minutes if I find any new entry I update that relevant product price in shop!
Another option I thought about was replication. But we are using express edition of SQL Server and according to my knowledge express edition can not work as publisher!
Is my first option is efficient for this purpose or am I missing something and there is a better alternative to accomplish this purpose!
You could have a trigger on the table like pRime says above but instead of writing directly to the other database write the changes to a local "staging" table and then every 20 min or so schedule a task to send the updates to the second db.
You could set up the second DB as a Linked Server.
This way you avoid making the table the trigger is on Read Only if the connection between the two dbs goes down.
you can create a trigger on Stock table.
CREATE TRIGGER triggerName
ON [Stock].[dbo].[products]
AFTER UPDATE
AS
IF ( UPDATE (productPrice))
BEGIN
--insert to shop
END
GO
IF can't use the MS SQL Server replication feature (requires some non-Express edition as you already identified) for this situation (see http://msdn.microsoft.com/en-us/library/ms151198.aspx) then another option is to use the MS Sync Framework (can work with DBs down to SQL CE etc., files even custom data sources etc.) - see http://msdn.microsoft.com/en-us/library/bb726002.aspx .
IF you really want to implement this in code yourself (I strongly recommend against that) then implement it as a "push-scenario" :
DB triggers which fill staging tables
Windows Service which does check for changes in the staging tables and apply themn
conflict resolution rules
complete logging of all this to be able to analyze discrepancies (just in case)