小談需求:
最近工做上接到一個需求,作一個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更新數據。瀏覽器
02 |
using System.Collections.Generic; |
03 |
using System.ComponentModel; |
05 |
using System.Data.SqlClient; |
09 |
using System.Net.Sockets; |
11 |
using System.Windows.Forms; |
12 |
using System.Threading; |
14 |
namespace sql_dependency |
16 |
public partial class Form1 : Form |
20 |
InitializeComponent(); |
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 ; |
27 |
private void Form1_Load( object sender, EventArgs e) |
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)); |
38 |
private void GetData() |
42 |
IPAddress localAddr = IPAddress.Parse( "127.0.0.1" ); |
43 |
TcpListener tcplistener = new TcpListener(localAddr, 10010); |
45 |
byte [] btServerReceive = new byte [2048]; |
46 |
string strServerReceive = string .Empty; |
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); |
60 |
private delegate void ChangeDataView(); |
61 |
private void SetData() |
63 |
if ( this .InvokeRequired) |
65 |
this .Invoke( new ChangeDataView(SetData)); |
69 |
using (SqlDataAdapter adapter = new SqlDataAdapter(command)) |
71 |
System.Data.DataSet ds = new DataSet(); |
72 |
adapter.Fill(ds, 0, 100, "Simple" ); |
73 |
dataGridView1.DataSource = ds.Tables[ "Simple" ]; |
81 |
private void Form1_Closed( object sender, FormClosedEventArgs e) |
84 |
SqlDependency.Stop(_connstr); |
91 |
} 服務器 數據庫與clr集成,編寫寫dll:SqlDependency.dll,sql server將在可編程性中加載此dll,
04 |
using System.Net.Sockets; |
05 |
using Microsoft.SqlServer.Server; |
07 |
using System.Net.Sockets; |
08 |
namespace SqlDependency |
13 |
[SqlFunction(IsDeterministic = true , DataAccess = DataAccessKind.Read)] |
14 |
public static String WriteStringToFile(String FileFullPath, String Contend) |
17 |
FileInfo Fi = new FileInfo(FileFullPath); |
18 |
if (!Fi.Directory.Exists) |
20 |
Fi.Directory.Create(); |
23 |
using (StreamWriter rw = File.CreateText(FileFullPath)) |
26 |
rw.WriteLine(Contend); |
27 |
TcpClient tcpClient = new TcpClient(); |
31 |
if (tcpClient == null ) |
33 |
tcpClient = new TcpClient(); |
34 |
tcpClient.ReceiveTimeout = 20000; |
36 |
if (tcpClient.Connected == false ) |
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); |
45 |
rw.Write( "鏈接成功,先發送指令" ); |
47 |
Byte[] data = System.Text.Encoding.ASCII.GetBytes( "new data!" ); |
49 |
NetworkStream stream = tcpClient.GetStream(); |
52 |
stream.Write(data, 0, data.Length); |
|