Results 1 to 8 of 8

Thread: [RESOLVED] SQL Server - Notified when a new entry is added?

  1. #1

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Resolved [RESOLVED] SQL Server - Notified when a new entry is added?

    Hi guys and gals, my question for you is:

    I have a VB.Net application that connects to a SQL Server database on my web host at GoDaddy. I'm wanting to use it for a chat program so that users can add messages to the database and then everyone else gets the new messages when it re-queries the database.

    The only problem is that the only way to get the new messages is to re-query the database every # seconds.

    Is there any way to have my program not re-query the database until an entry is added by another user?
    Would the SQL.SQLNotificationRequest be used for this?

    knxrb
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server - Notified when a new entry is added?

    Did you look at Insert triggers on the tables?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: SQL Server - Notified when a new entry is added?

    You should look at WCF to fix that problem.
    it's possible to push to clients with this technology.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  4. #4

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: SQL Server - Notified when a new entry is added?

    Insert triggers on the tables, hmm, I've seen them in the SQL Server admin area when I was adding the tables but I don't know what they are for as I've just started using SQL Server.

    I'll take a look at those and also WCF if the triggers don't work, thanks guys.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server - Notified when a new entry is added?

    I don't think a DB is a proper place for what you are looking for - a DB is more of a repository for data. A place to store the chat log from a chat session. There are other "pc-to-pc" communication methods that more closely match what you are looking for here.

    With that said - if you really want to do it in a table in SQL - it's all as simple as having your "chat" table have a simple integer type primary key that is an identity value.

    And then having your client program call a query that asks for all rows with a PK value greater then the last "max" PK value that was SELECTED.

    And you can also protect your "INSERT's" of new rows into this table so that they do not INSERT if a "newer" row is already in place - that way you would "know" to hold that INSERT until you did your SELECT (as noted above) and shown that "new post" to your client.

    There is no need for triggers or notification events (I've read that notification is not the "great" feature that you would think it to be).

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: SQL Server - Notified when a new entry is added?

    Hmm, thanks szlamany. I think your idea is the simplest and probably the most effective for my problem

    knxrb
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server - Notified when a new entry is added?

    btw - to do an INSERT that will fail if someone else has "since added a new row" do the following.

    Assume a table with two columns - ChatId as int and ChatText as varchar(100)

    And assume that you "know" the last "max" ChatId is 15.

    You are expecting to insert 16.

    This query will NOT INSERT if the "current max" ChatId is greater then 15.

    Note that with IDENTITY columns they auto-number - so you don't specify them in the INSERT statement - just the text.

    Code:
    Insert into ChatTable
       Select 'New Chat Text'
       Where (Select Max(ChatId) From ChatTable)=15
    This INSERT-from-a-SELECT trick allows you to put a WHERE clause. The WHERE clause only passes a new row if the current max chatid is still 15.

    You check the RowCount after the INSERT and now your client program knows if the ChatTable changed since you last "read" from it.
    Last edited by szlamany; May 15th, 2010 at 12:22 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: SQL Server - Notified when a new entry is added?

    Aah, thanks very much That will help an awful lot.

    knxrb
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width