- Función javascript que genera la tabla en la pagina html:
Función javascript que genera la tabla en la pagina html:
function getData() {
var $tbl = $('#tableEvents');
$.ajax({
url: '../home/Get',
type: 'GET',
datatype: 'json',
success: function (data) {
debugger;
alert(data);
if (data.length >> 0) {
var newdata = data;
$tbl.empty();
$tbl.append(' @Html<tr class="columns"><th>@Html.DisplayNameFor(model =>=> model.NameEvent)@Html</th><th>@Html.DisplayNameFor(model =>=> model.Protocol)@Html</th><th>@Html.DisplayNameFor(model =>=> model.IP)@Html</th><th>@Html.DisplayNameFor(model =>=> model.Port)@Html</th><th>@Html.DisplayNameFor(model =>=> model.AccountID)@Html</th><th>@Html.DisplayNameFor(model =>=> model.SessionID)@Html</th><th>@Html.DisplayNameFor(model =>=> model.DateEvent)@Html</th><th>@Html.DisplayNameFor(model =>=> model.SecurityImpact)@Html</th><th>@Html.DisplayNameFor(model =>=> model.Resolved)'</th></tr>');
var rows = [];
for (var i = 0; i < newdata.length; i++) {
rows.push(' <tr class="rowEvent"><td>' + newdata[i].NameEvent +
'''</td><td>' + newdata[i].Protocol +
'''</td><td>' + newdata[i].IP +
'''</td><td>' + newdata[i].Port +
'''</td><td>' + newdata[i].AccountID +
'''</td><td>' + newdata[i].SessionID +
'''</td><td>' + newdata[i].DateEvent +
'''</td><td>' + newdata[i].SecurityImpact +
'</td><td><input type="checkbox" id="' + 'newdata[i].Id ');
+ '" value="' + newdata[i].Resolved + '"/></td><td><a class="btn btn-xs btn-warning"><span class="glyphicon glyphicon-info-sign"></span></a></td></tr > ');
}
}
$tbl.append(rows.join(''));
}
}
});
}
- HomeController:
HomeController:
SECURITYEVENTSRepository objRepo = new SECURITYEVENTSRepository();
[Route("Home"Home/Get"Get")]
public IEnumerableIEnumerable<SECURITYEVENTS> Get()
{
var events = objRepo.GetData();
return events;
}
Repositorio: public class SECURITYEVENTSRepository { public IEnumerable GetData() {
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(@"SELECT [Id],[NameEvent],[Protocol],[IP],[Port],[AccountID],[SessionID],[DateEvent],[SecurityImpact],[Resolved] FROM [events].[dbo].[SECURITYEVENTS]", connection)) { // Make sure the command object does not already have // a notification object associated with it. command.Notification = null; SqlDependency dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); if (connection.State == ConnectionState.Closed) connection.Open(); var events = new List<SECURITYEVENTS>(); using (var reader = command.ExecuteReader()) events = reader.Cast<IDataRecord>() .Select(x => new SECURITYEVENTS() { Id = (int)x["Id"], NameEvent = (string)x["NameEvent"], Protocol = (string)x["Protocol"], IP = (string)x["IP"], Port = (int)x["Port"], AccountID = (string)x["AccountID"], SessionID = (string)x["SessionID"], DateEvent = (string)x["DateEvent"], SecurityImpact = (string)x["SecurityImpact"], Resolved = (string)x["Resolved"], }).ToList(); return events; } } } private void dependency_OnChange(object sender, SqlNotificationEventArgs e) { EventsHub.Show(); } }
Modelo:
Repositorio:
public class SECURITYEVENTSRepository
{
public partialIEnumerable<SECURITYEVENTS> classGetData()
SECURITYEVENTS {
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
public int Id connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [Id],[NameEvent],[Protocol],[IP],[Port],[AccountID],[SessionID],[DateEvent],[SecurityImpact],[Resolved] FROM [events].[dbo].[SECURITYEVENTS]", connection))
{
get; set; } // Make sure the command object does not already have
public string NameEvent // a notification object associated with it.
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var events = new List<SECURITYEVENTS>();
using (var reader = command.ExecuteReader())
events = reader.Cast<IDataRecord>()
.Select(x => new SECURITYEVENTS()
{
get; set; } Id = (int)x["Id"],
public NameEvent = (string)x["NameEvent"],
Protocol {= get;(string)x["Protocol"],
set; } IP = (string)x["IP"],
public Port = (int)x["Port"],
AccountID = (string)x["AccountID"],
IP { get; set; SessionID = (string)x["SessionID"],
DateEvent = (string)x["DateEvent"],
SecurityImpact = (string)x["SecurityImpact"],
Resolved = (string)x["Resolved"],
}).ToList();
return events;
}
public Nullable Port }
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
get; set; EventsHub.Show();
}
}
Modelo:
public partial class SECURITYEVENTS
{
public stringint AccountIDId { get; set; }
public string NameEvent { get; set; }
public string SessionIDProtocol { get; set; }
public string IP { get; set; }
public stringNullable<int> DateEventPort { get; set; }
public string AccountID { get; set; }
public string SecurityImpactSessionID { get; set; }
public string DateEvent { get; set; }
public string ResolvedSecurityImpact { get; set; }
public string Resolved { get; set; }
}