浅析SQL Server 2005中的主动式通知机制
2007-05-15 09:31:27 来源:WEB开发网核心提示: 有了上面的分析和相应的SQL设置后,现在让我们来观察一个使用SQL Server 2005主动式通知机制的Windows桌面应用程序的示例,浅析SQL Server 2005中的主动式通知机制(4),程序相应表单的设计界面如下图3所示:图3:表单的设计界面Public Class Desk
有了上面的分析和相应的SQL设置后,现在让我们来观察一个使用SQL Server 2005主动式通知机制的Windows桌面应用程序的示例。程序相应表单的设计界面如下图3所示:
图3:表单的设计界面
Public Class DeskNotification
Dim conn As New SqlConnection(ADONET20.My.Settings.AdventureWorksConnection)
Delegate Sub PopulateList()
Private Sub DeskNotification_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlDependency.Start(ADONET20.My.Settings.AdventureWorksConnection)
‘取得初始数据
ListProducts()
End Sub
Private Sub productListBox_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles productListBox.SelectedIndexChanged
Dim strItem As String = productListBox.SelectedItem.ToString
lblId.Text = strItem.Substring(0, strItem.IndexOf("-") - 1)
txtPrice.Text = strItem.Substring(strItem.IndexOf(":") + 1)
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cnn As New SqlConnection(ADONET20.My.Settings.AdventureWorksConnection)
If lblId.Text = "无" Then
MessageBox.Show("请选择某一条记录")
Exit Sub
End If
cnn.Open()
Dim cmd As New SqlCommand( _
"UPDATE Production.Product SET ListPrice=" & txtPrice.Text & " WHERE
ProductID=" & lblId.Text, _
cnn)
cmd.ExecuteNonQuery()
cnn.Close()
End Sub
Sub OnDependencyChanged(ByVal sender As Object, ByVal e As SqlNotificationEventArgs)
'SqlDependency对象的OnChanged事件触发时
'要执行的业务逻辑
Dim dR As DialogResult
dR = MessageBox.Show("数据已经完毕,要更新数据吗?", e.Info.ToString, _
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If dR = Windows.Forms.DialogResult.Yes Then
'由表单的主线程实现数据更新
Try
Me.Invoke(New PopulateList(AddressOf ListProducts))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Public Sub ListProducts()
'重新装载数据
' SqlDependency设置后,仅会注册一次的事件通知
Dim dep As New SqlDependency()
'设置SqlDependency对象的OnChanged事件发生时要调用哪个事件处理器
AddHandler dep.OnChange, AddressOf OnDependencyChanged
'限制查询的范围,避免太大的范围的大量用户都影响到这个范围内的数据以致使SQL Server
'频繁地触发通知
Using cmd As New SqlCommand( _
"SELECT ProductID, Name, ListPrice FROM Production.Product " & _
"WHERE ProductID BETWEEN @Start AND @End", conn)
With cmd
.Parameters.Add(New SqlParameter("@Start", Data.SqlDbType.Int))
.Parameters.Add(New SqlParameter("@End", Data.SqlDbType.Int))
.Parameters(0).Value = txtStart.Text
.Parameters(1).Value = txtEnd.Text
End With
'自动帮助我们设置SqlCommand的Notification属性所需的SqlNotificationRequest对象
'可以通过Debug来观察SqlCommand对象执行前后的关系
dep.AddCommandDependency(cmd)
productListBox.Items.Clear()
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
productListBox.Items.Add(reader("ProductID") & " - " & _
reader("Name").ToString & ": " & reader("ListPrice").ToString)
End While
End Using
conn.Close()
End Sub
Private Sub DeskNotification_FormClosing(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
SqlDependency.Stop(ADONET20.My.Settings.AdventureWorksConnection)
End Sub
End Class
在这个例子中,我们首先在Global.asax文件内的Application_Start事件加入通过SqlDependency类的静态方法Start启动监听。注意,这个Start方法需要传递数据库连接字符串。它将完成如下相应操作:
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
- ››SQL SERVER无法安装成功,sqlstp.log文件提示[未发...
- ››Sql Server中通过父记录查找出所有关联的子记录
- ››SqlServer触发器、存储过程和函数
- ››SQL Server 中的事务(含义,属性,管理)
- ››Sqlite数据库插入和读取图片数据
- ››Sql server 2005拒绝了对对象 'xx表' (数...
- ››Sql server 2005拒绝了对对象 'xx表' (数...
更多精彩
赞助商链接