我做了一个winform程序,我想要显示数据库中的更新数据,不想用定时器去重读数据库,然后将它显示出来,
请问有没有什么好的方法,让数据库一增加一条数据,就可以触发界面显示出来

解决方案 »

  1.   

    参考sql2005中的SqlDependency 类
    http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldependency(VS.80).aspx
      

  2.   

    您可以执行 SQL 语句并使用 SqlDependency 对象检测查询结果何时将不同于最初检索的结果。 如果您的应用程序保持状态,例如 Windows 窗体应用程序,则可以创建 SqlDependency 对象并保持对它的引用。您还可以将委托分配给 OnChange 事件。如果由于关联的命令导致结果发生变化,将引发 OnChange。您必须首先将 SqlDependency 与该命令相关联,然后才能执行该命令。如果您没有使用 OnChange,则可以检查 SqlDependency 的 HasChanges 属性,确定查询结果是否已更改。声明相关性,执行命令,并且在结果集更改时接收通知
    启动与服务器的 SqlDependency 连接。创建 SqlConnection 和 SqlCommand 对象,以便连接到服务器并定义 Transact-SQL 语句。创建一个新的 SqlDependency 对象,或者使用现有对象,然后将它绑定到 SqlCommand 对象。上述操作将在内部上创建一个 SqlNotificationRequest 对象,并根据需要将它绑定到命令对象上。此通知请求包含唯一标识此 SqlDependency 对象的 GUID。如果客户端侦听程序尚未处于活动状态,上述操作还启动该侦听程序。向 SqlDependency 对象的 OnChange 事件预订事件处理程序。使用 SqlCommand 对象的任意 Execute 方法来执行该命令。因为该命令绑定到通知对象,所以服务器认识到它必须生成一个通知,并且队列信息将指向相关性队列。停止与服务器的 SqlDependency 连接。如果同一用户或其他用户以后更改了基础数据,Microsoft SQL Server 将检测到对于此类更改有挂起的通知,并且发布已处理并通过分派机制转发到客户端的通知。与客户端联系所需的信息(例如客户端地址以及客户端理解的协议)嵌入在通知请求中。客户端侦听程序接收无效消息。基于 GUID(来自上面的第 2 步),客户端侦听程序然后定位关联的 SqlDependency 对象,并且引发 OnChange 事件。通过类似下面的 C# 代码所示的方式通知应用程序。void Initialization()
    {
        // Create a dependency connection
        SqlDependency.Start(cmd);
    }void SomeMethod()
    {
      // Assume c is an open SqlConnection.  // Create a new SqlCommand object.
      SqlCommand cmd=new SqlCommand( "SELECT * FROM Authors", c );  // Create a dependency and associate it with the SqlCommand.
      SqlDependency dep=new SqlDependency( cmd );
      // Maintain the refence in a class member.  // Subscribe to the SqlDependency event.
      dep.OnChange+=new OnChangeEventHandler( OnDependencyChange );  // Execute the command.
      cmd.ExecuteReader();
      // Process the DataReader.
    }// Handler method
    void OnDependencyChange( object sender, 
                              SqlNotificationsEventArgs e )
    {
      // Handle the event (for example, invalidate this cache entry).
    }void Termination()
    {
        // Release the dependency
        SqlDependency.Stop(cmd);
    }
      

  3.   


    sql2000不用定时器的话,还真不知道怎么做呢