* İKİ FARKLI UYGULAMADAN AYNI BİLGİYE ULAŞIYOR. BİRİNDEN LİSTELENİYOR, DİĞERİNDEN DEĞİŞTİRİLİYOR. DEĞİŞTİRİLDİĞİ ANDA LİSTELEME YAPAN UYGULAMA OTOMATİK OLARAK DEĞİŞİKLİĞİ ALGILAYIP LİSTEYİ YENİLİYOR.
** KULLANILAN TEKNOLOJİLER ;
- VERİTABANI TARAFINDA SQLDEPENDENCY
- LİSTELEME TARAFINDAKİ EKRANIN ÖNYÜZÜNDE JQUERY İLE SIGNALR.
.
********** C# code tarafı : (index.aspx)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignalRRealTimeSQL
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static IEnumerable<Shippers> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [ShipperID] ,[CompanyName] ,[Phone]
FROM [NORTHWND].[dbo].[Shippers]", connection))
{
command.Notification = null;
SqlDependency.Start(connection.ConnectionString);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new Shippers()
{
ShipperID = x.GetInt32(0),
CompanyName = x.GetString(1),
Phone = x.GetString(2)
}).ToList();
}
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
MyHub.Show();
}
}
}
------------------------------------------------------------------
********** HTML code tarafı :(index.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SignalRRealTimeSQL.index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-3.3.1.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="/signalR/hubs"></script>
<script type="text/javascript">
$(function () {
var job = $.connection.myHub;
job.client.displayStatus = function () {
getData();
};
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tbl');
//var zaman = Date();
$.ajax({
url: 'index.aspx/GetData',
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
success: function (data) {
debugger;
if (data.d.length > 0) {
var newdata = data.d;
$tbl.empty();
$tbl.append(' <thead>');
//$tbl.append(' <tr><th>Zaman :' + zaman + ' </th>');
$tbl.append(' </thead>');
$tbl.append(' <thead>');
$tbl.append(' <tr><th>ShipperID</th><th>CompanyName</th> <th>Phone</th></tr>');
$tbl.append(' </thead>');
var rows = [];
for (var i = 0; i < newdata.length; i++) {
rows.push(' <tr><td>' + newdata[i].ShipperID + '</td><td>' + newdata[i].CompanyName + '</td><td>' + newdata[i].Phone + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1>Sql serverdan bilgiyi real time SignalR ile gösterir</h1>
<p>SqlDependency ve signal kullanır.</p>
</div>
</div>
<div>
<table id="tbl" class="table table-bordered table-striped"></table>
</div>
</form>
</body>
</html>
-----------------------------------------
** SIGNAL HUB CLASS :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRRealTimeSQL
{
public class MyHub : Hub
{
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.displayStatus();
}
}
}
-----------------------------------------
** STARTUP CLASS :
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalRRealTimeSQL.Startup))]
namespace SignalRRealTimeSQL
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
-------------------------------------
** SOLUTİON FOLDERS:
*** SQL SERVERDA VERİTABANI ÖZELLİKLERİNDEN:
Yorumlar
Yorum Gönder