我是Asp .Net平台的学生开发人员。根据我的研究,使用Asp.Net可以实现实时数据库。这就是为什么我知道我需要使用SignalR的原因。在我的示例中,我尝试构建它,但是在从SQL Server提取数据时遇到了空值。我不是专业人士,也不知道问题出在哪里或问题出在哪里。许多来源不清楚,以供学生在建立实时数据库问题中理解。您能帮我解决这个问题吗?也许您的答案可能是像我这样的学生的榜样。谢谢。
我的模特:
private fun setStatusBarColors(flags: Int, @ColorInt bgColor: Int) {
window.decorView.systemUiVisibility = flags
window.statusBarColor = bgColor
toolbar.setBackgroundColor(bgColor)
}
“布局”页面中的脚本部分:
public partial class TextMessage
{
public int textId { get; set; }
public string textOwner { get; set; }
public string textContent { get; set; }
public Nullable<int> groupFk { get; set; }
public Nullable<System.DateTime> textDate { get; set; }
public virtual ICollection<Groups> Groups { get; set; }
}
}
我的控制器和方法:
<script src="~/scripts/jquery.signalR-2.4.1.min.js"></script>
<script src="~/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var text = $.connection.AgmHub;
text.client.displayTextMessage = function () {
getData();
};
$.connection.hub.start();
getData;
});
function getData() {
var $tbl = $('#tblInfo');
$.ajax({
url: $("#Get").val(),
type: 'GET',
datatype: 'json',
success: function (data) {
$tbl.empty();
$.each(data.listEmp, function (i, model) {
$tbl.append(
'<tr>' +
'<td>' + textId + '</td>' +
'<td>' + textOwner + '</td>' +
'<td>' + textContent + '</td>' +
'<td>' + groupFk + '</td>' +
'<td>' + textDate + '</td>' +
'</tr>'
);
});
}
});
}
</script>
我的Index.cshtml:
public JsonResult Get()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AgmEntities1"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"Select [Id],[textId],[textOwner],[textContent],[groupFk],[textDate] From [dbo].[TextMessage] where [Status]<> 0", connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
SqlDataReader reader = command.ExecuteReader();
var listText = reader.Cast<IDataRecord>().Select(x => new
{
Id = (int)x["Id"],
textOwner = (string)x["textOwner"],
textContent = (string)x["textContent"],
groupFk = (int)x["groupFk"],
textDate = (System.DateTime)x["textDate"],
}).ToList();
return Json(new { listText = listText }, JsonRequestBehavior.AllowGet);
}
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
AgmHub.Show();
}