我以前做过,但这次它不起作用。
我要做的就是 COUNT() 表中未读消息的数量。
表定义:
UserId(int), From(int), Type(nvarchar), Message(nvarchar), Read(bit)
Read 和UserId
columns 是我要检查的内容。我尝试了以下方法:
database.QuerySingle("SELECT COUNT(*) AS NumberOfNotifications FROM Notifications WHERE UserId = @0 AND Read = CONVERT(bit,0)", userid);
我也尝试了许多其他版本,但结果总是相同。我总是收到以下错误:
“/”应用程序中的服务器错误。
关键字“读取”附近的语法不正确。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Data.SqlClient.SqlException:关键字“读取”附近的语法不正确。
我正在尝试做的是: “在 NOTIFICATIONSTABLE 中获取未读消息的数量,其中 USERID = @0 AND READ = FALSE”
任何帮助都将不胜感激!
谢谢
更新
以下是我遇到问题的助手的完整代码。现在我已经修复了错误,我不知道为什么在我登录时它没有显示“您有新的通知消息” - 相应的表格中有 4 行。
@helper RetrievePhotoWithName(int userid)
{
var database = Database.Open("DUDE");
var name = database.QuerySingle("select FirstName, LastName, ProfilePicture from UserProfile where UserId = @0", userid);
var notifications = database.QuerySingle("SELECT COUNT(*) AS NumberOfNotifications FROM Notifications WHERE UserId = @0 AND [Read] = @1", userid, false);
var DisplayName = "";
if(notifications["NumberOfNotifications"] < 1)
{
DisplayName = name["FirstName"] + " " + name["LastName"];
}
else
{
DisplayName = name["FirstName"] + ", you have " + notifications["NumberOfNotifications"] + " new messages.";
}
<a href="@Href("~/Home")" title="My Account"><img src="@Href("~/Shared/Assets/Images/" + name["ProfilePicture"] + ".png")" id="MiniProfilePicture" /> @DisplayName</a>
database.Close();
}