[英]SQL Server CE: Select first value in column
I'm trying to get the date from my database but only the first one that matches, I don't need the other ones as I only want to display it one time and otherwise the foreach writes it out multiple time depending on the number of results of course. 我正在尝试从数据库中获取日期,但是只有第一个匹配的日期,我不需要其他日期,因为我只想显示一次,否则,foreach会多次写入它,具体取决于结果当然。
var getDate = "SELECT date FROM Test WHERE date = '" + inputDate + "'";
foreach (var c in db.Query(getDate)) {
DateTime Date = c.date;
var showDate = Date.ToString("MMMM dd, yyyy");
<a>@showDate</a>
}
I have tried limit
, first
and top
with no success, I've also found that it has to be SQL Server CE v.3.5+ but I don't know what I have, as I use webmatrix and cant find any info on it. 我尝试了
limit
, first
和top
,但都没有成功,我还发现它必须是SQL Server CE v.3.5 +,但是我不知道自己拥有什么,因为我使用了webmatrix并且找不到任何信息。
Is there any way to just display the first row of date that matches and write it out by itself all alone? 有什么方法可以显示匹配的第一行日期并单独写出来吗?
You can simply add a break
after <a>@showDate</a>
, as follow: 您可以在
<a>@showDate</a>
之后添加一个break
,如下所示:
var getDate = "SELECT date FROM Test WHERE date = '" + inputDate + "'";
foreach (var c in db.Query(getDate)) {
DateTime Date = c.date;
var showDate = Date.ToString("MMMM dd, yyyy");
<a>@showDate</a>
break;
}
break
command force the exit from a loop ( foreach
is a loop) break
命令强制退出循环( foreach
为循环)
建议的解决方案将加载所有匹配的日期,为什么不使用:
var getDate = "SELECT TOP 1 [date] FROM [Test] WHERE [date] = '" + inputDate + "'";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.