sql server主动推送客户端更新数据

2019年11月19日 阅读数:156
这篇文章主要向大家介绍sql server主动推送客户端更新数据,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

小谈需求:

最近工做上接到一个需求,作一个web展现数据的报表,最好能实时更新,不限制所用技术。java

第一个问题:web服务器推送给浏览器新数据,一开始我想到的最快的最简单的方法就是 在web页面上js轮询了。由于咱们的数据更新频率并不快。 后来以为这种办法有点太土了。 或许长轮询更有效。  固然长轮询的技术不少了。 java 的dwr,c#的 signalr。c#还能够同过异步请求来本身写长轮询。web

遇到的第二个问题,就是数据库如何通知web服务器更新数据,下面即是sql server2008的推送了,经过sql server的触发器,当数据表有变化时(增,删,改)就经过tcp请求服务器,服务器会在启动后开启端口一直监听,随时等待通讯请求。当收到请求后,就从数据库读取新数据,推送给浏览器。总体大概就这样。sql

下面是数据库通知服务器。这是一个 winform的demo ,winfom就至关于咱们展现数据的服务器了。数据库

最后demo图:编程

如今我插入一条数据:

而后再看那个客户端:

刚插入的数据已经出现了哦。

客户端代码:

winform:c#

程序启动后,开启端口监听,若是有收到通讯,则通知 dataview更新数据。浏览器

using System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Data.SqlClient;
06 using System.Drawing;
07 using System.Linq;
08 using System.Net;
09 using System.Net.Sockets;
10 using System.Text;
11 using System.Windows.Forms;
12 using System.Threading;
13  
14 namespace sql_dependency
15 {
16     public partial class Form1 : Form
17     {
18         public Form1()
19         {
20             InitializeComponent();
21         }
22  
23         System.Data.SqlClient.SqlConnection conn = null;
24         string _connstr = "Data Source = 10.6.154.251; database=Temp;user id=sa;pwd=MOcyou0543_";
25         System.Data.SqlClient.SqlCommand command = null;
26  
27         private void Form1_Load(object sender, EventArgs e)
28         {
29             conn = new System.Data.SqlClient.SqlConnection(_connstr);
30             command = conn.CreateCommand();
31             command.CommandText = "select [A],[B],[C] From [Temp].[dbo].[Simple]";
32             SqlDependency.Start(_connstr);//启动
33             Thread t = new Thread(new ThreadStart(GetData));
34             t.Start();
35         }
36  
37  
38         private void GetData()
39         {
40  
41             SetData();
42             IPAddress localAddr = IPAddress.Parse("127.0.0.1");
43             TcpListener tcplistener = new TcpListener(localAddr, 10010);
44             tcplistener.Start();
45             byte[] btServerReceive = new byte[2048];
46             string strServerReceive = string.Empty;
47             while (true)
48             {
49                 TcpClient tcp = tcplistener.AcceptTcpClient();
50                 Console.WriteLine("Connected!");
51                 NetworkStream ns = tcp.GetStream();
52                 int intReceiveLength = ns.Read(btServerReceive, 0, btServerReceive.Length);
53                 strServerReceive = Encoding.ASCII.GetString(btServerReceive, 0, intReceiveLength);
54  
55                 SetData();
56                 tcp.Close();
57             }
58  
59         }
60         private delegate void ChangeDataView();
61         private void SetData()
62         {
63             if (this.InvokeRequired)
64             {
65                 this.Invoke(new ChangeDataView(SetData));
66             }
67             else
68             {
69                 using (SqlDataAdapter adapter = new SqlDataAdapter(command)) //查询数据
70                 {
71                     System.Data.DataSet ds = new DataSet();
72                     adapter.Fill(ds, 0, 100, "Simple");
73                     dataGridView1.DataSource = ds.Tables["Simple"];
74                 }
75  
76             }
77         }
78  
79  
80  
81         private void Form1_Closed(object sender, FormClosedEventArgs e)
82         {
83             //清理现场
84             SqlDependency.Stop(_connstr);
85             conn.Close();
86             conn.Dispose();
87         }
88  
89  
90     }
91

}服务器

 

数据库与clr集成,编写写dll:SqlDependency.dll,sql server将在可编程性中加载此dll,

01 using System;
02 using System.IO;
03 using System.Net;
04 using System.Net.Sockets;
05 using Microsoft.SqlServer.Server;
06  
07 using System.Net.Sockets;
08 namespace SqlDependency
09 {
10     public class Program
11     {
12  
13         [SqlFunction(IsDeterministic = true, DataAccess = DataAccessKind.Read)]
14         public static String WriteStringToFile(String FileFullPath, String Contend)
15         {
16  
17             FileInfo Fi = new FileInfo(FileFullPath);
18             if (!Fi.Directory.Exists)
19             {
20                 Fi.Directory.Create();
21             }
22  
23             using (StreamWriter rw = File.CreateText(FileFullPath))
24             {
25  
26                 rw.WriteLine(Contend);
27                 TcpClient tcpClient = new TcpClient();
28  
29                 try
30                 {
31                     if (tcpClient == null)
32                     {
33                         tcpClient = new TcpClient();
34                         tcpClient.ReceiveTimeout = 20000;
35                     }
36                     if (tcpClient.Connected == false)
37                     {
38                         System.Net.IPAddress address = System.Net.IPAddress.Parse(Contend);
39                         System.Net.IPHostEntry ipInfor = System.Net.Dns.GetHostByAddress(address);
40                         string hostName = ipInfor.HostName;
41                         IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10010);
42                         tcpClient.Connect(serverEndPoint);
43                         rw.Write(hostName);
44                     }
45                     rw.Write("链接成功,先发送指令");
46                     // Translate the passed message into ASCII and store it as a Byte array.
47                     Byte[] data = System.Text.Encoding.ASCII.GetBytes("new data!");
48  
49                     NetworkStream stream = tcpClient.GetStream();
50  
51                     // Send the message to the connected TcpServer.
52                     stream.Write(data, 0, data.Length);
53                     stream.Close();