我对我的SQL服务器返回的错误感到非常困惑 - 这会导致什么原因?
我正在运行的C#代码是:
List<CalendarEvent> events = new List<CalendarEvent>();
var db = Database.Open("myDatabase");
string username = HttpContext.Current.Request.Cookies.Get("username").Value;
var listOfGroups = db.Query("SELECT GroupID FROM Membership WHERE UserID = (SELECT UserID from Users WHERE Username = @0 )", username);
foreach(var groupID in listOfGroups)
{
var result = db.Query(
@"SELECT e.event_id, e.title, e.description, e.event_start, e.event_end, e.group_id, e.recurring
FROM event e
JOIN Membership m ON m.GroupID = e.group_id
WHERE e.recurring = 0
AND m.GroupID = @0
AND e.event_start >= @1
AND e.event_end <= @2
UNION ALL
SELECT e.event_id, e.title, e.description, DATEADD(week, w.weeks, e.event_start), DATEADD(week, w.weeks, e.event_end), e.group_id, e.recurring
FROM event e
JOIN Membership m ON m.GroupID = e.group_id
CROSS JOIN
( SELECT row_number() OVER (ORDER BY Object_ID) AS weeks
FROM SYS.OBJECTS
) AS w
WHERE e.recurring = 1
AND m.GroupID = $3
AND e.event_start >= @4
AND e.event_end <= @5", groupID, start, end, groupID, start, end
);
foreach(var record in result)
{
CalendarEvent cevent = new CalendarEvent();
cevent.id = record.event_id;
cevent.title = record.title;
cevent.description = record.description;
cevent.start = record.event_start;
cevent.end = record.event_end;
cevent.recurring = record.recurring;
events.Add(cevent);
}
}
return events;
}
错误:
异常详细信息:System.ArgumentException:不存在映射 对象类型WebMatrix.Data.DynamicRecord到已知的托管提供程序 本地类型。
可能导致此问题的原因以及如何解决?
我想要做的是返回所有重复出现的事件= 0,以及任何重复出现的事件= 1我希望在本周和之后的52周内返回。
答案 0 :(得分:1)
您的问题与groupID
输入参数相符。
var listOfGroups = db.Query("SELECT GroupID FROM Membership WHERE UserID = (SELECT UserID from Users WHERE Username = @0 )", username);
您的listOfGroups
将作为WebMatrix.Data.DynamicRecord
个对象的集合返回。您似乎只想要int
值,因为您在groupID
语句中得到foreach(var groupID in listOfGroups)
。
尝试替换最后一行:
AND e.event_end <= @5", groupID, start, end, groupID, start, end
使用:
AND e.event_end <= @5", (int)groupID.GroupID, start, end, (int)groupID.GroupID, start, end
这应该从每个动态查询结果的第一个(也是唯一的)列中提取值,该值是您之后的int组ID。
编辑:更正了DynamicRecord查询结果的值调用语法。