displaying updation of data to multiple users whic

2019-07-22 10:15发布

问题:

i need to show the updated data immediately to a user as he inputs into the database , i.e. online view.

i am using sql server and building a c# .net winform application. the application would be used on LAN by 3 users, U1,U2,U3.

the users would input data into an ITEMS table using the application and as U2 clicks the insert button , he should view the updated data on the same Form at the bottom in a gridview or else , and also, U1,U3 would also automatically see the updated table on their PCs in the application.

how do i do this?

U1,U2,U3 have opened the application.

U2 has navigated to the INSERT items Form.

U1,U3 are on the view items Form.

i want that as U2 inserts data, then automatically the grid view on the application of U1,U3 is updated and they could see the new data inserted by U2 in the ITEMS table. they don't even need to refresh or reopen the view items Form.

Trigger have to be used on the ITEMS table. is there any way to avoid triggers here?

回答1:

You do not need a trigger (at least not for the purpose described).

The common practice is to implement a refresh-on-demand capability (usually F5).

It is a bit absurd to keep the PCs or screens up-to-date with the database all the time. Consider the network traffic. Consider that no -one is there to read the screen when U1 or U2 answer the phone, go for lunch or a comfort break. A Refresh on demand capability is more reasonable.

Further, if you have to implement such a function, add a Last-Updated-TimeStamp or DateTime column to the table, and only retrieve the rows that have been updated since the last retrieval.

Response to Comments

Thanks.

  1. On each table, include a Timestamp or Datetime column, named something like UpdatedDateTime. Set a default of GETDATE().

  2. Update this column whenever you update the row. Note that you can only meaningfully have one data window in focus, ie. in front, all other windows are out of focus, cannot be seen. So there is no point in coding the refresh or automatic refresh into every window; only those windows that actually need automatic refresh.

  3. On the client or .NET side, for each window that needs refresh, write code for the Function-5 button. Whenever F5 is activated, refresh the window. That means:

    • fill your List Object (containing the logical data rows) for that window:
      SELECT column_list
          FROM table t1
          JOIN join_table t2 ...
          WHERE t1.UpdatedDateTime > @SavedDateTime
          OR    t2.UpdatedDateTime > @SavedDateTime
          ... other_conditions
    • SET @SavedDateTime = GETDATE()
    • Paint the window That will avoid moving masses of dat that do not change, across the network; and move only those rows that have changed into the window. An intelligent refresh.
  4. Test that. Ensure that the manual refresh works as intended. Update the data on another PC, press F5 on this PC, and verify that the data in the window changes to reflect the updates; and that the window is now repainted when data does not change.

  5. Now for the automatic refresh. Write an endless loop (always bad new), that waits 30 seconds, and executes the F5 button code. Now check that that works as intended.

  6. If and when someone very senior wants their window refreshed more frequently (than 30 seconds), change that wait parameter to 15 seconds. If and when someone very senior wants their window refreshed more frequently (than 15 seconds), change that wait parameter to 10 seconds. Et cetera. Never set the wait to 1 or 2 or 5 seconds to begin with. And allow each PC to set its own wait period (big boss 5 seconds, little boss 30 seconds).

  7. When the bosses wail about the network being slow, show them the packets that are flooding the network when no one is sitting in front of the PC to read the window, and suggest that they turn the Automatic Refresh off. The manual refresh remains available for anyone who is actually sitting in front of the window, whose fingers are not broken, to use.



回答2:

you can do this by running a separate thread which will after a certain time span keep updating the data.



回答3:

I would consider using to Event Notification/Service broker to notify clients of any data changes. No need to poll or refresh on the client.

And more detail on the blog of the Service Broker team